Eureka 服务注册与发现组件

Eureka 是 Spring Cloud 中负责服务注册与发现的组件,遵循 CAP 理论中的 A(可用性)和 P(分区容错性)。

Eureka 分为 Eureka Server 和 Eureka Client:

  • Eureka Server: 作为服务的注册与发现中心
  • Eureka Client: 既可以作为服务的生产者,又可以作为服务的消费者

案例实现

本案例采用一个项目中集成多个子 module 的方式实现:

  1. 启动 Eureka Server (子module: eurekaserver)
  2. 启动第一个 Eureka Client 作为服务提供者 (子module: eurekaprovider)
  3. 启动第二个 Eureka Client 作为服务消费者 (子module: eurekaconsumer)

项目结构

基于 Spring Boot 创建一个项目,包含三个子 module:

  • eurekaserver
  • eurekaprovider
  • eurekaconsumer

父 Module 的 POM 配置

<packaging>pom</packaging>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
    </dependency>
</dependencies>

Eureka Server 实现

Server POM 配置

<artifactId>eurekaserver</artifactId>
<name>eureka-server</name>
<description>Demo project for Eureka Server</description>

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

Server 启动类

@EnableEurekaServer
@SpringBootApplication
public class EurekaServicecenterApplication {

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

应用配置信息 (application.yml)

spring:
  application:
    name: eureka-server
server:
  port: 10000
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    enable-self-preservation: true
    renewal-percent-threshold: 0.8

Eureka Provider 实现

Provider POM 配置

<artifactId>eurekaprovider</artifactId>
<name>eureka-provider</name>
<description>Demo project for Eureka Provider</description>

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

Provider 启动类

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServerApplication {

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

应用配置信息 (application.properties)

server.port=10001
spring.application.name=eureka-provider
eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://localhost:10000/eureka
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.lease-expiration-duration-in-seconds=30

对外提供服务

@RestController
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(String name) {
        return name + ", Welcome to service provider";
    }
}

Eureka Consumer 实现

Consumer POM 配置

<artifactId>eurekaconsumer</artifactId>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>

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

Consumer 启动类

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

应用配置信息 (application.properties)

server.port=10002
spring.application.name=eureka-client
eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://localhost:10000/eureka
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.lease-expiration-duration-in-seconds=30

远程调用示例

@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @LoadBalanced
    @Bean
    public RestTemplate rest() {
        return new RestTemplate();
    }

    @RequestMapping("/hi")
    public String hello(String name) {
        return restTemplate.getForObject("http://eureka-provider/hello?name=" + name, String.class);
    }
}

总结

本文介绍了 Eureka 的单中心案例实现。下一篇文章将介绍 Eureka 的高可用方案。

该案例源码所在地址: 单节点 Eureka

分类: 技术分享 标签: 暂无标签

评论

暂无评论数据

暂无评论数据

目录