Java之 Spring Cloud 微服务 Eureka (第一个阶段)【二】【SpringBoot项目实现商品服务器端是调用】

x33g5p2x  于2021-11-18 转载在 Java  
字(13.9k)|赞(0)|评价(0)|浏览(570)

一、服务注册Eureka基础

1、微服务的注册中心

注册中心可以说是微服务架构中的”通讯录“,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就这里找到服务的地址,进行调用。

(1)注册中心的主要作用

服务注册中心(下称注册中心)是微服务架构非常重要的一个组件,在微服务架构里主要起到了协调者
的一个作用。注册中心一般包含如下几个功能:

  1. 服务发现:
    服务注册/反注册:保存服务提供者和服务调用者的信息
    服务订阅/取消订阅:服务调用者订阅服务提供者的信息,最好有实时推送的功能
    服务路由(可选):具有筛选整合服务提供者的能力。
  2. 服务配置:
    配置订阅:服务提供者和服务调用者订阅微服务相关的配置
    配置下发:主动将配置推送给服务提供者和服务调用者
  3. 服务健康检测
    检测服务提供者的健康情况

(2) 常见的注册中心

Zookeeper
zookeeper它是一个分布式服务框架,是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等。简单来说zookeeper=文件系统+监听通知机制。

Eureka
Eureka是在Java语言上,基于Restful Api开发的服务注册与发现组件,Springcloud Netflix中的重要组件

Consul
Consul是由HashiCorp基于Go语言开发的支持多数据中心分布式高可用的服务发布和注册服务软件,采用Raft算法保证服务的一致性,且支持健康检查。

Nacos
Nacos是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。

简单来说 Nacos 就是注册中心 + 配置中心的组合,提供简单易用的特性集,帮助我们解决微服务开发必会涉及到的服务注册与发现,服务配置,服务管理等问题。

Nacos 还是 Spring Cloud Alibaba 组件之一,负责服务注册与发现。

最后我们通过一张表格大致了解Eureka、Consul、Zookeeper的异同点。选择什么类型的服务注册与发现组件可以根据自身项目要求决定。

2、Eureka的概述

(1) Eureka的基础知识

Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能。

上图简要描述了Eureka的基本架构,由3个角色组成:
1、Eureka Server

  • 提供服务注册和发现

2、Service Provider

  • 服务提供方
  • 将自身服务注册到Eureka,从而使服务消费方能够找到

3、Service Consumer

  • 服务消费方
  • 从Eureka获取注册服务列表,从而能够消费服务

(2) Eureka的交互流程与原理

图是来自Eureka官方的架构图,大致描述了Eureka集群的工作过程。图中包含的组件非常多,可能比较难以理解,我们用通俗易懂的语言解释一下:

  • Application Service 相当于本书中的服务提供者,Application Client相当于服务消费者;
  • Make Remote Call,可以简单理解为调用RESTful API;
  • us-east-1c、us-east-1d等都是zone,它们都属于us-east-1这个region;

由图可知,Eureka包含两个组件:Eureka Server 和 Eureka Client,它们的作用如下:

  • Eureka Client是一个Java客户端,用于简化与Eureka Server的交互;
  • Eureka Server提供服务发现的能力,各个微服务启动时,会通过Eureka Client向Eureka Server进行注册自己的信息(例如网络信息),Eureka Server会存储该服务的信息;
  • 微服务启动后,会周期性地向Eureka Server发送心跳(默认周期为30秒)以续约自己的信息。如果Eureka Server在一定时间内没有接收到某个微服务节点的心跳,Eureka Server将会注销该微服务节点(默认90秒);
  • 每个Eureka Server同时也是Eureka Client,多个Eureka Server之间通过复制的方式完成服务注册表的同步;
  • Eureka Client会缓存Eureka Server中的信息。即使所有的Eureka Server节点都宕掉,服务消费者依然可以使用缓存中的信息找到服务提供者。

综上,Eureka通过心跳检测、健康检查和客户端缓存等机制,提高了系统的灵活性、可伸缩性和可用性。

