Eureka 简介
Spring Cloud Eureka 是 Spring Cloud Netflix 子项目的核心组件之一,主要用于微服务架构中的服务治理。 本文将对搭建 Eureka 注册中心,搭建 Eureka 客户端,搭建 Eureka 集群及给 Eureka 注册中心添加登录认证进行介绍。
在微服务架构中往往会有一个注册中心,每个微服务都会向注册中心去注册自己的地址及端口信息,注册中心维护着服务名称与服务实例的对应关系。每个微服务都会定时从注册中心获取服务列表,同时汇报自己的运行情况,这样当有的服务需要调用其他服务时,就可以从自己获取到的服务列表中获取实例地址进行调用,Eureka实现了这套服务注册与发现机制。
搭建 Eureka 注册中心
这里我们以创建并运行 Eureka 注册中心来看看在 IDEA 中创建并运行 Spring Cloud 应用的正确姿势。
创建一个 eureka-server 模块,并使用 Spring Initializer 初始化一个 Spring Boot 项目

填写应用信息

选择需要的 Spring Cloud Eureka Server 组件进行创建

引入依赖
在 pom.xml 中引入 spring-cloud-starter-netflix-eureka-server
依赖
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
|
相关配置
在启动类上添加 @EnableEurekaServer
注解来启用 Euerka 注册中心功能
1 2 3 4 5 6 7 8 9
| @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication {
public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }
}
|
在配置文件 application.yml
中添加 Eureka 注册中心的配置
1 2 3 4 5 6 7 8 9 10 11 12 13
| server: port: 8001 spring: application: name: eureka-server eureka: instance: hostname: localhost client: fetch-registry: false register-with-eureka: false server: enable-self-preservation: false
|
运行完成后访问地址 http://localhost:8001/ 可以看到Eureka注册中心的界面

搭建 Eureka 客户端
#### 引入依赖
新建一个 eureka-client 模块,并在 pom.xml 中添加如下依赖
1 2 3 4 5 6 7 8 9
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
|
相关配置
在启动类上添加 @EnableDiscoveryClient
注解表明是一个 Eureka 客户端
1 2 3 4 5 6 7 8 9
| @EnableDiscoveryClient @SpringBootApplication public class EurekaClientApplication {
public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); }
}
|
在配置文件 application.yml 中添加 Eureka 客户端的配置
1 2 3 4 5 6 7 8 9 10 11
| server: port: 8101 spring: application: name: eureka-client eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8001/eureka/
|
运行 eureka-client

查看注册中心 http://localhost:8001/ 发现Eureka客户端已经成功注册

搭建 Eureka 注册中心集群
由于所有服务都会注册到注册中心去,服务之间的调用都是通过从注册中心获取的服务列表来调用,注册中心一旦宕机,所有服务调用都会出现问题。所以我们需要多个注册中心组成集群来提供服务,下面将搭建一个双节点的注册中心集群。
相关配置
给 eureka-sever 添加配置文件 application-replica1.yml 配置第一个注册中心
1 2 3 4 5 6 7 8 9 10 11 12 13
| server: port: 8002 spring: application: name: eureka-server eureka: instance: hostname: replica1 client: serviceUrl: defaultZone: http://replica2:8003/eureka/ fetch-registry: true register-with-eureka: true
|
给 eureka-sever 添加配置文件 application-replica2.yml 配置第二个注册中心
1 2 3 4 5 6 7 8 9 10 11 12 13
| server: port: 8003 spring: application: name: eureka-server eureka: instance: hostname: replica2 client: serviceUrl: defaultZone: http://replica1:8002/eureka/ fetch-registry: true register-with-eureka: true
|
这里我们通过两个注册中心互相注册,搭建了注册中心的双节点集群,由于defaultZone使用了域名,所以还需在本机的host文件中配置一下。
修改本地host文件
1 2
| 127.0.0.1 replica1 127.0.0.1 replica2
|
运行 Eureka 注册中心集群
在 IDEA 中我们可以通过使用不同的配置文件来启动同一个 Spring Boot 应用。
添加两个配置,分别以 application-replica1.yml 和 application-replica2.yml 来启动 eureka-server,从原启动配置中复制一个出来

配置启动的配置文件

启动两个eureka-server,访问其中一个注册中心 http://replica1:8002/ 发现另一个已经成为其备份

修改Eureka-client,让其连接到集群。添加eureka-client的配置文件application-replica.yml,让其同时注册到两个注册中心。
1 2 3 4 5 6 7 8 9 10 11
| server: port: 8102 spring: application: name: eureka-client eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://replica1:8002/eureka/,http://replica2:8003/eureka/
|
以该配置文件启动后访问任意一个注册中心节点都可以看到eureka-client

给 Eureka 注册中心添加认证
#### 引入依赖
需要添加 SpringSecurity 依赖
1 2 3 4 5 6 7 8 9
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
|
相关配置
主要是配置了登录注册中心的用户名和密码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| server: port: 8004 spring: application: name: eureka-security-server security: user: name: antonio password: 123456 eureka: instance: hostname: localhost client: fetch-registry: false register-with-eureka: false
|
添加 Java 配置 WebSecurityConfig。默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。
1 2 3 4 5 6 7 8 9
| @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) throws Exception { http.csrf().ignoringAntMatchers("/eureka/**"); super.configure(http); } }
|
运行eureka-security-server,访问 http://localhost:8004 发现需要登录认证

eureka-client 注册到有登录认证的注册中心
配置文件中需要修改注册中心地址格式
1
| http://${username}:${password}@${hostname}:${port}/eureka/
|
添加 application-security.yml 配置文件,按格式修改用户名和密码
1 2 3 4 5 6 7 8 9 10 11
| server: port: 8103 spring: application: name: eureka-client eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://antonio:123456@localhost:8004/eureka/
|
以 application-security.yml 配置运行 eureka-client,可以在注册中心界面看到 eureka-client 已经成功注册

Eureka的常用配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8001/eureka/ enabled: true registry-fetch-interval-seconds: 30 instance: lease-renewal-interval-in-seconds: 30 lease-expiration-duration-in-seconds: 90 metadata-map: zone: jiangsu hostname: localhost prefer-ip-address: false server: enable-self-preservation: false
|
更多干货请移步:https://antoniopeng.com
如果你喜欢这个博客或发现它对你有用,欢迎你点击右下角 “OPEN CHAT” 进行评论。也欢迎你分享这个博客,让更多的人参与进来。如果在博客中的内容侵犯了您的版权,请联系博主删除它们。谢谢你!