Eureka组件是Spring Cloud Netflix 中服务与发现模块。
Eureka的GitHub:https://github.com/Netflix/Eureka
Eureka Server
服务端-没有存储,内存保持,每服务实例需要发送心跳去续约。客户端-在内存中缓存着eureka的注册信息,因此不必每请求到eureka查找服务 - eureka之间会做注册服务同步,从而保证状态一致,客户端只需访问一个eureka
Service Provider
会向Eureka Server做Register(服务注册)、Renew(服务续约)、Cancel(服务下线)等操作
Service Consumer
会向Eureka Server获取注册服务列表,并消费服务
创建mvn 父工程
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.github</groupId><artifactId>hello-world</artifactId><version>1.0-SNAPSHOT</version><modules><module>eurekaserver</module><module>eurekaclient</module></modules><packaging>pom</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Greenwich.RELEASE</version><type>pom</type><scope>import</scope><exclusions></exclusions></dependency></dependencies></dependencyManagement></project>
服务注册中心

创建module 名称是eurekaserver
设置pom
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.github.springcloud</groupId><artifactId>eureka-server</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-server</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><spring-cloud.version>Hoxton.SR3</spring-cloud.version></properties><dependencies><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-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
主要添加eureka-server依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>
@SpringCloudApplication 外,这个类还增加了一个注解:EnableEurekaServer,这个注解的作用就是标注该应用程序是一个注册中心,
package com.github.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}
application.properties文件类型改成application.yml,个人比较喜欢yml格式的文件
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注册中心的界面
服务提供者
创建Module 子项目名称eurekaclient
或者通过

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.github.springcloud</groupId><artifactId>eureka-client</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-client</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><spring-cloud.version>Hoxton.SR3</spring-cloud.version></properties><dependencies><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
主要需要添加
<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客户端
package com.github.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class EurekaClientApplication {public static void main(String[] args) {SpringApplication.run(EurekaClientApplication.class, args);}}
application.yml
server:port: 8002 #运行端口号spring:application:name: eureka-client #服务名称eureka:client:register-with-eureka: true #注册到Eureka的注册中心fetch-registry: true #获取注册实例列表service-url:defaultZone: http://localhost:8001/eureka/ #配置注册中心地址
Eureka的常用配置
eureka:client: #eureka客户端配置register-with-eureka: true #是否将自己注册到eureka服务端上去fetch-registry: true #是否获取eureka服务端上注册的服务列表service-url:defaultZone: http://localhost:8001/eureka/ # 指定注册中心地址enabled: true # 启用eureka客户端registry-fetch-interval-seconds: 30 #定义去eureka服务端获取服务列表的时间间隔instance: #eureka客户端实例配置lease-renewal-interval-in-seconds: 30 #定义服务多久去注册中心续约lease-expiration-duration-in-seconds: 90 #定义服务多久不去续约认为服务失效metadata-map:zone: jiangsu #所在区域hostname: localhost #服务主机名称prefer-ip-address: false #是否优先使用ip来作为主机名server: #eureka服务端配置enable-self-preservation: false #关闭eureka服务端的保护机制
注册中心添加认证
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud-learning</artifactId><groupId>org.example.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka-security-server</artifactId><version>1.0.0</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><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></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.3.7.RELEASE</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
去掉CSRF
�
package com.exapmle.cloud.eureka.config;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** @Description: * 默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,* * Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。* @Author: baxiang* @Date: 2021/9/25.*/@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().ignoringAntMatchers("/eureka/**");super.configure(http);}}
添加配置
server:port: 8004spring:application:name: eureka-security-serversecurity: #配置SpringSecurity登录用户名和密码user:name: adminpassword: 123456eureka:instance:hostname: localhostclient:fetch-registry: falseregister-with-eureka: false
运行eureka-security-server,访问http://localhost:8004发现需要登录认证
eureka-client注册到有登录认证的注册中心配置文件中需要修改注册中心地址格式
http://${username}:${password}@${hostname}:${port}/eureka/
例如
server:port: 8101 #运行端口号spring:application:name: eureka-client #服务名称eureka:client:register-with-eureka: true #注册到Eureka的注册中心fetch-registry: true #获取注册实例列表service-url:#defaultZone: http://localhost:8001/eureka/ #配置注册中心地址defaultZone: http://admin:123456@localhost:8004/eureka/ #配置认证注册中心地址
源码编译
https://github.com/Netflix/eureka
下载源码
git clone https://github.com/Netflix/eureka.gitcd eurekagit checkout v1.7.x
使用1.7.x的分支
加速编译速度 在build.gradle 增加阿里云镜像
repositories {maven { url 'https://maven.aliyun.com/repository/public/' }maven { url 'https://maven.aliyun.com/repository/jcenter'}jcenter()}