3、搭建Eureka注册中心

(1)搭建Eureka服务中心

1)创建子模块
01)工程搭建

02)添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  5. </dependency>
  6. </dependencies>
03)编写配置文件

  1. server:
  2. port: 9000 #端口
  3. # 配置eureka server
  4. eureke:
  5. instance:
  6. hostname: localhost
  7. client:
  8. register-with-eureka: false # 是否将自己注册到注册中心
  9. fetch-registry: false # 是否从eureka中获取注册信息
  10. # 配置暴露给Eureka Client 的 请求地址、
  11. service-url:
  12. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
04)创建对应的启动类

  1. package cn.itbluebox.eureke;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. //激活eurekaserver
  7. @EnableEurekaServer
  8. public class EurekaServerApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(EurekaServerApplication.class,args);
  11. }
  12. }
05)运行测试

访问:http://localhost:9000/

(2)将服务提供者注册到eurekaServer上

1)引入EurekaClient的坐标

修改product_service的pom.xml

  1. <!--引入EurekaClient-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
2)修改application.yml添加EurekaServer的信息

  1. server:
  2. port: 9001 #端口
  3. spring:
  4. application:
  5. name: service-product #服务名称
  6. datasource:
  7. driver-class-name: com.mysql.jdbc.Driver
  8. url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8
  9. username: root
  10. password: root
  11. jpa:
  12. database: MySQL
  13. show-sql: true
  14. open-in-view: true
  15. #配置Eureka
  16. eureka:
  17. client:
  18. service-url:
  19. defaultZone: http://localhost:9000/eureka/
  20. instance:
  21. prefer-ip-address: true #使用ip地址注册
3)修改启动类,添加服务发现的支持(可选)

  1. package cn.itbluebox.product;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.autoconfigure.domain.EntityScan;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. @SpringBootApplication
  7. @EntityScan("cn.itbluebox.product.entity")
  8. //激活Eureka
  9. //@EnableEurekaClient
  10. @EnableDiscoveryClient //写不写都一样
  11. public class ProductApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(ProductApplication.class,args);
  14. }
  15. }
4)运行测试,启动商品微服务

访问测试:http://localhost:9000/

多了

(3)服务消费者通过注册中心获取服务列表,并调用

Eureka中的元数据:服务的主机名,ip,等信息.可以通过eurekaserver进行获取,用于服务之间的调用,

1)引入EurekaClient的坐标

  1. <!--引入EurekaClient-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
2)修改application.yml添加EurekaServer的信息

  1. server:
  2. port: 9002 #端口
  3. spring:
  4. application:
  5. name: service-order #服务名称
  6. datasource:
  7. driver-class-name: com.mysql.jdbc.Driver
  8. url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8
  9. username: root
  10. password: root
  11. jpa:
  12. database: MySQL
  13. show-sql: true
  14. open-in-view: true
  15. #配置Eureka
  16. eureka:
  17. client:
  18. service-url:
  19. defaultZone: http://localhost:9000/eureka/
  20. instance:
  21. prefer-ip-address: true #使用ip地址注册
