赞
踩
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
(1)再服务提供者controller中加入
- @Autowired
- private DiscoveryClient client;
- @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
- public Object discovery()
- {
- //盘点eureka中的微服务
- List<String> list = client.getServices();
- System.out.println("**********" + list);
-
- //获取某个微服务
- List<ServiceInstance> srvList = client.getInstances("springcloud-demo");
- for (ServiceInstance element : srvList) {
- System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
- + element.getUri());
- }
- return this.client;
- }
-
-
-
-
- import org.springframework.cloud.client.discovery.DiscoveryClient;
(2)服务提供者启动类中加入注解 @EnableDiscoveryClient
(3)自测
(4)修改消费者controller
- private static final String REST_URL_PREFIX = "http://localhost:8001";
-
- // 测试@EnableDiscoveryClient,消费端可以调用服务发现
- @RequestMapping(value = "/consumer/dept/discovery")
- public Object discovery()
- {
- return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
- }
测试: localhost/consumer/dept/discovery
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。