首页后端开发其他后端知识新手如何使用SpringCloud Eureka服务注册中心

新手如何使用SpringCloud Eureka服务注册中心

时间2024-03-24 23:16:03发布访客分类其他后端知识浏览840
导读:这篇文章主要为大家详细介绍了新手如何使用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);

    }

}
    

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

ListApplication>
     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服务注册中心”已经有了进一步的了解,更多相关的问题,欢迎关注网络或到官网咨询客服。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 新手如何使用SpringCloud Eureka服务注册中心
本文地址: https://pptw.com/jishu/652383.html
Oracle锁表查询怎样做?如何进行解锁? 浅谈Oracle数据库操作、表操作、数据操作

游客 回复需填写必要信息