服务治理Spring-Cloud-Eureka上

10次阅读

共计 7256 个字符,预计需要花费 19 分钟才能阅读完成。

服务治理:Spring Cloud Eureka(上)

Netflix Eureka 是由 Netflix 开源的一款基于 REST 的服务治理组件,包括 Eureka Server 及 Eureka Client。由于种种原因,Eureka 2.x 版本已经冻结开发,目前最新版本是 2018 年 8 月份发布的 1.9.4 版本。
Spring Cloud Eureka 是 Pivotal 公司为 Netflix Eureka 整合于 Spring Cloud 生态系统提供的版本。

1. 服务发现

1.1 Eureka 简介

Eureka 是 Netflix 公司提供的开源服务发现组件(现已闭源),最新版本是 1.9.4,该组件提供的服务发现可以为负载均衡、failover 等提供支持。Eureka 包括 Eureka Server 和 Eureka Client。Eureka Server 提供 REST 服务,Eureka Clinet 多数是使用 Java 编写的客户端(Eureka Client 可以使用其他语言编写,比如 Node.js 或.NET),用于简化和 Eureka Server 的交互。

1.2 Eureka Server 简单案例

所有工程使用 Spring Cloud 的新版 Greenwich.SR1 和 Maven 构建。

1.2.1 创建 Spring Cloud Eureka Server 工程

pom.xml 内容如下:

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>watermelon.cloud</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Spring Cloud Eureka Server</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <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>

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

Finchley 版本之后,Eureka 的 depenecy 片段稍微有点不同

    <!-- 原版本 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    <!-- 新版本 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>  

1.2.2 开启 EurekaServer 支持

在启动类上加上 @EnableEurekaServer 注解。

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

}

1.2.3 修改 application.yml 配置文件

server:
  port: 1111
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    server:
      waitTimeInMsWhenSyncEmpty: 0
      enableSelfPreservation: false

# 后面详细讲配置 

1.2.4 编译、启动

浏览器访问 http://localhost:1111/ 出现如下页面就说明服务端简单案例构建成功。

1.3 Eureka Client 简单案例

1.3.1 创建 Eureka Client 工程

pom.xml 内容如下:

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>watermelon.cloud</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client</name>
    <description>Spring Cloud Eureka Client</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>

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

1.3.2 启动类启用 DiscoveryClient 支持

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

}

1.3.3 修改 applicaiton.yml 配置文件

server:
  port: 2222
spring:
  application:
    name: eureka-client # 如果不配置 name 属性,在注册中心的实例名都将是 UNKNOWN
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/ # 服务注册中心地址 

1.3.4 HelloController 简单打印输出

@RestController
@Slf4j
public class HelloController {

    private final DiscoveryClient client;

    @Autowired
    public HelloController(DiscoveryClient client) {this.client = client;}

    @GetMapping("/hello")
    public String sayHello() {List<ServiceInstance> serviceInstances = client.getInstances("eureka-client");
        serviceInstances.forEach(serviceInstance -> {log.info("Host: {}, Port: {}", serviceInstance.getHost(), serviceInstance.getPort());
            log.info("serviceId: {}, InstanceId: {}", serviceInstance.getServiceId(), serviceInstance.getInstanceId());
        });
        return "Hello, Spring Cloud Eureka. -" + LocalDateTime.now().toString();
    }
}

先启动 Eureka Server,再启动 Eureka Client,测试

访问 http://localhost:111/eureka/,如果 Server 和 Client 都启动成功并且配置正确的情况将会如下情况。

访问 http://localhost:2222/hello/,会出现文字信息和日志输出。

简单的入门案例就此搭建结束,虽然没实现什么功能,但是我们可以从服务注册中心观察到可用的 Eureka Client 实例,和在日志中打印服务实例的一些简短信息。

1.4 Eureka Server 的 REST API

Eureka 在 GitHub 的 wiki 上专门写了一篇《Eureka REST operations》来介绍 Eureka Server 的 REST API 接口,Spring Cloud Netfix Eureka 跟 Spring Boot 适配之后,提供的 REST API 与原始的 REST API 有一点点不同,其路径中的 {version} 值固定为 eureka,其他的变化不大.

以下简单演示 apps 的 REST 端点:访问 http://localhost:1111/eureka/apps,会得到以下返结果。

<applications>
    <versions__delta>1</versions__delta>
    <apps__hashcode>UP_1_</apps__hashcode>
    <application>
        <name>EUREKA-CLIENT</name>
        <instance>
            <instanceId>localhost:eureka-client:2222</instanceId>
            <hostName>localhost</hostName>
            <app>EUREKA-CLIENT</app>
            <ipAddr>192.168.91.1</ipAddr>
            <status>UP</status>
            <overriddenstatus>UNKNOWN</overriddenstatus>
            <port enabled="true">2222</port>
            <securePort enabled="false">443</securePort>
            <countryId>1</countryId>
            <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
                <name>MyOwn</name>
            </dataCenterInfo>
            <leaseInfo>
                <renewalIntervalInSecs>30</renewalIntervalInSecs>
                <durationInSecs>90</durationInSecs>
                <registrationTimestamp>1557113978372</registrationTimestamp>
                <lastRenewalTimestamp>1557114128293</lastRenewalTimestamp>
                <evictionTimestamp>0</evictionTimestamp>
                <serviceUpTimestamp>1557113978373</serviceUpTimestamp>
            </leaseInfo>
            <metadata>
                <management.port>2222</management.port>
            </metadata>
            <homePageUrl>http://localhost:2222/</homePageUrl>
            <statusPageUrl>http://localhost:2222/actuator/info</statusPageUrl>
            <healthCheckUrl>http://localhost:2222/actuator/health</healthCheckUrl>
            <vipAddress>eureka-client</vipAddress>
            <secureVipAddress>eureka-client</secureVipAddress>
            <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
            <lastUpdatedTimestamp>1557113978373</lastUpdatedTimestamp>
            <lastDirtyTimestamp>1557113978278</lastDirtyTimestamp>
            <actionType>ADDED</actionType>
        </instance>
    </application>
</applications>

文末提供一些,服务发现选型对比,下篇文章介绍 Eureka 的核心类及其内容。

2. 服务发现选型


其中比较受众关注的就是 Eureka 和 Consul 这两款产品,这两款产品各有所长,各有所适,开发者可用按需选择。


个人微信公众号,欢迎交流鸭!

正文完
 0