-
多节点无缝切换问题
-
分布式节点中的服务宕机或者重启不影响客户端使用
- 分布式节点中的服务宕机重启不影响业务服务内部通信
如果在某个分布式系统中想要解决上述问题,那么这篇文章就是精华之处。
回顾一下以前的常用手段:
- 单节点运行,其他节点备用,无法无缝连接,内网通信无法保证
- 多节点运行,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.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.8version>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>2021.0.3version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
3.2 eureka服务端
- eureka服务端依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
- yml配置
server:
port: 7510
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: false
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 -> {
log.info("{} -- {}", info.getAppName(), info.getInstanceId());
});
});
3.3 客户端
- pom依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
- yml配置
server:
port: 7530
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7510/eureka/
instance:
prefer-ip-address: true
lease-renewal-interval-in-seconds: 3
lease-expiration-duration-in-seconds: 5
spring:
application:
name: order-service
jackson:
default-property-inclusion: non_null
time-zone: Asia/Shanghai
cloud:
inetutils:
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:目前这个配置已在线上跑过,未发现其他问题!
以上就是本章的全部内容了。
上一篇:JVM第三话 – JVM性能调优实战和高频面试题记录
下一篇:SpringCloud第二话 – 基于Nacos实现配置中心
少年易学老难成,一寸光阴不可轻
Original: https://blog.csdn.net/qq_35551875/article/details/125605884
Author: 有头发的程序猿!
Title: SpringCloud第一话 -- Eureka服务注册中心