假设在provider中,想知道自己注册到了哪个eureka,ip是多少,服务名叫什么等等这样的服务元信息, 如何才能做到?

要在provider中引入一个对象 DiscoveryClient,通过它来了解服务注册的元信息

  1. 在provider的启动类上加入注解

@EnableDiscoveryClient //启用服务发现客户端,以获取当前provider的注册信息

@SpringBootApplication
@EnableEurekaClient    //启动eureka客户端
@EnableDiscoveryClient   //启用服务发现客户端,以获取当前provider的注册信息
public class ProductApp {
    public static void main(String[] args) {
        SpringApplication.run( ProductApp.class,args);
    }
}
  1. 在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;
    }
  1. 重启provider服务,测试一下

猜你喜欢

转载自blog.csdn.net/weixin_45942124/article/details/109080759