美文网首页Spring Cloud 入门学习
Chapter 2 - Spring Cloud Eureka

Chapter 2 - Spring Cloud Eureka

作者: liuyangcc | 来源:发表于2018-08-20 17:29 被阅读0次

    1 微服务模块功能介绍

    1.1 服务治理

    服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现。
    Spring Cloud Eureka是Spring Cloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装。主要负责完成微服务架构中的服务治理功能。

    1.2 服务注册

    在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,包括服务的主机与端口号、服务版本号、通讯协议等一些附加信息。注册中心按照服务名分类组织服务清单,同时还需要以心跳检测的方式去监测清单中的服务是否可用,若不可用需要从服务清单中剔除,以达到排除故障服务的效果。

    1.3 服务发现

    在服务治理框架下,服务间的调用不再通过指定具体的实例地址来实现,而是通过服务名发起请求调用实现。服务调用方通过服务名从服务注册中心的服务清单中获取服务实例的列表清单,通过指定的负载均衡策略取出一个服务实例位置来进行服务调用。

    2 Demo搭建

    2.1 Eureka Server 搭建服务注册中心

    工程结构如下

    /src
      /main
        /java
          /com/lerr/baseeureka
            BaseEurekaApplication.java
        /resources
          application.properties
    pom.xml
    
    2.1.1 以Maven为例,进行工程依赖配置
    • pom.xml (pom.xml引入相关依赖)
        <!-- 使用阿里云配置,下载速度可靠 -->
        <repositories>
            <repository>
                <id>alimaven</id>
                <name>aliyun maven</name>
                <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            </repository>
        </repositories>
    
        <!-- 继承 spring-boot-starter-parent 一些已有的配置  -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.4.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>
            <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <!-- 引入 eureka-server表明当前工程是作为服务注册中心 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    2.1.2 主启动类,增加注解@EnableEurekaServer,开启Server能力
    • BaseEurekaApplication.java
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer //启动一个服务注册中心,供其他应用进行对话
    @SpringBootApplication
    public class BaseEurekaApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(BaseEurekaApplication.class, args);
        }
    
    }
    
    
    2.1.3 配置文件配置

    由于本身是服务注册中心,因此不需要自己向自己注册,eureka.client.register-with-eureka因为默认是true,所以需要特别注意声明为false

    • application.properties
    server.port=1111
    
    eureka.instance.hostname=localhost
    # 不向注册中心注册自己
    eureka.client.register-with-eureka=false
    # 注册中心的职责是维护实例,不需要去检索服务
    eureka.client.fetch-registry=false
    
    2.1.4 启动工程并访问

    访问 http://localhost:1111/

    Eureka 主页

    2.2 微服务模块去注册中心注册

    工程结构如下

    /src
      /main
        /java
          /com/lerr/basemsone
            BaseMsOneApplication.java
        /resources
          application.properties
          bootstrap.properties
    pom.xml
    
    2.2.1 依赖配置,pom.xml
    • pom.xml
     <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.4.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>
            <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <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>
    
            <!-- 如果是具体业务模块,引入eureka-client依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
    
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    2.2.2 启动类配置
    @EnableDiscoveryClient  //表明去注册中心注册  
    @SpringBootApplication
    public class BaseMsOneApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(BaseMsOneApplication.class, args);
        }
    
    }
    
    2.2.3 配置文件
    • application.properties
    server.port=8180
    
    • bootstrap.properties
    # 设定服务名称
    spring.application.name=ms-one
    # 服务注册中心的地址,告诉应用去这个地方注册
    eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
    

    3 启动服务注册中心和微服务

    3.1 服务注册中心页面截图

    image.png

    3.2 日志分析

    可以看一下微服务模块的日志,表示的是去服务中心注册

    2018-08-20 23:17:05.257  INFO 21816 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1534778225257 with initial instances count: 0
    2018-08-20 23:17:05.261  INFO 21816 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application ms-one with eureka with status UP
    2018-08-20 23:17:05.262  INFO 21816 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1534778225262, current=UP, previous=STARTING]
    2018-08-20 23:17:05.264  INFO 21816 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_MS-ONE/F0R6S7J.SU.intra.cpic.com.cn:ms-one:8180: registering service...
    

    同时看一下服务注册中心的日志,当有应用过来注册,则输出如下日志

    2018-08-20 23:17:05.395  INFO 24168 --- [nio-1111-exec-9] c.n.e.registry.AbstractInstanceRegistry  : Registered instance MS-ONE/F0R6S7J.SU.intra.cpic.com.cn:ms-one:8180 with status UP (replication=false)
    

    相关文章

      网友评论

        本文标题:Chapter 2 - Spring Cloud Eureka

        本文链接:https://www.haomeiwen.com/subject/pdpliftx.html