Eureka服务治理

作者: gluawwa | 来源:发表于2018-08-20 23:57 被阅读5次

    构建服务注册中心

    创建工程

      首先创建一个基本的SpringBoot工程,命名为eurekaserver,向pom文件中引入必要的依赖。

    <?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.example</groupId>
        <artifactId>demo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>eureka-server</name>
        <description>Demo project for Spring Boot</description>
    
        <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>
        </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>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Finchley.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    启动类EurekaServerApplication

    在启动类上添加@EnableEurekaServer注解

    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    
    

    配置文件application.yaml

    #不向服务中心注册自身
    eureka.client.register-with-eureka=false
    #不检索服务
    eureka.client.fetch-registry=false
    

    注册服务提供者

      使用上一章的工程,作为服务发布自己。首先在pom中加入eureka的依赖:

    <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-netflix-eureka-server</artifactId>
            </dependency>
    
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Finchley.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    

      在主类中添加@EnableEurekaClient注解

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @EnableEurekaClient
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

      最后,需要在application.properties文件中对服务命名,并配置之前构建的服务注册中心地址:

    spring.application.name=hello-service
    #多个地址用逗号分隔
    eureka.client.service-url.defaultZone=http://localhost:8080/eureka
    
    56B6935A-BE82-4A75-9049-5B5D7445C67E.png

    高可用注册中心

      在生产环境中,我们会注册多个eureka注册中心实例来保证服务的高可用性。我们可以在之前的注册实例工程之上进行扩展,创建application-dev.properties和application-test.properties文件,并在hosts文件中修改dev、test使其指向127.0.0.1。

    spring.application.name=eureka-server
    server.port=2222
    eureka.instance.hostname=dev
    eureka.client.service-url.defaultZone=http://test:1111/eureka
    eureka.client.register-with-eureka=true 
    eureka.client.fetch-registry=true
    
    spring.application.name=eureka-server
    server.port=1111
    eureka.instance.hostname=test
    eureka.client.service-url.defaultZone=http://dev:2222/eureka
    eureka.client.register-with-eureka=true 
    eureka.client.fetch-registry=true
    

      使用两个配置文件分别启动注册中心之后,需要对之前的hello-service配置文件进行修改:

    eureka.client.service-url.defaultZone=http://localhost:1111/eureka,http://localhost:2222/eureka
    

    eureka集群available-replicas不为空需满足如下条件:
      1.不能用localhost比如:
      eureka.client.service-url.defaultZone=http://localhost:2222/eureka
      要采用:
      eureka.client.serviceUrl.defaultZone=http://dev:2222/eureka
      2.相互注册要开启:
      eureka.client.register-with-eureka=true
      eureka.client.fetch-registry=true

    相关文章

      网友评论

        本文标题:Eureka服务治理

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