java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringCloud Eureka服务注册中心

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

作者:有头发的程序猿!

这篇文章主要介绍了Spring Cloud Eureka服务注册中心入门流程分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

1.多节点无缝切换问题

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

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

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

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均配置时,如下图所示

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

启动类上加@EnableEurekaClient注解

3.4 控制台

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

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

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

到此这篇关于SpringCloud Eureka服务注册中心应用入门详解的文章就介绍到这了,更多相关SpringCloud Eureka服务注册中心内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文