3)完善order_service当中的 OrderController

  1. package cn.itbluebox.order.controller;
  2. import cn.itbluebox.order.entity.Product;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.cloud.client.ServiceInstance;
  5. import org.springframework.cloud.client.discovery.DiscoveryClient;
  6. import org.springframework.web.bind.annotation.*;
  7. import org.springframework.web.client.RestTemplate;
  8. import java.util.List;
  9. @RestController
  10. @RequestMapping("/order")
  11. public class OrderController {
  12. //注入RestTemplate对象
  13. @Autowired
  14. private RestTemplate restTemplate;
  15. /* 注入DiscoveryClient: SpringCloud 提供的获取原数组的工具类 调用方法获取服务的元数据信息 */
  16. @Autowired
  17. private DiscoveryClient discoveryClient;
  18. /* 参数:商品ID 通过订单系统,调用商品服务 1、需要配置商品对象 2、需要调用商品服务 3、entity.Product 使用java 当中的urlconnection,httpclient,okhttp */
  19. @RequestMapping(value = "/buy/{id}", method = RequestMethod.GET)
  20. public Product findById(@PathVariable Long id) {
  21. //调用discoveryClient方法
  22. //已调用服务名称获取所有的元数据
  23. List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
  24. //获取唯一的一个元数据
  25. ServiceInstance instance = instances.get(0);
  26. //根据元数据中的主机地址和端口号拼接请求微服务的URL
  27. Product product = null;
  28. //如何调用商品服务?
  29. product = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/"+id,Product.class);
  30. return product;
  31. }
  32. @RequestMapping(method = RequestMethod.POST)
  33. public String save(@RequestBody Product product) {
  34. //调用discoveryClient方法
  35. //已调用服务名称获取所有的元数据
  36. List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
  37. //获取唯一的一个元数据
  38. ServiceInstance instance = instances.get(0);
  39. restTemplate.postForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/",product,Product.class);
  40. return "保存成功";
  41. }
  42. @RequestMapping(method = RequestMethod.PUT)
  43. public String update(@RequestBody Product product) {
  44. //调用discoveryClient方法
  45. //已调用服务名称获取所有的元数据
  46. List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
  47. //获取唯一的一个元数据
  48. ServiceInstance instance = instances.get(0);
  49. restTemplate.put("http://"+instance.getHost()+":"+instance.getPort()+"/product/",product);
  50. return "更新成功";
  51. }
  52. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  53. public String delete(@PathVariable Long id) {
  54. //调用discoveryClient方法
  55. //已调用服务名称获取所有的元数据
  56. List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
  57. //获取唯一的一个元数据
  58. ServiceInstance instance = instances.get(0);
  59. restTemplate.delete("http://"+instance.getHost()+":"+instance.getPort()+"/product/"+id);
  60. return "删除成功";
  61. }
  62. }
4)运行测试

访问http://localhost:9002/order/buy/1

二、服务注册Eureka高级

1、Eureka Server 高可用集群

在上一个章节,实现了单节点的Eureka Server的服务注册与服务发现功能。

Eureka Client会定时连接Eureka Server,获取注册表中的信息并缓存到本地。

微服务在消费远程API时总是使用本地缓存中的数据。

因此一般来说,即使Eureka Server发生宕机,也不会影响到服务之间的调用。但如果Eureka Server宕机时,某些微服务也出现了不可用的情况,Eureka Server中的缓存若不被刷新,就可能会影响到微服务的调用,甚至影响到整个应用系统的高可用。

因此,在生成环境中,通常会部署一个高可用的Eureka Server集群。

Eureka Server可以通过运行多个实例并相互注册的方式实现高可用部署,Eureka Server实例会彼此增量地同步信息,从而确保所有节点数据一致。

事实上,节点之间相互注册是Eureka Server的默认行为。

(1)搭建 Eureka Server高可用集群

1 准备2个EurekaServer,需要相互注册

1号server : 9000
2号server : 8000I

2需要将微服务注册到两个EurekaServer上

1)修改eureka_server当中的application.yml

  1. #模拟两个EurekaServer
  2. #端口9000 , 8000
  3. #两个server需要相互注册
  4. spring:
  5. application:
  6. name: eureka-server
  7. server:
  8. port: 9000 #端口
  9. #配置eureka server
  10. eureka:
  11. client:
  12. #register-with-eureka: false #是否将自己注册到注册中心
  13. #fetch-registry: false #是否从eureka中获取注册信息
  14. service-url: #配置暴露给Eureka Client的请求地址
  15. defaultZone: http://127.0.0.1:8000/eureka/

运行测试

打开Run Dashboard

  • 修改application.yml

  1. #模拟两个EurekaServer
  2. #端口9000 , 8000
  3. #两个server需要相互注册
  4. spring:
  5. application:
  6. name: eureka-server
  7. server:
  8. port: 8000 #端口
  9. #配置eureka server
  10. eureka:
  11. client:
  12. #register-with-eureka: false #是否将自己注册到注册中心
  13. #fetch-registry: false #是否从eureka中获取注册信息
  14. service-url: #配置暴露给Eureka Client的请求地址
  15. defaultZone: http://127.0.0.1:9000/eureka/

