6

Spring Cloud之Finchley版学习(五)-服务注册与服务发现-Eureka学习

 3 years ago
source link: https://www.wencst.com/archives/1177
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Spring Cloud之Finchley版学习(五)-服务注册与服务发现-Eureka学习

作者: wencst 分类: JAVA,微服务,架构设计 发布时间: 2019-01-02 11:42 阅读: 1,368 次

本节讲解基于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

  • 加依赖
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>
    </dependencies>
    • 加注解
      @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上

    • 加依赖
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
    • 加注解
      @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

如果文章对您有用,扫一下支付宝的红包,不胜感激!

欢迎加入QQ群进行技术交流:656897351(各种技术、招聘、兼职、培训欢迎加入)

Leave a Reply Cancel reply

You must be logged in to post a comment.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK