|
@@ -22,43 +22,27 @@ public class ConsumerController {
|
|
@Autowired
|
|
@Autowired
|
|
private DiscoveryClient discoveryClient;
|
|
private DiscoveryClient discoveryClient;
|
|
|
|
|
|
- // http://localhost:8080/echo/helloConsumer
|
|
|
|
|
|
+ // http://localhost:8080/echo/weight123
|
|
@GetMapping("/echo/{string}")
|
|
@GetMapping("/echo/{string}")
|
|
public String echo(@PathVariable("string") String str) {
|
|
public String echo(@PathVariable("string") String str) {
|
|
// 通过consumer提供的接口 来调用provider的相同接口
|
|
// 通过consumer提供的接口 来调用provider的相同接口
|
|
// 就得把当前服务作为客户端 来构建http请求
|
|
// 就得把当前服务作为客户端 来构建http请求
|
|
- // getForObject是发送指定url的请求 参数是url地址 + 参数类型 + 参数具体值
|
|
|
|
|
|
+ // getForObject是发送指定url的请求 参数是url地址 + 返回数据的类型 + 参数具体值
|
|
// String result = restTemplate.getForObject("http://localhost:8070/echo/{string}", String.class, str);
|
|
// String result = restTemplate.getForObject("http://localhost:8070/echo/{string}", String.class, str);
|
|
- // 使用服务名称 代替ip地址+端口号
|
|
|
|
-// String result = restTemplate.getForObject("http://service-provider-demo/echo/{string}", String.class, str);
|
|
|
|
-// System.out.println("result:" + result);
|
|
|
|
|
|
|
|
- String serviceId = "service-provider-demo";
|
|
|
|
- List<ServiceInstance> serviceInstances = new ArrayList<>();
|
|
|
|
- // 找到所有服务名
|
|
|
|
- List<String> services = discoveryClient.getServices();
|
|
|
|
- System.out.println(services);
|
|
|
|
- for (String service : services) {
|
|
|
|
- // 找到指定服务的实例
|
|
|
|
- List<ServiceInstance> instances = discoveryClient.getInstances(service);
|
|
|
|
- for (ServiceInstance instance : instances) {
|
|
|
|
- System.out.println(instance.getInstanceId() + ":" + instance.getHost() +
|
|
|
|
- ":" + instance.getPort() + ":" + instance.getUri());
|
|
|
|
- }
|
|
|
|
- if (service.equals(serviceId)) {
|
|
|
|
- serviceInstances = instances;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ // 使用服务名称 代替ip地址+端口号
|
|
|
|
+ String result = restTemplate.getForObject("http://service-provider-demo/echo/{string}", String.class, str);
|
|
|
|
+ System.out.println("result:" + result);
|
|
|
|
+ return "result:" + result;
|
|
|
|
+ }
|
|
|
|
|
|
- // 使用随机算法来分发
|
|
|
|
- Random random = new Random();
|
|
|
|
- int randomIndex = random.nextInt(serviceInstances.size());
|
|
|
|
- System.out.println("randomIndex:" + randomIndex);
|
|
|
|
- ServiceInstance serviceInstance = serviceInstances.get(randomIndex);
|
|
|
|
- String newUrl = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/echo/{string}";
|
|
|
|
- System.out.println("newUrl:" + newUrl);
|
|
|
|
- String result = restTemplate.getForObject(newUrl, String.class, str);
|
|
|
|
|
|
|
|
|
|
+ // http://localhost:8080/random/100
|
|
|
|
+ @GetMapping("/random/{range}")
|
|
|
|
+ public String random(@PathVariable("range") int range) {
|
|
|
|
+ // getForObject 的参数 第二个是返回数据的类型 参数是直接传递的
|
|
|
|
+ String result = restTemplate.getForObject("http://service-provider-demo/random/{range}", String.class, range);
|
|
|
|
+ System.out.println("result:" + result);
|
|
return "result:" + result;
|
|
return "result:" + result;
|
|
}
|
|
}
|
|
}
|
|
}
|