fix:使用ip调用

上级 fc77265f
......@@ -26,9 +26,6 @@ public class ConsumerUserController {
@Autowired
private RestTemplate restTemplate;
/**
* feign方式调用
*/
@Autowired
private FeignUserService computeClient;
......@@ -36,9 +33,17 @@ public class ConsumerUserController {
@Autowired
private DiscoveryClient discoveryClient;
/**
* 使用LoadBalanceClient和RestTemplate结合的方式来访问
* http://127.0.0.1:8085/nacos/consumer/loadBalancerClient/1
* 使用服务名访问
*
* @param id
* @return
*/
@GetMapping(value = "/loadBalancerClient/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result loadBalancerClient(@PathVariable("id") Integer id) {
//使用LoadBalanceClient和RestTemplate结合的方式来访问
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-server-producer");
String url = String.format("http://%s:%s/user/%s", "nacos-server-producer", serviceInstance.getPort(), id);
System.out.println("request url:" + url);
......@@ -46,6 +51,24 @@ public class ConsumerUserController {
}
/**
* 使用ip访问
* http://127.0.0.1:8085/nacos/consumer/loadBalancerClientIp/1
*
* @param id
* @return
*/
@GetMapping(value = "/loadBalancerClientIp/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result loadBalancerClientIp(@PathVariable("id") Integer id) {
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-server-producer");
String url = String.format("http://%s:%s/user/%s", serviceInstance.getHost(), serviceInstance.getPort(), id);
System.out.println("request url:" + url);
//必须要用新的RestTemplate
RestTemplate restTemplate = new RestTemplate();
return Result.ok(restTemplate.getForObject(url, Object.class));
}
/**
* feign调用
* http://127.0.0.1:8085/nacos/consumer/1
* <p>
* http://localhost:9091/user/1
......@@ -58,6 +81,7 @@ public class ConsumerUserController {
return Result.ok(computeClient.selectOne(id));
}
/**
* 获取配置的变量
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册