• SpringCloud原生组件之Eureka服务注册中心


    1. Eureka相关概念

    微服务架构下的系统角色常见的有注册中心、服务提供者、远程客户端组件
    服务治理:管理服务与服务直接依赖关系,可以实现服务调用、负载均衡、容错、发现和注册
    服务注册:服务提供者将自己的服务信息(服务名、IP地址)告知服务注册中心
    服务发现:注册中心客户端组件从注册中心查询所有服务提供者信息,当其他服务下线后,注册中心能够告知注册中心客户端组件这种变化

    Eureka采用CS设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心,系统中的其他微服务,使用Eureka客户端连接到Eureka Server并维持心跳连接。Eureka包含Eureka Server和Eureka Client两个组件,Eureka Server提供服务注册服务,各个微服务节点通过配置启动后,会在Eureka Server中进行注册,这样Eureka Server中的服务注册表中将会存储所有可用服务节点的信息,这些信息可用再界面直接看到。Eureka Client通过注册中心进行访问,用于简化与Eureka Server的交互,其内部有一个使用轮询(round-robin)负载算法的负载均衡器,在应用启动后,将会向Eureka Server发送心跳(30秒),如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(90秒)

    2. SpringCloud版本说明

    由于SpringCloud原生组件中Eureka、Hystrix、Zuul等多个组件已经停更运维,本文采用Spring Cloud Netflix最更新版本对应的Spring Cloud Hoxton.SR12和Spring Boot 2.3.12.RELEASE
    在maven项目pom.xml文件中引入SpringBoot和SpringCloud依赖

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-dependenciesartifactId>
          <version>Hoxton.SR12version>
          <type>pomtype>
          <scope>importscope>
        dependency>
        <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-dependenciesartifactId>
          <version>2.3.12.RELEASEversion>
          <type>pomtype>
          <scope>importscope>
        dependency>
      dependencies>
    dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Spring Boot与Spring Cloud版本对应如下所示:
    Spring Boot与Spring Cloud版本对应

    3. 单机模式Eureka Server搭建

    3.1. 引入核心依赖

    新建SpringBoot项目,在其pom.xml文件中引入Eureka Server依赖

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3.2. 编写application.yml配置文件

    server:
      port: 8761
    spring:
      application:
        name: cloud-eureka-server
    eureka:
      client:
        fetch-registry: false #表示不检索服务
        register-with-eureka: false #表示不向注册中心注册
        service-url:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      server:
        enable-self-preservation: true #开启自我保护机制
        eviction-interval-timer-in-ms: 60000 #清理无效节点的时间间隔,Eureka Server处于保护状态,此配置无效
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3.3. 编写Application主启动类

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3.4. Eureka Server应用事件

    Eureka Server提供多个和Provider Instance相关的Spring上下文ApplicationEvent,当Server启动、服务注册、服务下线、服务续约等事件发生时,Eureka Server会发布相对应的ApplicationEvent,常见的Eureka Server应用事件如下:

    • EurekaInstanceRenewedEvent:服务续约事件
    • EurekaInstanceRegisteredEvent:服务注册事件
    • EurekaInstanceCanceledEvent:服务下线事件
    • EurekaRegistryAvailableEvent:Eureka注册中心启动事件
    • EurekaServerStartedEvent:Eureka Server启动事件

    新建监听类EurekaStateChangeListener

    @Component
    public class EurekaStateChangeListener {
    
        private static final Logger logger = LoggerFactory.getLogger(EurekaStateChangeListener.class);
    
        @EventListener
        public void listen(EurekaInstanceCanceledEvent event) {
            logger.info("{} {} 服务下线!", event.getServerId(), event.getAppName());
        }
    
        @EventListener
        public void listen(EurekaInstanceRegisteredEvent event) {
            InstanceInfo instanceInfo = event.getInstanceInfo();
            logger.info("{}:{} {} 服务上线!", instanceInfo.getIPAddr(), instanceInfo.getPort(), instanceInfo.getAppName());
        }
    
        @EventListener
        public void listen(EurekaInstanceRenewedEvent event) {
            logger.info("{} {} 服务续约!", event.getServerId(), event.getAppName());
        }
    
        @EventListener
        public void listen(EurekaServerStartedEvent event) {
            logger.info("{} {} 服务启动!", event.getSource(), event.getTimestamp());
        }
    
        @EventListener
        public void listen(EurekaRegistryAvailableEvent event) {
            logger.info("{} {} 注册中心服务启动!", event.getSource(), event.getTimestamp());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    4. 集群模式Eureka Server搭建

    只需要修改配置文件即可,本文启动多个实例验证,实际开发中会部署到多台服务器,配置文件如下:

    server:
      port: 8761
    spring:
      application:
        name: cloud-eureka-server
    eureka:
      client:
        fetch-registry: false #表示不检索服务
        register-with-eureka: false #表示不向注册中心注册
        service-url:
          defaultZone: http://eureka-node1:8761/eureka/,http://eureka-node2:8762/eureka/
      server:
        enable-self-preservation: false #关闭自我保护机制
        eviction-interval-timer-in-ms: 20000 #清理无效节点的时间间隔,Eureka Server处于保护状态,此配置无效
    ---
    spring:
      profiles: eureka-node2
    server:
      port: 8761
    eureka:
      instance:
        hostname: eureka-node1
    ---
    spring:
      profiles: eureka-node2
    server:
      port: 8762
    eureka:
      instance:
        hostname: eureka-node2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    需要在本机host文件中配置ip映射eureka-node1和eureka-node2,如果是分别部署,配置文件稍有不同,在node1中配置

    eureka:
      instance:
        hostname: eureka-node1 #服务端实例名称
      client:
        service-url:
          defaultZone: http://eureka-node2:8762/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在node2中配置

    eureka:
      instance:
        hostname: eureka-node2 #服务端实例名称
      client:
        service-url:
          defaultZone: http://eureka-node1:8761/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5. Eureka Server配置安全登录

    可以通过Spring Security对Eureka Server进行安全保护,需要引入Spring Security相关依赖

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-securityartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    在yml配置文件中配置用户名和密码信息

    spring:
      security:
        user:
          name: eureka
          password: 123456
     eureka:
      client:
        service-url:
          defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-node1:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-node2:8762/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Spring Security相关配置如下:

    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().ignoringAntMatchers("/eureka/**");
            super.configure(http);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    猜谜游戏、彩云词典爬虫、SOCKS5代理的 Go(Golang) 小实践,附带全代码解释
    一个命令下载Windows的所有pdb
    Map接口实现类的特点
    使用navicat for mongodb连接mongodb
    Partially ordered set
    Java SPI机制
    [强化学习总结6] actor-critic算法
    TCP端口崩溃,msg:socket(): Too many open files
    数据库系统原理与实践 笔记 #9
    尚品汇电商项目总结
  • 原文地址:https://blog.csdn.net/liu320yj/article/details/126215173