目录
  • 1.多节点无缝切换问题
  • 2.服务注册与发现 Eureka
  • 3.Springboot集成Eureka
    • 3.1 父包pom依赖
    • 3.2 eureka服务端
    • 3.3 客户端
      • pom依赖
      • yml配置
    • 3.4 控制台

    1.多节点无缝切换问题

    • 分布式节点中的服务宕机或者重启不影响客户端使用
    • 分布式节点中的服务宕机重启不影响业务服务内部通信

    如果在某个分布式系统中想要解决上述问题,那么这篇文章就是精华之处。

    回顾一下以前的常用手段:

    • 单节点运行,其他节点备用,无法无缝连接,内网通信无法保证
    • 多节点运行,nginx转发,这种时候需要加探测,内网通信需要先走到nginx
    • 多节点运行,再运行gateway做静态转发,宕机节点无法过滤

    那么服务注册与发现就油然而生。

    2.服务注册与发现 Eureka

    Eureka是什么?

    Eureka是springcloud的核心模块之一,它是一个基于RestFul的服务,用于实现中间层服务发现和故障转移,Eureka对于微服务来说是非常重要的。

    有了Eureka后,在服务中通信只需要使用对应的服务表示,不再需要再配置文件中配一堆地址了,功能类似于dubbo的注册中心zookeeper。

    Eureka原理

    Eureka采用C/S的架构设计,所有的客户端都往服务端注册,在客户端中都与Eureka服务端保持心跳连接,在内网通信中可以直接使用服务名来调用其他服务,例如zuul、gatewway、内网RPC通信。

    Eureka基于心跳的形式保持连接,在客户端启动后,默认每30s往服务端发送心跳,如果服务端在默认90s(三个周期)后没有接收客户端的心跳,则会将这个客户端移除。

    3.Springboot集成Eureka

    3.1 父包pom依赖

    基于springboot2.6.8,spring-cloud-dependencies2021.0.3

    <parent>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-parent</artifactId>
    	<version>2.6.8</version>
    </parent>
    <dependencyManagement>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-dependencies</artifactId>
    			<version>2021.0.3</version>
    			<type>pom</type>
    			<scope>import</scope>
    		</dependency>
    	</dependencies>
    </dependencyManagement>

    3.2 eureka服务端

    eureka服务端依赖

    <!--注册中心eureka-->
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    

    yml配置

    server:
      port: 7510

    eureka:
      instance:
        hostname: localhost
        #显示真实IP 显示DS replicas副本集
        prefer-ip-address: true
      client:
        # false表示自己端就是注册中心,不需要去检索服务
        fetch-registry: false
        # false表示不向注册中心注册自己,因为自己本身就是注册中心
        register-with-eureka: false
        #其他服务的注册地址
        service-url:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      server:
        #关闭自我保护
        enable-self-preservation: false
        #启用主动失效,并且每次主动检测客户端 间隔为3s 默认60s
        #解决失联服务没被移除的问题
        eviction-interval-timer-in-ms: 3000

    Java代码

    启动类添加注解

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaApplication.class, args);
        }
    }
    

    获取当前已注册的服务信息

    List<Application> applications = EurekaServerContextHolder.getInstance().getServerContext().getRegistry().getSortedApplications();
    applications.forEach(application -> {
        application.getInstances().forEach(info -> {
        	//c.e.e.controller.EurekaTestController    : ORDER-SERVICE -- localhost:order-service:7530
            log.info("{} -- {}", info.getAppName(), info.getInstanceId());
        });
    });

    3.3 客户端

    pom依赖

    <!--注册中心eureka client-->
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    

    yml配置

    server:
      port: 7530

    eureka:
      client:
        service-url:
          #服务端注册地址
          defaultZone: http://127.0.0.1:7510/eureka/
      instance:
        #控制台status显示真实ip 需要配合prefer-ip-address
    #    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
        #显示真实IP
        prefer-ip-address: true
        #每3秒发送心跳,证明存活
        lease-renewal-interval-in-seconds: 3
        #告知服务端超过5秒未收到当前服务心跳视为挂掉
        lease-expiration-duration-in-seconds: 5

    spring:
      application:
        name: order-service
      jackson:
        default-property-inclusion: non_null
        time-zone: Asia/Shanghai
      cloud:
        inetutils:
          #指定取网段的网卡 未开始真实ip时默认就是localhost
          preferred-networks: 192.168.0

    preferred-networks和prefer-ip-address均配置时,如下图所示

    SpringCloud Eureka服务注册中心应用入门详解

    都不配置时,如下图所示,此时内网ip是不通的

    SpringCloud Eureka服务注册中心应用入门详解

    启动类上加@EnableEurekaClient注解

    3.4 控制台

    访问http://127.0.0.1:7510/,注意不是服务注册地址

    SpringCloud Eureka服务注册中心应用入门详解

    同一个服务不同端口,一个客户端打开了instance-id配置,显示的是真实IP。一个未打开显示的是localhost

    PS:目前这个配置已在线上跑过,未发现其他问题!

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。