跟我学Spring Cloud(Finchley版)-05-服务注册与服务发现-Eureka入门

本节讲解基于Eureka的服务发现。

Eureka简介

Eureka是Netflix开源的服务发现组件,本身是一个基于REST的服务,包含Server和Client两部分,Spring Cloud将它集成在子项目Spring Cloud Netflix中。

拓展阅读

  • Eureka的GitHub:https://github.com/Netflix/Eureka

  • Netflix是一家在线影片租赁提供商。

  • Eureka的典故:阿基米德发现浮力时,非常开心,于是说:“Eureka!”意思是“我找到了!”。Netflix将它们的服务发现组件命名为Eureka实在是非常形象。

理解跟我学Spring Cloud(Finchley版)-04-服务注册与服务发现-原理剖析 所讲的服务发现原理后,我们来编写基于Eureka的服务发现——首先编写一个Eureka Server,然后将前文的微服务都注册到Eureka Server上。

编写Eureka Server

  • 加依赖
 
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-server
    
 
  • 加注解
  @SpringBootApplication
  @EnableEurekaServer
  public class EurekaApplication {
     public static void main(String[] args) {
     SpringApplication.run(EurekaApplication.class, args);
   }
  }
  • 写配置
 server:
    port: 8761
 eureka:
    client:
      # 是否要注册到其他Eureka Server实例
      register-with-eureka: false
      # 是否要从其他Eureka Server实例获取数据
      fetch-registry: false
    service-url: 
      defaultZone: http://localhost:8761/eureka/

TIPS

这里,大家可先不去探究registerWithEureka 以及fetchRegistry 究竟是什么鬼,笔者将在下一节为大家揭晓。

将应用注册到Eureka Server上

  • 加依赖

   org.springframework.cloud
   spring-cloud-starter-netflix-eureka-client

  • 加注解
@SpringBootApplication
public class ProviderUserApplication {
  public static void main(String[] args) {
  SpringApplication.run(ProviderUserApplication.class, args);
  }
 }
**注意**:早期的版本(Dalston及更早版本)还需在启动类上添加注解 `@EnableDiscoveryClient`或`@EnableEurekaClient` ,从Edgware开始,该注解可省略。
  • 添加配置:
spring:
     application:
     # 指定注册到eureka server上的服务名称,对于电影微服务,本系列将名称设为microservice-consumer-movie
     name: microservice-provider-user
eureka:
     client:
       service-url:
       # 指定eureka server通信地址,注意/eureka/小尾巴不能少
         defaultZone: http://localhost:8761/eureka/
     instance:
       # 是否注册IP到eureka server,如不指定或设为false,那就会注册主机名到eureka server
       prefer-ip-address: true

测试

  • 依次启动Eureka Server以及用户微服务、电影微服务;

  • 访问http://localhost:8761 可观察到类似如下界面:

    Eureka Server-01
  • 将用户微服务停止,可看到Eureka Server首页变成类似如下界面:

    Eureka Server-02

配套代码

  • GitHub:

    • Eureka Server:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-discovery-eureka
    • microservice-provider-user:https://www.github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-provider-user
    • microservice-consumer-movie:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie
  • Gitee:

    • Eureka Server:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-discovery-eureka
    • microservice-provider-user:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-provider-user
    • microservice-consumer-movie:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie

相关文章

  • 跟我学Spring Cloud(Finchley版)番外-01-Eureka安全详解
  • 跟我学Spring Cloud(Finchley版)-06-服务注册与服务发现-Eureka深入
  • 跟我学Spring Cloud(Finchley版)-04-服务注册与服务发现-原理剖析
  • 关于Eureka 2.x,别再人云亦云了!
  • Spring Cloud Alibaba迁移指南1:零代码从Eureka迁移到Nacos

本文链接:跟我学Spring Cloud(Finchley版)-05-服务注册与服务发现-Eureka入门

转载声明:本博客由周立创作,采用 CC BY 3.0 CN 许可协议。可自由转载、引用,但需署名作者且注明文章出处。

你可能感兴趣的:(跟我学Spring Cloud(Finchley版)-05-服务注册与服务发现-Eureka入门)