要在provider中引入一个对象 DiscoveryClient,通过它来了解服务注册的元信息
- 在provider的启动类上加入注解
@EnableDiscoveryClient //启用服务发现客户端,以获取当前provider的注册信息
@SpringBootApplication
@EnableEurekaClient //启动eureka客户端
@EnableDiscoveryClient //启用服务发现客户端,以获取当前provider的注册信息
public class ProductApp {
public static void main(String[] args) {
SpringApplication.run( ProductApp.class,args);
}
}
- 在ProductController中加入托管Bean的注入
@Resource
private DiscoveryClient discoveryClient; //注意要导入springframework中的接口
@RequestMapping("/discover")
public Object discover(){
System.out.println( this.discoveryClient.description() );
System.out.println( ((EurekaDiscoveryClient)this.discoveryClient).getServices());
//再通过Service获取EurekaServiceInstance
return this.discoveryClient;
}
- 重启provider服务,测试一下