樣例:
@Slf4j
@RestController
@RequestMapping(path = "/test")
public class TestController {
@Autowired
@LoadBalanced
private RestTemplate restTemplate;
@ApiOperation(value = "test", notes = "test")
@GetMapping(path = "/test")
public void test() {
Map<String, String> paramMap = new HashMap<>();
paramMap.put("channel", "11111");
paramMap.put("id", "22222");
String url = "http://SERVICE/auth-api/v1/inn/getInnRandomId?channel={channel}&id={id}";
ResponseEntity<JSONObject> responseEntity = restTemplate.getForEntity(url, JSONObject.class, paramMap);
if (responseEntity.getStatusCode().is2xxSuccessful()) {
Map<String, Object> innInfoBody = responseEntity.getBody();
innInfoBody.forEach((k, v) -> log.debug("key:" + k + "value:" + v));
} else {
log.debug("接口訪問失敗");
}
}
}
注意:
1.微服務間的訪問可以通過http://服務名/映射鏈接 這種方式來訪問
2.如果調用的服務存在上下文,那麼則要通過http://服務名/服務上下文/映射鏈接 這種方式來訪問。
3.當項目application.yml配置了服務上下文,但是在訪問鏈接不想帶上服務配置的上下文訪問,可以通過以下配置方式來實現(增加eureka.instance.home-page-url-path配置)。
eureka:
client:
service-url:
defaultZone: http://172.11.62.183:6600/eureka/,http://172.11.62.184:6600/eureka/,http://172.11.62.185:6600/eureka/,http://172.25.62.50:7200/eureka/
instance:
home-page-url-path: ${server.servlet.context-path}