Eureka是SpringCloud中的一个负责服务注册与发现的组件。Eureka中分为Server和Client:

    • Server是服务的注册与发现中心,Server端无需向注册中心注册,因为Server本身就是注册中心
    • Client既可以作为服务的生产者,又可以作为服务的消费者。生产者Client和消费者Client都必须向注册中心注册

    Eureka结构如下图:
    1635423824790-21b04da1-8f98-4a34-954c-a3a11eb472bb2.jpg :::info 服务中心Server:8761 ::: 添加spring-cloud-starter-netflix-eureka-server依赖

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>2.0.6.RELEASE</version>
    5. <relativePath/> <!-- lookup parent from repository -->
    6. </parent>
    7. <properties>
    8. <java.version>1.8</java.version>
    9. <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    10. </properties>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.cloud</groupId>
    14. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    15. </dependency>
    16. <dependency>
    17. <groupId>org.springframework.boot</groupId>
    18. <artifactId>spring-boot-starter-security</artifactId>
    19. </dependency>
    20. <dependency>
    21. <groupId>org.projectlombok</groupId>
    22. <artifactId>lombok</artifactId>
    23. <optional>true</optional>
    24. </dependency>
    25. <dependency>
    26. <groupId>org.springframework.boot</groupId>
    27. <artifactId>spring-boot-starter-test</artifactId>
    28. <scope>test</scope>
    29. <exclusions>
    30. <exclusion>
    31. <groupId>org.junit.vintage</groupId>
    32. <artifactId>junit-vintage-engine</artifactId>
    33. </exclusion>
    34. </exclusions>
    35. </dependency>
    36. </dependencies>
    37. <dependencyManagement>
    38. <dependencies>
    39. <dependency>
    40. <groupId>org.springframework.cloud</groupId>
    41. <artifactId>spring-cloud-dependencies</artifactId>
    42. <version>${spring-cloud.version}</version>
    43. <type>pom</type>
    44. <scope>import</scope>
    45. </dependency>
    46. </dependencies>
    47. </dependencyManagement>

    在SpringBoot启动类上添加注解@EnableEurekaServer

    1. @SpringBootApplication
    2. //开启服务治理功能
    3. @EnableEurekaServer
    4. public class EurekaServerApplication {
    5. public static void main(String[] args) {
    6. SpringApplication.run(EurekaServerApplication.class, args);
    7. }
    8. }

    集成Security框架,实现Eureka密码登录

    1. @EnableWebSecurity
    2. @Configuration
    3. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    4. @Override
    5. protected void configure(HttpSecurity http) throws Exception {
    6. http.csrf().disable();
    7. http.authorizeRequests()
    8. .anyRequest()
    9. .authenticated()
    10. .and()
    11. .httpBasic();
    12. }
    13. }

    配置文件:

    1. spring:
    2. application:
    3. # 服务名
    4. name: eureka-server
    5. # security安全认证配置
    6. security:
    7. user:
    8. name: yangle
    9. password: 123
    10. server:
    11. port: 8761
    12. eureka:
    13. client:
    14. # 该应用为注册中心,不需要向注册中心注册自己
    15. register-with-eureka: false
    16. # 关闭检索服务的功能,只需要维护服务
    17. fetch-registry: false

    项目启动后,访问http://localhost:8761/,输入用户名和密码 yangle 123
    目前我们还没有创建服务,因此下图Application栏目没有服务实例
    7870e944-49e0-4e78-a831-ba38271cfefe.jpg :::info 生产者客户端:8081 ::: 生产者会向服务中心注册自己的服务并周期性的发送心跳

    • 心跳间隔默认30s:eureka.instance.lease-renewal-interval-in-seconds=5
    • 心跳超时时间默认90s:eureka.instance.lease-expiration-duration-in-seconds=10

    同时生产者也会周期性的从服务中心获取服务清单信息缓存到本地

    • 是否从eureka获取服务清单:fetch-registry: true
    • 向eureka服务端更新自己实例信息的间隔时间s:instance-info-replication-interval-seconds: 15
    • 从eureka客户端获得服务清单的间隔时间s:registry-fetch-interval-seconds: 15

    添加spring-cloud-starter-netflix-eureka-client依赖:

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>2.0.6.RELEASE</version>
    5. <relativePath/> <!-- lookup parent from repository -->
    6. </parent>
    7. <properties>
    8. <java.version>1.8</java.version>
    9. <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    10. </properties>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.cloud</groupId>
    14. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    15. </dependency>
    16. <dependency>
    17. <groupId>org.springframework.boot</groupId>
    18. <artifactId>spring-boot-starter-web</artifactId>
    19. </dependency>
    20. <dependency>
    21. <groupId>org.springframework.boot</groupId>
    22. <artifactId>spring-boot-starter-test</artifactId>
    23. <scope>test</scope>
    24. <exclusions>
    25. <exclusion>
    26. <groupId>org.junit.vintage</groupId>
    27. <artifactId>junit-vintage-engine</artifactId>
    28. </exclusion>
    29. </exclusions>
    30. </dependency>
    31. </dependencies>
    32. <dependencyManagement>
    33. <dependencies>
    34. <dependency>
    35. <groupId>org.springframework.cloud</groupId>
    36. <artifactId>spring-cloud-dependencies</artifactId>
    37. <version>${spring-cloud.version}</version>
    38. <type>pom</type>
    39. <scope>import</scope>
    40. </dependency>
    41. </dependencies>
    42. </dependencyManagement>

    然后在启动类中添加注解@EnableEurekaClient

    1. @SpringBootApplication
    2. //开启服务发现功能
    3. @EnableDiscoveryClient
    4. public class EurekaClientProducterApplication {
    5. public static void main(String[] args) {
    6. SpringApplication.run(EurekaClientProducterApplication.class, args);
    7. }
    8. }

    添加一个订单服务

    1. @RestController
    2. public class OrderService {
    3. @RequestMapping("getOrder")
    4. public String getOrder(){
    5. return "{code:0,data:{}}";
    6. }
    7. }

    在配置文件中添加配置信息:

    1. spring:
    2. application:
    3. name: eureka-client-order-service
    4. server:
    5. port: 8081
    6. eureka:
    7. client:
    8. serviceUrl:
    9. # 指定注册中心
    10. defaultZone: http://yangle:123@localhost:8761/eureka
    11. instance:
    12. #可选
    13. preferIpAddress: true
    14. #实例ID支持自定义,可选
    15. instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

    我们重新打开http://localhost:8761/,会看到生产者已经注册到注册中心里面了
    daf75b64-be52-433c-9726-f97f461bac59.jpg :::info 消费者客户端:8082 ::: 将restTemplate注入到容器中,SpringRestTemplate是Spring提供的用于访问Rest服务的客端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率,所以很多客户端比如Android或者第三方服务商都是使用RestTemplate请求restful服务

    1. @SpringBootApplication
    2. public class EurekaClientConsumerApplication {
    3. public static void main(String[] args) {
    4. SpringApplication.run(EurekaClientConsumerApplication.class, args);
    5. }
    6. @Bean(name = "restTemplate")
    7. public RestTemplate getRestTemplate() {
    8. return new RestTemplate();
    9. }
    10. @Bean(name = "restTemplate2")
    11. @LoadBalanced//开启负载均衡,需要使用IDEA复制多份消费者
    12. public RestTemplate getRestTemplate2() {
    13. return new RestTemplate();
    14. }
    15. }

    添加一个接口去调用生产者提供的服务

    1. @RestController
    2. public class OrderConsumerService {
    3. @Autowired
    4. @Qualifier("restTemplate")
    5. private RestTemplate restTemplate;
    6. @Autowired
    7. @Qualifier("restTemplate2")
    8. private RestTemplate restTemplate2;
    9. @RequestMapping("getOrder")
    10. public String getOrder(){
    11. return restTemplate.getForObject("http://localhost:8081/getOrder",String.class);
    12. }
    13. //实现负载均衡的服务,不需要指定服务端口,只需要指定使用的服务名称
    14. @RequestMapping("getOrderForLoadBalence")
    15. public String getOrderForLoadBalence(){
    16. return restTemplate2.getForObject("http://eureka-client-order-service/getOrder",String.class);
    17. }
    18. }

    配置文件中添加配置信息:

    1. spring:
    2. application:
    3. name: eureka-client-order-consumer-service
    4. server:
    5. port: 8082
    6. eureka:
    7. client:
    8. serviceUrl:
    9. defaultZone: http://yangle:123@localhost:8761/eureka
    10. instance:
    11. preferIpAddress: true
    12. instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

    查看eureka界面http://localhost:8761/,看到消费者也注册进来了
    53a055ff-5079-4802-bc20-915d6e0d5dfb.jpg :::info 高可用注册中心 ::: 服务注册中心集群方式部署,当其中某个注册中心分片故障后,Eureka就会转入自我保护模式,允许在故障期间继续进行服务注册与发现;
    等到故障分片恢复时,集群其他分片会把它们的状态同步给故障分片;
    集群中的分片会以异步的方式互相复制各自的状态;
    复制eureka-server项目,更名为eureka-server-slave,eureka-server-slave的配置文件如下:

    1. spring:
    2. application:
    3. name: eureka-server
    4. security:
    5. user:
    6. name: yangle
    7. password: 123
    8. server:
    9. port: 8762
    10. eureka:
    11. client:
    12. serviceUrl:
    13. defaultZone: http://yangle:123@localhost:8761/eureka

    修改eureka-server的配置文件

    1. spring:
    2. application:
    3. name: eureka-server
    4. security:
    5. user:
    6. name: yangle
    7. password: 123
    8. server:
    9. port: 8761
    10. eureka:
    11. client:
    12. serviceUrl:
    13. defaultZone: http://yangle:123@localhost:8762/eureka

    修改producter的配置文件

    1. spring:
    2. application:
    3. name: eureka-client-order-service
    4. server:
    5. port: 8081
    6. eureka:
    7. client:
    8. serviceUrl:
    9. defaultZone: http://yangle:123@localhost:8761/eureka,http://yangle:123@localhost:8762/eureka
    10. instance:
    11. preferIpAddress: true
    12. instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

    修改consumer的配置文件

    1. spring:
    2. application:
    3. name: eureka-client-order-consumer-service
    4. server:
    5. port: 8082
    6. eureka:
    7. client:
    8. serviceUrl:
    9. defaultZone: http://yangle:123@localhost:8761/eureka,http://yangle:123@localhost:8762/eureka
    10. instance:
    11. preferIpAddress: true
    12. instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}