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 父工程

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.github</groupId>
  7. <artifactId>hello-world</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <modules>
  10. <module>eurekaserver</module>
  11. <module>eurekaclient</module>
  12. </modules>
  13. <packaging>pom</packaging>
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>2.1.3.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20. <dependencyManagement>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-dependencies</artifactId>
  25. <version>Greenwich.RELEASE</version>
  26. <type>pom</type>
  27. <scope>import</scope>
  28. <exclusions>
  29. </exclusions>
  30. </dependency>
  31. </dependencies>
  32. </dependencyManagement>
  33. </project>

image.png

服务注册中心

image.png

创建module 名称是eurekaserver
image.png
设置pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.2.5.RELEASE</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.github.springcloud</groupId>
  13. <artifactId>eureka-server</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>eureka-server</name>
  16. <description>Demo project for Spring Boot</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <scope>test</scope>
  30. <exclusions>
  31. <exclusion>
  32. <groupId>org.junit.vintage</groupId>
  33. <artifactId>junit-vintage-engine</artifactId>
  34. </exclusion>
  35. </exclusions>
  36. </dependency>
  37. </dependencies>
  38. <dependencyManagement>
  39. <dependencies>
  40. <dependency>
  41. <groupId>org.springframework.cloud</groupId>
  42. <artifactId>spring-cloud-dependencies</artifactId>
  43. <version>${spring-cloud.version}</version>
  44. <type>pom</type>
  45. <scope>import</scope>
  46. </dependency>
  47. </dependencies>
  48. </dependencyManagement>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>

主要添加eureka-server依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  4. </dependency>

@SpringCloudApplication 外,这个类还增加了一个注解:EnableEurekaServer,这个注解的作用就是标注该应用程序是一个注册中心,

  1. package com.github.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer
  7. public class EurekaServerApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(EurekaServerApplication.class, args);
  10. }
  11. }

application.properties文件类型改成application.yml,个人比较喜欢yml格式的文件

  1. server:
  2. port: 8001 #指定运行端口
  3. spring:
  4. application:
  5. name: eureka-server #指定服务名称
  6. eureka:
  7. instance:
  8. hostname: localhost #指定主机地址
  9. client:
  10. fetch-registry: false #指定是否要从注册中心获取服务(注册中心不需要开启)
  11. register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)
  12. server:
  13. enable-self-preservation: false #关闭保护模式

启动项目,运行完成后访问地址http://localhost:8001/可以看到Eureka注册中心的界面
image.png

服务提供者

创建Module 子项目名称eurekaclient
image.png或者通过

图片.png

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.2.5.RELEASE</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.github.springcloud</groupId>
  13. <artifactId>eureka-client</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>eureka-client</name>
  16. <description>Demo project for Spring Boot</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. <exclusions>
  35. <exclusion>
  36. <groupId>org.junit.vintage</groupId>
  37. <artifactId>junit-vintage-engine</artifactId>
  38. </exclusion>
  39. </exclusions>
  40. </dependency>
  41. </dependencies>
  42. <dependencyManagement>
  43. <dependencies>
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-dependencies</artifactId>
  47. <version>${spring-cloud.version}</version>
  48. <type>pom</type>
  49. <scope>import</scope>
  50. </dependency>
  51. </dependencies>
  52. </dependencyManagement>
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. </plugin>
  59. </plugins>
  60. </build>
  61. </project>

主要需要添加

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>

在启动类上添加@EnableDiscoveryClient注解表明是一个Eureka客户端

  1. package com.github.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. @SpringBootApplication
  6. @EnableEurekaClient
  7. public class EurekaClientApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(EurekaClientApplication.class, args);
  10. }
  11. }

application.yml

  1. server:
  2. port: 8002 #运行端口号
  3. spring:
  4. application:
  5. name: eureka-client #服务名称
  6. eureka:
  7. client:
  8. register-with-eureka: true #注册到Eureka的注册中心
  9. fetch-registry: true #获取注册实例列表
  10. service-url:
  11. defaultZone: http://localhost:8001/eureka/ #配置注册中心地址

