SpringCloud学习笔记(三)_服务提供者集群与服务发现Discovery

服务提供者集群

既然SpringCloud的是微服务结构,那么对于同一种服务,当然不可能只有一个节点,需要部署多个节点

架构图如下:

由上可以看出存在多个同一种服务提供者(Service Provider)

搭建服务提供者集群

1、参考:SpringCloud 快速入门搭建单机版的:Eureka Server、Service Provider、Service Consumer

2、根据支付模块服务提供者(test-springcloud-provider-payment8001),在父工程中,同样新建一个支付模块的服务提供者(test-springcloud-provider-payment8002)

服务8001与8002的配置中,除端口外,其他都相同,且spring.application.name应用名称必须相同,表明2个服务是同一种服务

服务8002配置文件如下:

复制代码
 1 # 端口
 2 server:
 3   port: 8002
 4 
 5 spring:
 6   application:
 7     name: cloud-payment-service
 8   #   数据源基本配置
 9   datasource:
10     driver-class-name: com.mysql.cj.jdbc.Driver
11     url: jdbc:mysql://localhost:3306/test_springcloud?allowPublicKeyRetrieval=true&useSSL=true
12     username: admin
13     password: 123456
14 
15 eureka:
16   client:
17     # 表示将自己注册进Eureka Server默认为true
18     register-with-eureka: true
19     # 是否从Eureka Server抓去已有的注册信息,默认是true
20     fetch-registry: true
21     # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
22     service-url:
23       defaultZone: http://localhost:8761/eureka
24 
25 mybatis:
26   mapperLocations: classpath:mapper/*Mapper.xml
27   # 所有entity别名类所在的包
28   type-aliases-pachage: com.test.springcloud.entities

View Code

3、重新启动项目,打开Eureka查看,发现有2个支付服务

4、使用订单模块消费者调用支付服务

消费者部分代码模块如下:

复制代码
 1 @Configuration
 2 public class AppConfig {
 3 
 4     /**
 5      * 注入restTemplate,请用请求rest接口
 6      * @return
 7      */
 8     @Bean
 9     // 标注此注解后,RestTemplate就具有了客户端负载均衡能力
10     // 负载均衡技术依赖于的是Ribbon组件~
11     // RestTemplate都塞入一个loadBalancerInterceptor 让其具备有负载均衡的能力
12     @LoadBalanced
13     public RestTemplate restTemplate(){
14         return new RestTemplate();
15     }
16 }
17 
18 
19 @RestController
20 @Slf4j
21 public class OrderController {
22 
23     public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
24 
25     @Autowired
26     private RestTemplate restTemplate;
27 
28     @GetMapping("/consumer/payment/get/{id}")
29     public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
30         return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
31     }
32 
33     ...
34 }

由上可以,通过配置向容器中注入了RestTemplate对象,而RestTemplate又被标注此@LoadBalanced注解后,RestTemplate就具有了客户端负载均衡能力,也就是说RestTemplate会轮流调用服务的各个节点,到达均衡的目的

5、测试调用,启动项目,使用地址:http://localhost:8000/consumer/payment/get/1,进行访问,可以看到已到达负载均衡的目的

服务发现Discovery

服务发现就是对于注册进eureka里面的微服务,可以通过服务发现获得该服务的信息

案例如下:

1、在微服务中使用@EnableDiscoveryClient注解,启用服务发现

复制代码
 1 // Eureka客户端
 2 @EnableEurekaClient
 3 // 启用服务发现
 4 @EnableDiscoveryClient
 5 @SpringBootApplication
 6 public class PaymentMain8001 {
 7     public static void main(String[] args) {
 8         SpringApplication.run(PaymentMain8001.class, args);
 9     }
10 }

2、编辑Controller,启用服务发现之后,它会自动向容器注入DiscoveryClient(服务发现客户端)

通过调用DiscoveryClient的getServices方法,从注册中心获取服务列表

通过调用DiscoveryClient的getInstances方法,从注册中心获取服务实例集

复制代码
 1 public class PaymentController {
 2 
 3     @Autowired
 4     private DiscoveryClient discoveryClient;
 5 
 6     @GetMapping(value = "/payment/discovery")
 7     public Object discovery(){
 8         // 获取服务列表
 9         List<String> services = discoveryClient.getServices();
10         for (String element : services) {
11             log.info("=====element:" + element);
12         }
13         
14         // 获取服务实例集
15         List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
16         for (ServiceInstance instance : instances) {
17             log.info("服务发现" + "\n"
18                     + "getServiceId === " + instance.getServiceId() + "\n"
19                     + "getHost === " + instance.getHost() + "\n"
20                     + "getPort === " + instance.getPort() + "\n"
21                     + "getUri === " + instance.getUri() );
22         }
23 
24         return  this.discoveryClient;
25     }
26 
27 } 

3、启用服务,使用地址:http://localhost:8001/payment/discovery,进行访问

页面结果:

后台日志:

由上可知,通过DiscoveryClient能获取到注册进eureka里面的微服务信息。

相关推荐
旖旎夜光6 分钟前
LeetCode 1576: 替换所有的问号(模拟) —— 题解
c++·学习·算法·leetcode·力控
mlidongfeng7 分钟前
【学习】ParallelKittens(PK)学习与分析
学习
坤坤子吖32 分钟前
Python基础语法学习:列表和元组
开发语言·笔记·python·学习
JWASX42 分钟前
Java 转 go 学习 - 接口
学习·golang
by209991 小时前
list 的使用:把节点、位置和操作联系起来的综合叙述(上)
数据结构·c++·笔记·list
I Am a robert girl1 小时前
从零读懂世界模型的持续学习:一份组合式基准的源码级拆解
学习·具身智能·持续学习·灾难性遗忘·机器人学习·世界模型·组合式基准
扶风ff1 小时前
练题簿在线免费刷题:刷题、背题、错题复习,一个小程序安排日常备考
学习·小程序
東隅已逝,桑榆非晚1 小时前
数据结构队列
数据结构·笔记·学习
老王爱玩车1 小时前
字符串和字符串函数
c语言·开发语言·数据结构·学习
扶风ff1 小时前
练题簿在线免费刷题:共享题库、学习任务、在线考试,岗位培训更方便
学习·小程序