Eureka服务发现---SpringCloud(一)

五迷三道 提交于 2019-12-10 06:10:44

Eureka服务发现

(一)前言

    服务治理是微服务架构中最为核心的基础模块,主要用来实现各个微服务实例的自动化注册和发现;
    在微服务最开始的时候,服务并不多,我们可以通过静态配置来完成服务间的调用;若A服务调用B服务,对B服务做了HA,我们可以手动维护一份B实例的清单,
当B服务某些实例不可用,还可以手动去清除部分不可用的实例;但随着业务的发展,需要维护的B实例越来越多,系统功能也越来越复杂,维护难度可想而知;而且相同
服务不同实例很可能会命名冲突,配置冲突等,如port;
    因此我们需要一个服务治理框架和产品,来对不同服务的多个实例进行自动化的维护;
    目前服务治理框架有Netflix的Eureka,alibaba的Dubbo等,这些框架的实现都围绕着服务注册与服务发现机制来完成对微服务应用实例的自动化管理;

(二)搭建服务注册中心

我们使用project-module的形式创建项目; 我们创建一个CloudServer的项目,

   <?xml version="1.0" encoding="UTF-8"?>
   <project xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>

       <groupId>com.river</groupId>
       <artifactId>CloudServer</artifactId>
       <packaging>pom</packaging>
       <version>1.0-SNAPSHOT</version>

       <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>1.5.2.RELEASE</version>
           <relativePath/>
       </parent>

       <properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
           <java.version>1.8</java.version>
           <swaggger2.version>2.2.2</swaggger2.version>
       </properties>

       <!--依赖管理,用于统一管理整个项目依赖的版本等-->
       <dependencyManagement>
           <dependencies>
                <!--swagger的依赖,用于自动化接口列表,可删除-->
               <dependency>
                   <groupId>io.springfox</groupId>
                   <artifactId>springfox-swagger2</artifactId>
                   <version>${swaggger2.version}</version>
               </dependency>
               <dependency>
                   <groupId>io.springfox</groupId>
                   <artifactId>springfox-swagger-ui</artifactId>
                   <version>${swaggger2.version}</version>
               </dependency>
               <!--swagger-->


               <dependency>
                   <groupId>org.springframework.cloud</groupId>
                   <artifactId>spring-cloud-dependencies</artifactId>
                   <version>Dalston.RC1</version>
                   <type>pom</type>
                   <scope>import</scope>
               </dependency>
           </dependencies>
       </dependencyManagement>

       <!--导入依赖的包,省略version-->
       <dependencies>

           <dependency>
               <groupId>io.springfox</groupId>
               <artifactId>springfox-swagger2</artifactId>
           </dependency>
           <dependency>
               <groupId>io.springfox</groupId>
               <artifactId>springfox-swagger-ui</artifactId>
           </dependency>

           <!--一个springboot的监控-->
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-actuator</artifactId>
           </dependency>
       </dependencies>

       <build>
           <plugins>
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
               </plugin>
           </plugins>
       </build>
   </project>

下面我们添加一个EurekaServer的module模块,作为Eureka注册中心;

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>CloudServer</artifactId>
            <groupId>com.river</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>

        <artifactId>EurekaServer</artifactId>

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

            <!-- spring boot test-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>   

配置文件application.yaml

    server:
      port: 8761 #端口号
    eureka:
      instance:
        hostname: localhost
        home-page-url: swagger-ui.html
      client:
        register-with-eureka: false  #是否向注册中心注册,这里由于自己就是注册中心,因此为false;
        fetch-registry: false        #检索服务,由于自己在维护服务实例,因此无需检索服务,故false;
        service-url:
          defaultZone: http://localhost:8761/eureka/  
      server:
        enable-self-preservation: false
    spring:
      application:
        name: eureka-server  #应用名

此时添加springboot启动类

       package com.river.eureka.server;


       import org.springframework.boot.SpringApplication;
       import org.springframework.boot.autoconfigure.SpringBootApplication;
       import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

       @EnableEurekaServer //启动一个服务注册中心,与其他服务进行对话;
       @SpringBootApplication
       public class EurekaServerApplication {

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

此时我们启动启动类,然后访问localhost:8761,会看到Eureka服务实例列表页,但是在Instances一栏为空,因为我们没有服务实例注册在Eureka上;

(三)客户端

我们在新建一个名字为ApplicationClientOne的module;

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>CloudServer</artifactId>
            <groupId>com.river</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>

        <artifactId>ApplicationClientOne</artifactId>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>

        </dependencies>

    </project>

配置application.yaml;

   server:
      port: 9001
   eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
          register-with-eureka: true
          fetch-registry: true
   spring:
     application:
       name: applicationClient

添加启动类:

package com.river.application.one;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient   //说明这是一个客户端,与@EnableDiscoveryClient注解作用相同
@SpringBootApplication
public class ApplicationClientOne {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationClientOne.class,args);
    }
}

说明: @EnableEurekaClient: @EnableDiscoveryClient: 在springcloud中的服务治理discovery service可以有多种实现(eureka,consul,zookeeper等),后者来自于spring-cloud-commons包,可以支持不同的服务治理插件; 而前者来自于eureka,只支持eureka;

启动客户端项目,查看日志可以发现:

2018-07-31 10:40:40.558  INFO 11296 --- [nio-8761-exec-2] c.n.e.registry.AbstractInstanceRegistry  : 
Registered instance APPLICATIONCLIENT/PC-HEPENGFEI.ppmoney.com:applicationClient:9001 with status UP (replication=false)

在Eureka的服务实例列表中可以看到我们的这个实例;

