SpringCloud2020 学习笔记(十四)服务发现Discovery
- 我使用spring boot 2.2.2
- 我使用spring cloud Hoxton.SR1
- 我使用spring cloud alibaba 2.1.0.RELEASE
- 一.说明
- 二.编写服务发现简单代码
- 三.启动测试
- 四.添加服务发现的postman接口测试脚本
我使用spring boot 2.2.2
我使用spring cloud Hoxton.SR1
我使用spring cloud alibaba 2.1.0.RELEASE
为什么使用这个三个版本,是有讲究的;spring boot 2.2.2,spring cloud Hoxton.SR1,spring cloud alibaba 2.1.0.RELEASE
一.说明
服务发现,可以理解为:对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息。 或者说该服务把自己微服务的说明暴露给别人,别人调用就能看到该微服务的 微服务信息(关于我)
二.编写服务发现简单代码
代码语言:javascript复制 @Resource
private DiscoveryClient discoveryClient;
@GetMapping(value = "/payment/discovery")
public Object discovery(){
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("******微服务service " service);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info("微服务集群CLOUD-PAYMENT-SERVICE:");
log.info(instance.getServiceId() "t" instance.getHost() "t" instance.getPort() "t" instance.getUri());
}
return discoveryClient;
}记得启动类上要加@EnableDiscoveryClient注解
@EnableDiscoveryClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}三.启动测试
http://eureka7001.com:7001/
http://localhost:8001/payment/discovery



四.添加服务发现的postman接口测试脚本

导入postman即可 忘记怎么导入postman的可以点我看文章的#11和#12
项目地址: https://github.com/cookily/cloud2020.git


