《深入理解Spring Cloud与微服务构建》学习笔记(八)~服务注册和发现 Eureka

        Eureka 是一个用户服务注册和发现的组件,和Consul、Zookeeper类似。Eureka分为Eureka Server服务注册中心,Eureka Client客户端。
        Eureka 是Spring Cloud 首选推荐的服务注册与发现组件,与 Spring Cloud 其他组件 可以无缝对接。 Eureka 和其他组件,比如负载均衡组件 Ribbon 、熔断器组件 Hystrix、熔断器监控组 Hystrix Dashboard 组件、熔断器聚合监控 Turbine 组件,以及网关 Zuul 组件相互配合, 能够很容易实现服务注册、负载均衡、熔断和智能路由等功能。 这些组件都是由 Netflix 公司开源的, 一起被称为 Netflix OSS 组件, Netflix OSS 组件由 Spring Cloud 整合为 Spring Cloud Netflix 组件, 它是 Spring Cloud 构架微服务的核心组件,也是基础组件。

一、Eureka 的基本架构

       Eureka 的基本架构,其中主要包括以下 种角色。
       Register Service :服务注册中心,它是一个 Eureka Server ,提供服务注册和发现的功能。
       Provider Service :服务提供者,它是 Eureka Client ,提供服务
       Consumer Service :服务消费者,它是 Eureka Cient ,消费服务
        服务消费的基本过程如下:首先前要一个服务注册中心 Eureka Server ,服务提供 Eureka Client 向服务注册中心 Eureka Server 注册,将自己的信息(比如服务名和服务的 IP 地址等) 通过REST API的形式提交给服务注册中心 Eureka Server 。同样 ,服务消费 Eureka Client 向服务注册中心 Eureka Server 注册,同时服务消费者获取一份服务注册列表的信息 该列表包含了所有向服务注册中心 Eureka Server 注册的服务信息。获取服务注册列表信息之后,服务消费者就知道服务提供者的 IP 地址,可以通过 Http 远程调度来消费服务提供者的服务。

二、编写EurekaServer
由于Eureka需要多个工程,根据教程,这里也采用Maven多Module结构构件项目。

  1. 首先创建一个空的Maven主工程,在主工程pom.xml里引入Eureka项目所需要的共同的依赖包,如:spring-boot-starter-parent、spring-cloud-dependencies 指定jdk版本为1.8,使用utf-8编码:
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.17.RELEASE</version>
            <relativePath/>
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF 8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>l.8</java.version>
            <spring-cloud-version>Dalston.SRl</spring-cloud-version>
            </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Edgware.SR3</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

    创建完主Maven工程,并且配置完pom之后,创建一个Module工程。

  2. 采用Spring Initializr的方式构件一个Module工程,命名为 eureka_server,作为注册中心Eureka Server的工程,如:
    在新建项目的pom.xml中自动继承引入主工程的依赖,还需要引入spring-cloud-starter-eureka-server依赖,如下:
     

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.17.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>1.5.17.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
                <version>1.4.6.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

    这里需要注意配置 spring-boot-starter-parent 和主工程的spring-boot-starter-parent、spring-cloud-dependencies的版本对应问题,由于一直想着用最新版本,结果导致启动不了,折腾了好多次,最终还是妥协降级版本。这里摘抄了一份
    Spring Cloud & Spring Boot 依赖关系表:

    • Finchley 是基于 Spring Boot 2.0.x 构建的,不支持 Spring Boot 1.5.x
    • Dalston 和 Edgware 是基于 Spring Boot 1.5.x 构建的,不支持 Spring Boot 2.0.x
    • Camden 构建于 Spring Boot 1.4.x,但依然能支持 Spring Boot 1.5.x
    maven仓库查看版本:https://mvnrepository.com/
    参考来源:https://blog.csdn.net/zjh_746140129/article/details/80714341

  3. 在工程配置文件application.yml中配置EurekaServer相关属性,通过server.port 配置Eureka Server端口为:8761。默认情况下EurekaServer 会向自己注册,为了防止向自己注册,需要将register-with-eureka、fetch-registry配置为false,配置如下:

    server:
      port: 8761
    eureka:
      instance:
        hostname: localhost
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone:
            http://${eureka.instance.hostname}:${server.port}/eureka/
    

    在工程启动类EurekaServerApplication添加注解@EnableEurekaServer,启动Eureka Server 功能,如:

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

    此时Eureka Server 项目搭建完成。

  4. 启动程序类EurekaServerApplication 的 main方法,在浏览器访问 http://localhost:8761/ ,进入Eureka Server 主界面,Instances currently registered with Eureka是已经向Eureka Server 注册了的客户端实例,此时没有任何实例,如:

三、编写EurekaClient

  1. 新建一个Module项目,命名为 eureka_client , 该项目将作为一个客户端向 eureka_server 进行注册。该项目pom.xml继承了主工程的pom文件,并且需要添加依赖spring-cloud-starter-eureka,如:
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.17.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
                <version>1.4.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

    pom配置完成,继续进行application.yml配置。

  2. 在application.yml进行配置Eureka Client 客户端项目的配置,配置程序名为 eurake-client,程序端口 8762,服务注册地址为以上Eureka Server 配置的服务地址,如:
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka
    server:
      port: 8762
    spring:
      application:
        name: eureka-client

    此处:defaultZone 为eureka 服务中心地址。

  3. 在项目主类EurekaClientApplication添加注解@EnableEurekaClient ,开启EurekaClient功能,如:
    @EnableEurekaClient
    @SpringBootApplication
    public class EurekaClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
    }

    启动client项目,可以看到控制台信息,启动成功:

  4. 启动客户端前,先保证服务端已经启动,在启动客户端,否则会报错连接服务端超时,此时打开服务端地址:http://localhost:8761/ ,可以看到已经有客户端注册进来,如:

    demo代码:https://download.csdn.net/download/ssdate/10756240
  5. 也可以移步至:https://gitee.com/ssdate/spring-test/tree/master/study_0307_eureka
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值