启动多个eureka_server

实现eureka的相互注册

访问地址:
http://localhost:9000/

http://localhost:8000/

2)启动product_service

将product_service注册到http://localhost:8000/eureka/
修改product_service当中的application.yml

  1. server:
  2. port: 9001 #端口
  3. spring:
  4. application:
  5. name: service-product #服务名称
  6. datasource:
  7. driver-class-name: com.mysql.jdbc.Driver
  8. url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8
  9. username: root
  10. password: root
  11. jpa:
  12. database: MySQL
  13. show-sql: true
  14. open-in-view: true
  15. #配置Eureka
  16. eureka:
  17. client:
  18. service-url:
  19. defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/
  20. instance:
  21. prefer-ip-address: true #使用ip地址注册
  • 启动product_service

http://localhost:9000/上查看

http://localhost:8000/

3)启动order_service

将order_service注册到http://localhost:8000/eureka/
修改order_service当中的application.yml

  1. server:
  2. port: 9002 #端口
  3. spring:
  4. application:
  5. name: service-order #服务名称
  6. datasource:
  7. driver-class-name: com.mysql.jdbc.Driver
  8. url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8
  9. username: root
  10. password: root
  11. jpa:
  12. database: MySQL
  13. show-sql: true
  14. open-in-view: true
  15. #配置Eureka
  16. eureka:
  17. client:
  18. service-url:
  19. defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/
  20. instance:
  21. prefer-ip-address: true #使用ip地址注册

访问:http://localhost:9000/

访问:http://localhost:8000/

4)测试

断开一台eurekaService

8000断开

测试数据是否可以正确获取:http://localhost:9002/order/buy/1

2、显示护IP

修改eureka_server当中的application.yml,修改回之前的配置方便后期测试

  1. #模拟两个EurekaServer
  2. #端口9000 , 8000
  3. #两个server需要相互注册
  4. spring:
  5. application:
  6. name: eureka-server
  7. server:
  8. port: 9000 #端口
  9. #配置eureka server
  10. eureka:
  11. client:
  12. register-with-eureka: false #是否将自己注册到注册中心
  13. fetch-registry: false #是否从eureka中获取注册信息
  14. service-url: #配置暴露给Eureka Client的请求地址
  15. defaultZone: http://127.0.0.1:8000/eureka/

1)使product_service暴露对应的ip

修改product_service当中的application.yml

  1. server:
  2. port: 9001 #端口
  3. spring:
  4. application:
  5. name: service-product #服务名称
  6. datasource:
  7. driver-class-name: com.mysql.jdbc.Driver
  8. url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8
  9. username: root
  10. password: root
  11. jpa:
  12. database: MySQL
  13. show-sql: true
  14. open-in-view: true
  15. #配置Eureka
  16. eureka:
  17. client:
  18. service-url:
  19. defaultZone: http://localhost:9000/eureka/
  20. instance:
  21. prefer-ip-address: true #使用ip地址注册
  22. instance-id: ${spring.cloud.client.ip-address}:${server.port} #向组成中心注册服务ID

重新运行并测试

访问:http://localhost:9000/

3、服务续约时间设置

在服务的提供者,设置心跳间隔,设置续约到期时间
修改product_service当中的application.yml

  1. server:
  2. port: 9001 #端口
  3. spring:
  4. application:
  5. name: service-product #服务名称
  6. datasource:
  7. driver-class-name: com.mysql.jdbc.Driver
  8. url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8
  9. username: root
  10. password: root
  11. jpa:
  12. database: MySQL
  13. show-sql: true
  14. open-in-view: true
  15. #配置Eureka
  16. eureka:
  17. client:
  18. service-url:
  19. defaultZone: http://localhost:9000/eureka/
  20. instance:
  21. prefer-ip-address: true #使用ip地址注册
  22. instance-id: ${spring.cloud.client.ip-address}:${server.port} #向组成中心注册服务ID
  23. lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id
  24. lease-expiration-duration-in-seconds: 10 #续约到期的时间

