【发布时间】:2020-08-14 03:35:38
【问题描述】:
例如,我有一个对象存储在第一个 Web 服务上,可以通过“http://localhost:5000/articles/1”访问{"id":"1","name":"article1","description":"Desc1","partsId": 2}
另一个可以被“http://localhost:80/api/parts2”访问的网络服务存储部件对象{"id": 2,"manufacturer": "manufacture", "name": "name", "price": "228", "type": "type"}
我的 docker-compose.yml:
`version: '3.7'
services:
articles-service:
container_name: articles-service
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:5000"
volumes:
- .:/service
networks:
- ws_bridge
computer-parts:
build: ./comp_parts
command: python app.py
ports:
- "80:5000"
volumes:
- .:/service
container_name: external
networks:
- ws_bridge
networks:
ws_bridge:`
我希望能够通过其 id 获取 Part 对象
我写的代码:
@GetMapping("/parts/{id}")
public PcPart getPartFromOtherService(@PathVariable String id) {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<PcPart[]> response =
restTemplate.getForEntity(
"http://external:80/api/parts/" + id, // The problem lies here in the url
PcPart[].class);
PcPart[] parts = response.getBody();
return parts[0];
}
我应该使用什么 url 或如何连接它们才能检索数据(在 url 中将外部更改为 localhost 没有帮助)
【问题讨论】:
标签: java spring-boot docker web-services docker-compose