栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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依赖


  
    
      org.springframework.cloud
      spring-cloud-dependencies
      Hoxton.SR12
      pom
      import
    
    
      org.springframework.boot
      spring-boot-dependencies
      2.3.12.RELEASE
      pom
      import
    
  

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

3. 单机模式Eureka Server搭建 3.1. 引入核心依赖

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


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server

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处于保护状态,此配置无效
3.3. 编写Application主启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
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());
    }
}
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

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

eureka:
  instance:
    hostname: eureka-node1 #服务端实例名称
  client:
    service-url:
      defaultZone: http://eureka-node2:8762/eureka/

在node2中配置

eureka:
  instance:
    hostname: eureka-node2 #服务端实例名称
  client:
    service-url:
      defaultZone: http://eureka-node1:8761/eureka/
5. Eureka Server配置安全登录

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


    org.springframework.boot
    spring-boot-starter-security

在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/

Spring Security相关配置如下:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1040230.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号