重新启动

4、Eureka自我保护机制

eureka_server当中的application.yml当中配置,中配置关闭自我保护,和剔除服务间

  1. server:
  2. enable-self-preservation: false #关闭自我保护
  3. eviction-interval-timer-in-ms: 4000 #剔除服务间隔

三、Eureka源码解析

1、SpringBoot中的自动装载

(1)ImportSelector

ImportSelector接口是Spring导入外部配置的核心接口,在SpringBoot的自动化配置和@EnableXXX(功能性注解)中起到了决定性的作用。

当在@Configuration标注的Class上使用@Import引入了一个ImportSelector实现类后,会把实现类中返回的Class名称都定义为bean。

1)创建模块

2)创建对应的User实体类

  1. package cn.itbluebox.bean;
  2. import lombok.Data;
  3. /* Bean对象 */
  4. @Data
  5. public class User {
  6. private String username;
  7. private Integer age;
  8. }
3)创建UserConfiguration

  1. package cn.itbluebox.bean;
  2. import org.springframework.context.annotation.Bean;
  3. /* 没有Spring注解 */
  4. public class UserConfiguration {
  5. @Bean
  6. public User getUser(){
  7. return new User();
  8. }
  9. }
4)创建UserImportSelector

UserImportSelector接口继承ImportSelector,他和ImportSelector的区别在于装载bean的时机上,UserImportSelector需要等所有的@Configuration都执行完毕后才会进行装载。

  1. package cn.itbluebox.bean;
  2. import org.springframework.context.annotation.ImportSelector;
  3. import org.springframework.core.type.AnnotationMetadata;
  4. public class UserImportSelector implements ImportSelector {
  5. @Override
  6. public String[] selectImports(AnnotationMetadata annotationMetadata) {
  7. //获取配置类的名称
  8. return new String[]{UserConfiguration.class.getName()};//自动找到对应的类将其加载到Spring容器当中
  9. }
  10. }
5)创建EnableUserBean自定义注解

  1. package cn.itbluebox.bean;
  2. import org.springframework.context.annotation.Import;
  3. import java.lang.annotation.*;
  4. @Retention(RetentionPolicy.RUNTIME)
  5. @Documented
  6. @Target(ElementType.TYPE) //设置当前注解是可以配置到类上的注解
  7. @Import(UserImportSelector.class)
  8. public @interface EnableUserBean {
  9. }
6)创建测试类运行
01)创建TestApplication

  1. package cn.itbluebox.test;
  2. import cn.itbluebox.bean.EnableUserBean;
  3. import cn.itbluebox.bean.User;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. @EnableUserBean
  6. public class TestApplication {
  7. public static void main(String[] args) {
  8. //获取Spring容器
  9. AnnotationConfigApplicationContext ac =
  10. new AnnotationConfigApplicationContext(TestApplication.class);
  11. User user = ac.getBean(User.class);
  12. System.out.println(user);
  13. }
  14. }
02)为了打印的时候有值,设置UserConfiguration初始化User的时候设置一些值

  1. package cn.itbluebox.bean;
  2. import org.springframework.context.annotation.Bean;
  3. /* 没有Spring注解 */
  4. public class UserConfiguration {
  5. @Bean
  6. public User getUser(){
  7. User user = new User();
  8. user.setAge(12);
  9. user.setUsername("张三");
  10. return user;
  11. }
  12. }
03)运行测试

代码执行流程
-->@EnableUserBean --> UserImportSelector --> UserConfiguration --> User

(2)springBoot自动装载

SpringBoot开箱即用的特点,很大程度上归功于ImportSelector。接下来我们看下springBoot是如何在
spring的基础上做扩展的。
在SpringBoot中最重要的一个注解SpringBootApplication

2、Eureka服务注册核心源码解析

EnableEurekaServer注解作用
EurekaServer启动过程

相关文章

最新文章

更多

目录