温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringCloud Eureka服务注册中心应用入门实例分析

发布时间:2022-07-12 14:27:15 来源:亿速云 阅读:150 作者:iii 栏目:开发技术

这篇文章主要介绍“SpringCloud Eureka服务注册中心应用入门实例分析”,在日常操作中,相信很多人在SpringCloud Eureka服务注册中心应用入门实例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringCloud Eureka服务注册中心应用入门实例分析”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    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:目前这个配置已在线上跑过,未发现其他问题!

    到此,关于“SpringCloud Eureka服务注册中心应用入门实例分析”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI