首页 文章资讯内容详情

SpringCloud Hoxton.SR3 服务注册中心 Eureka

2026-06-01 2 花语

本文内容纲要:

服务注册中心Eureka

Eureka,是SpringCloudNetflix组件之一。包含服务治理(Eureka)包括服务注册、服务发现和服务检测监控等。

在SpringCloudNetflix整合技术栈中,Eureka既可以作为服务注册中心也可以用于服务发现对整个微服务架构起着最核心的整合作用。

那啥是服务注册中心?

举个例子:某某洗浴中心则作为服务注册中心。

那啥是服务注册?

举个例子:某某洗浴中心有N个PLMM来上班将信息登记到上面去

那啥是服务发现?

举个例子:某男子来到了洗浴中心翻了翻了人员信息找到了某项洗浴服务选了一位对应的PLMM,然后就干了些该干的事情然后就走了。

在微服务架构中,由于每一个服务的粒度相对传统SOA来说要小的多,所以服务的数量会成倍增加。这时如果有效管理服务的注册信息就尤为重要。

在分布式系统中,我们不仅仅是需要在注册中心找到服务和服务地址的映射关系这么简单,我们还需要考虑更多更复杂的问题:

服务注册后,如何被及时发现 服务宕机后,如何及时下线 服务如何有效的水平扩展 服务发现时,如何进行路由 服务异常时,如何进行降级 注册中心如何实现自身的高可用

这些问题解决都依赖服务注册中心。

注册中心是微服务架构非常重要的一个组件,管理各种服务功能包括服务的注册、发现、熔断、负载、降级等。可以说是微服务架构中的”通讯录“,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用。 工程版本信息 SpringCloudHoxton.SR3 SpringBoot2.2.1.RELEASE java1.8 父工程依赖管理 <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR3</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> SpringCloudEureka服务注册中心搭建

创建名称为eureka-server的子工程pom信息如下:

<dependencies> <!--Web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--eureka服务端依赖jar包--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>

创建SpringBoot启动类EurekaServerApplication内容如下:

/** *Eureka服务端 *author:SimpleWu */ @EnableEurekaServer @SpringBootApplication publicclassEurekaServerApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(EurekaServerApplication.class,args); } }

创建application.properties内容如下:

spring.application.name=eureka-server server.port=23001 #是否向服务注册中心注册自己 eureka.client.register-with-eureka=false #是否检索服务 eureka.client.fetch-registry=false #eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@localhost:${server.port}/eureka eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka

到了这里直接启动EurekaServerApplicationmain方法。

启动成功后可以访问:http://localhost:23001/就能够查看到EurekaWeb界面。 Eureka增加安全认证

增加依赖:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>

增加properties配置:

#springsecurity用户密码 spring.security.user.name=root spring.security.user.password=123456

关闭csrf:

/** *关闭csrf *@authorSimpleWu */ @EnableWebSecurity @Configuration publicclassWebSecurityConfigextendsWebSecurityConfigurerAdapter{ @Override protectedvoidconfigure(HttpSecurityhttp)throwsException{ http.csrf().disable(); //http.authorizeRequests().anyRequest().permitAll().and().logout().permitAll(); super.configure(http); } }

然后我们重新访问的时候就需要输入账号与密码才能够放文档Eureka服务。

客户端连接Eureka需要携带账号密码properties配置可以使用 eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@localhost:${server.port}/eureka Eureka集群模式

3个服务组成一组通用的高可用服务分别为23001端口,23002端口,23003端口

配置Host三个实例服务映射:

127.0.0.1eureka001

127.0.0.1eureka002

127.0.0.1eureka003

然后分别启动3个Eureka服务,配置分别如下:

eureka001: server.port=23001 eureka.instance.hostname=eureka001 eureka.client.service-url.defaultZone=http://eureka002:23002/eureka,http://eureka003:23003/eureka

eureka002:

server.port=23002 eureka.instance.hostname=eureka002 eureka.client.service-url.defaultZone=http://eureka001:23001/eureka,http://eureka003:23003/eureka

eureka003:

server.port=23003 eureka.instance.hostname=eureka003 eureka.client.service-url.defaultZone=http://eureka001:23001/eureka,http://eureka003:23003/eureka

客户端(服务提供方配置):

eureka.client.service-url.defaultZone=http://eureka001:23001/eureka,http://eureka002:23002/eureka,http://eureka003:23003/eureka

到这里我们Eureka高可用集群就搭建成功了,可以客户端只向Eureka001注册服务,euereka002,eureka003中也会同步到euerka001的服务节点数据。

Eureka监听事件

EurekaInstanceCanceledEvent服务下线事件

EurekaInstanceRegisteredEvent服务注册事件

EurekaInstanceRenewedEvent服务续约事件

EurekaRegistryAvailableEventEureka注册中心启动事件

EurekaServerStartedEventEurekaServer启动事件

只需要在方法上标注@EventListener注解就可以监听到以上事件案例如下:

@EventListener publicvoidlisten(EurekaInstanceCanceledEventevent){ logger.info("服务{}已下线",event.getAppName()); logger.info("server地址信息{}",event.getServerId()); //邮箱通知给管理员发送警告 }

本文内容总结:

原文链接:https://www.cnblogs.com/SimpleWu/p/11177200.html