#Eureka服务发现

(一)前言

    服务治理是微服务架构中最为核心的基础模块,主要用来实现各个微服务实例的自动化注册和发现;
    在微服务最开始的时候,服务并不多,我们可以通过静态配置来完成服务间的调用;若A服务调用B服务,对B服务做了HA,我们可以手动维护一份B实例的清单,
当B服务某些实例不可用,还可以手动去清除部分不可用的实例;但随着业务的发展,需要维护的B实例越来越多,系统功能也越来越复杂,维护难度可想而知;而且相同
服务不同实例很可能会命名冲突,配置冲突等,如port;
    因此我们需要一个服务治理框架和产品,来对不同服务的多个实例进行自动化的维护;
    目前服务治理框架有Netflix的Eureka,alibaba的Dubbo等,这些框架的实现都围绕着服务注册与服务发现机制来完成对微服务应用实例的自动化管理;

(二)搭建服务注册中心

我们使用project-module的形式创建项目; 我们创建一个CloudServer的项目,

   <?xml version="1.0" encoding="UTF-8"?>
   <project xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>

       <groupId>com.river</groupId>
       <artifactId>CloudServer</artifactId>
       <packaging>pom</packaging>
       <version>1.0-SNAPSHOT</version>

       <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>1.5.2.RELEASE</version>
           <relativePath/>
       </parent>

       <properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
           <java.version>1.8</java.version>
           <swaggger2.version>2.2.2</swaggger2.version>
       </properties>

       <!--依赖管理,用于统一管理整个项目依赖的版本等-->
       <dependencyManagement>
           <dependencies>
                <!--swagger的依赖,用于自动化接口列表,可删除-->
               <dependency>
                   <groupId>io.springfox</groupId>
                   <artifactId>springfox-swagger2</artifactId>
                   <version>${swaggger2.version}</version>
               </dependency>
               <dependency>
                   <groupId>io.springfox</groupId>
                   <artifactId>springfox-swagger-ui</artifactId>
                   <version>${swaggger2.version}</version>
               </dependency>
               <!--swagger-->


               <dependency>
                   <groupId>org.springframework.cloud</groupId>
                   <artifactId>spring-cloud-dependencies</artifactId>
                   <version>Dalston.RC1</version>
                   <type>pom</type>
                   <scope>import</scope>
               </dependency>
           </dependencies>
       </dependencyManagement>

       <!--导入依赖的包,省略version-->
       <dependencies>

           <dependency>
               <groupId>io.springfox</groupId>
               <artifactId>springfox-swagger2</artifactId>
           </dependency>
           <dependency>
               <groupId>io.springfox</groupId>
               <artifactId>springfox-swagger-ui</artifactId>
           </dependency>

           <!--一个springboot的监控-->
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-actuator</artifactId>
           </dependency>
       </dependencies>

       <build>
           <plugins>
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
               </plugin>
           </plugins>
       </build>
   </project>

下面我们添加一个EurekaServer的module模块,作为Eureka注册中心;

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>CloudServer</artifactId>
            <groupId>com.river</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>

        <artifactId>EurekaServer</artifactId>

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

            <!-- spring boot test-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>   

配置文件application.yaml

    server:
      port: 8761 #端口号
    eureka:
      instance:
        hostname: localhost
        home-page-url: swagger-ui.html
      client:
        register-with-eureka: false  #是否向注册中心注册,这里由于自己就是注册中心,因此为false;
        fetch-registry: false        #检索服务,由于自己在维护服务实例,因此无需检索服务,故false;
        service-url:
          defaultZone: http://localhost:8761/eureka/  
      server:
        enable-self-preservation: false
    spring:
      application:
        name: eureka-server  #应用名

此时添加springboot启动类

       package com.river.eureka.server;


       import org.springframework.boot.SpringApplication;
       import org.springframework.boot.autoconfigure.SpringBootApplication;
       import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

       @EnableEurekaServer //启动一个服务注册中心,与其他服务进行对话;
       @SpringBootApplication
       public class EurekaServerApplication {

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

此时我们启动启动类,然后访问localhost:8761,会看到Eureka服务实例列表页,但是在Instances一栏为空,因为我们没有服务实例注册在Eureka上;

(三)客户端

我们在新建一个名字为ApplicationClientOne的module;

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>CloudServer</artifactId>
            <groupId>com.river</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>

        <artifactId>ApplicationClientOne</artifactId>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>

        </dependencies>

    </project>

配置application.yaml;

   server:
      port: 9001
   eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
          register-with-eureka: true
          fetch-registry: true
   spring:
     application:
       name: applicationClient

添加启动类:

package com.river.application.one;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient   //说明这是一个客户端,与@EnableDiscoveryClient注解作用相同
@SpringBootApplication
public class ApplicationClientOne {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationClientOne.class,args);
    }
}

说明: @EnableEurekaClient:

@EnableDiscoveryClient:

在springcloud中的服务治理discovery service可以有多种实现(eureka,consul,zookeeper等),后者来自于spring-cloud-commons包,可以支持不同的服务治理插件; 而前者来自于eureka,只支持eureka;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnableDiscoveryClient
public @interface EnableEurekaClient {

}

启动客户端项目,查看日志可以发现:

2018-07-31 10:40:40.558  INFO 11296 --- [nio-8761-exec-2] c.n.e.registry.AbstractInstanceRegistry  : 
Registered instance APPLICATIONCLIENT/PC-HEPENGFEI.ppmoney.com:applicationClient:9001 with status UP (replication=false)

在Eureka的服务实例列表中可以看到我们的这个实例;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!