日常开发中,有时候需要将服务的信息暴露给同事,方便调用联调,可以直接针对服务提供一个详细的接口。
开启
启动类添加:
@EnableEurekaClient // 启动的时候自动注册到 Eureka
@EnableDiscoveryClient // 服务发现
使用
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--完善监控信息--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
# Eureka 配置 eureka: client: service-url: #这里模拟的是集群 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: springcloud-provider-dept8001 # 修改 eureka 上的默认描述信息 prefer-ip-address: true #true 显示服务的ip # info 配置 info: app.name: springcloud-provider commony.name: xxxx.com
@Resource private DiscoveryClient discoveryClient; @GetMapping(value = "/payment/discovery") public Object discovery() { List<String> services = discoveryClient.getServices(); for (String element : services) { log.info("******element:" + element); } List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); for (ServiceInstance instance : instances) { log.info(instance.getServiceId() + "\t" + instance.getInstanceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri ()); } return this.discoveryClient; }