image.png

Eureka的常用配置

  1. eureka:
  2. client: #eureka客户端配置
  3. register-with-eureka: true #是否将自己注册到eureka服务端上去
  4. fetch-registry: true #是否获取eureka服务端上注册的服务列表
  5. service-url:
  6. defaultZone: http://localhost:8001/eureka/ # 指定注册中心地址
  7. enabled: true # 启用eureka客户端
  8. registry-fetch-interval-seconds: 30 #定义去eureka服务端获取服务列表的时间间隔
  9. instance: #eureka客户端实例配置
  10. lease-renewal-interval-in-seconds: 30 #定义服务多久去注册中心续约
  11. lease-expiration-duration-in-seconds: 90 #定义服务多久不去续约认为服务失效
  12. metadata-map:
  13. zone: jiangsu #所在区域
  14. hostname: localhost #服务主机名称
  15. prefer-ip-address: false #是否优先使用ip来作为主机名
  16. server: #eureka服务端配置
  17. enable-self-preservation: false #关闭eureka服务端的保护机制

注册中心添加认证

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>springcloud-learning</artifactId>
  7. <groupId>org.example.cloud</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>eureka-security-server</artifactId>
  12. <version>1.0.0</version>
  13. <properties>
  14. <maven.compiler.source>8</maven.compiler.source>
  15. <maven.compiler.target>8</maven.compiler.target>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-security</artifactId>
  25. </dependency>
  26. </dependencies>
  27. <build>
  28. <finalName>${project.artifactId}</finalName>
  29. <plugins>
  30. <plugin>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-maven-plugin</artifactId>
  33. <version>2.3.7.RELEASE</version>
  34. <executions>
  35. <execution>
  36. <goals>
  37. <goal>repackage</goal>
  38. </goals>
  39. </execution>
  40. </executions>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </project>

去掉CSRF

  1. package com.exapmle.cloud.eureka.config;
  2. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  3. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. /**
  6. * @Description: * 默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,
  7. * * Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token
  8. * @Author: baxiang
  9. * @Date: 2021/9/25.
  10. */
  11. @EnableWebSecurity
  12. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  13. @Override
  14. protected void configure(HttpSecurity http) throws Exception {
  15. http.csrf().ignoringAntMatchers("/eureka/**");
  16. super.configure(http);
  17. }
  18. }

添加配置

  1. server:
  2. port: 8004
  3. spring:
  4. application:
  5. name: eureka-security-server
  6. security: #配置SpringSecurity登录用户名和密码
  7. user:
  8. name: admin
  9. password: 123456
  10. eureka:
  11. instance:
  12. hostname: localhost
  13. client:
  14. fetch-registry: false
  15. register-with-eureka: false

运行eureka-security-server,访问http://localhost:8004发现需要登录认证
图片.png
eureka-client注册到有登录认证的注册中心配置文件中需要修改注册中心地址格式

  1. http://${username}:${password}@${hostname}:${port}/eureka/

例如

  1. server:
  2. port: 8101 #运行端口号
  3. spring:
  4. application:
  5. name: eureka-client #服务名称
  6. eureka:
  7. client:
  8. register-with-eureka: true #注册到Eureka的注册中心
  9. fetch-registry: true #获取注册实例列表
  10. service-url:
  11. #defaultZone: http://localhost:8001/eureka/ #配置注册中心地址
  12. defaultZone: http://admin:123456@localhost:8004/eureka/ #配置认证注册中心地址

源码编译

https://github.com/Netflix/eureka
下载源码

  1. git clone https://github.com/Netflix/eureka.git
  2. cd eureka
  3. git checkout v1.7.x

使用1.7.x的分支
image.png

加速编译速度 在build.gradle 增加阿里云镜像

  1. repositories {
  2. maven { url 'https://maven.aliyun.com/repository/public/' }
  3. maven { url 'https://maven.aliyun.com/repository/jcenter'}
  4. jcenter()
  5. }

image.png