Eureka原理实践

阶段 1:搭建 Eureka Server

  1. 创建一个 Spring Boot 项目:

    • 打开 IDE(如 IntelliJ IDEA 或 Eclipse),创建一个新的 Spring Boot 项目。

    • pom.xml 文件中添加 Eureka Server 依赖:

      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
          </dependency>
      </dependencies>
      

  2. 配置 Eureka Server:

    • 在项目的主类上添加 @EnableEurekaServer 注解,使其成为一个 Eureka 服务器:

      @SpringBootApplication
      @EnableEurekaServer
      public class EurekaServerApplication {
          public static void main(String[] args) {
              SpringApplication.run(EurekaServerApplication.class, args);
          }
      }
      
    • application.yml 中配置基本信息,如端口号和实例名称:

      server:
        port: 8761
      
      eureka:
        client:
          register-with-eureka: false
          fetch-registry: false
        instance:
          hostname: localhost
      
  3. 启动 Eureka Server:

    • 运行主类 EurekaServerApplication,访问 http://localhost:8761,你应该能看到一个 Eureka 的管理界面。

阶段 2:注册服务到 Eureka

  1. 创建服务提供者(微服务):

    • 创建一个新的 Spring Boot 项目,作为服务提供者(例如 Service-Provider)。

    • pom.xml 中添加 Eureka Client 依赖:

      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
          </dependency>
      </dependencies>
      
  2. 配置 Eureka Client:

    • 在服务提供者的主类上添加 @EnableEurekaClient 注解:

      @SpringBootApplication
      @EnableEurekaClient
      public class ServiceProviderApplication {
          public static void main(String[] args) {
              SpringApplication.run(ServiceProviderApplication.class, args);
          }
      }
      
    • application.yml 中配置 Eureka Server 地址及服务信息:

      server:
        port: 8081
      
      spring:
        application:
          name: service-provider
      
      eureka:
        client:
          service-url:
            defaultZone: http://localhost:8761/eureka/
        instance:
          instance-id: ${spring.application.name}:${server.port}
      

  3. 编写简单的服务接口:

    • 创建一个简单的 REST 控制器,供其他服务调用:

      @RestController
      public class GreetingController {
          @GetMapping("/greet")
          public String greet() {
              return "Hello from Service Provider!";
          }
      }
      
  4. 启动服务提供者:

    • 运行 ServiceProviderApplication,该服务将自动注册到 Eureka Server。
  5. 验证注册结果:

    • 访问 Eureka Server 管理界面,你应该能看到 service-provider 已经注册成功。

阶段 3:注册另一个客户端服务

  1. 创建服务消费者(客户端):

    • 创建一个新的 Spring Boot 项目,作为服务消费者(例如 Service-Consumer)。

    • pom.xml 中添加 Eureka Client 和 RestTemplate 依赖:

      <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>
      </dependencies>
      
  2. 配置 Eureka Client:

    • 在服务消费者的主类上添加 @EnableEurekaClient 注解:

      @SpringBootApplication
      @EnableEurekaClient
      public class ServiceConsumerApplication {
          public static void main(String[] args) {
              SpringApplication.run(ServiceConsumerApplication.class, args);
          }
      }
      

    • application.yml 中配置 Eureka Server 地址:

      server:
        port: 8082
      
      spring:
        application:
          name: service-consumer
      
      eureka:
        client:
          service-url:
            defaultZone: http://localhost:8761/eureka/
        instance:
          instance-id: ${spring.application.name}:${server.port}
      

  3. 创建一个调用服务提供者的接口:

    • 使用 RestTemplate 调用服务提供者的接口:

      @RestController
      public class ConsumerController {
          
          @Autowired
          private RestTemplate restTemplate;
      
          @GetMapping("/consume")
          public String consume() {
              return restTemplate.getForObject("http://service-provider/greet", String.class);
          }
      }
      
      @Configuration
      public class RestTemplateConfig {
          
          @Bean
          @LoadBalanced
          public RestTemplate restTemplate() {
              return new RestTemplate();
          }
      }
      
  4. 启动服务消费者:

    • 运行 ServiceConsumerApplication,并通过浏览器访问 http://localhost:8082/consume,你将看到来自 service-provider 的响应。

阶段 4:测试服务的注册、发现和故障恢复

  1. 服务下线测试:

    • 停止服务提供者(Service-Provider),观察 Eureka Server 上的状态变化。服务将从可用列表中移除。
  2. 自我保护模式测试:

    • 修改 Eureka Server 配置,模拟网络故障并观察自我保护模式:

      eureka:
        server:
          enable-self-preservation: true
      
  3. 多实例注册和负载均衡:

    • 启动多个 Service-Provider 实例(可以更改端口号来模拟),并观察 Eureka Server 中的注册情况。
    • 测试通过服务消费者访问时的负载均衡效果。

阶段 5:优化和监控

  1. 健康检查配置:

    • 在服务提供者中配置健康检查路径和策略,例如:

      eureka:
        instance:
          lease-renewal-interval-in-seconds: 10
          lease-expiration-duration-in-seconds: 30
          health-check-url: http://localhost:8081/actuator/health
      
  2. 监控和日志:

    • 集成 Spring Boot Actuator,监控服务的运行状态和 Eureka 的状态变化。
    • 在代码中增加日志记录,以便于调试和监控。
  3. 生产环境优化:

    • 在生产环境中,可以考虑配置 Eureka 的高可用性,通过集群方式部署多个 Eureka Server,以避免单点故障。

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愿时光不负.

爱意随风起,风止意难平。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值