Spring Cloud实战系列(一) - 服务注册与发现Eureka

前言

Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现 服务注册和发现Eureka 采用了 C-S设计架构Eureka Server 作为 服务注册中心,系统中的 其他微服务,使用 Eureka客户端 连接到 Eureka Server,并通过 心跳连接 检测服务的 存活状态

正文

  • Eureka Server: 作为 服务注册中心,提供 服务注册和发现

  • Eureka Client: 所有注册到 服务中心 的服务。

    • Service Provider: 把 自身的服务 注册到 Eureka Server,从而使 服务消费方 能够找到。

    • Service Consumer: 从 Eureka Server 获取 服务注册列表,从而能够 消费服务

1. 创建服务注册中心

创建 2 个项目 Module,一个 Module(即 Spring Boot)工程作为 服务注册中心,即 Eureka Server,另一个作为 Eureka Client

Eureka Server 创建完后的工程 pom.xml 文件如下:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.ostenant.github.springcloud</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-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>
</project>
复制代码

2. 启动服务注册中心

Eureka 是一个 高可用 的组件,它没有 后端缓存。每一个 实例 注册之后,需要 定时注册中心 发送 心跳(因此可以在内存中完成)。在默认情况下 Eureka Server 也是一个 Eureka Client,必须要指定一个 Server。在启动之前,首先对 Eureka Server 配置 application.yml 文件。

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码
  • eureka.client.register-with-eureka:

设置是否将自己作为 Eureka Client 注册到 Eureka Server,默认为 true

  • eureka.client.fetch-registry

设置是否从 Eureka Server 获取 注册信息,默认为 true。因为本例是一个 单点Eureka Server,不需要 同步 其他 Eureka Server 节点的数据,所以设置为 false

  • eureka.client.service-url.defaultZone

设置的是与 Eureka Server交互地址查询注册服务 都依赖这个地址,如果有多个可以使用 英文逗号分隔

扫描二维码关注公众号,回复: 5079495 查看本文章

然后再把注解 @EnableEurekaServer 加在 Spring Boot 工程的启动类 Application 上面:

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
    }
}
复制代码

Eureka Server 是有界面的,启动项目后,打开浏览器访问 http://localhost:8761 即可查看。

3. 创建服务提供者

当一个 Eureka ClientEureka Server 发起 注册 时,它会提供一些 元数据,例如 主机端口 等等。Eureka Server 从每个 Eureka Client 实例接收 心跳消息。 如果 心跳超时,则通常将该实例从 Eureka Server 中删除。

创建一个 service-hiModule,创建完成后的 pom.xml 如下:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.ostenant.github.springcloud</groupId>
    <artifactId>service-hi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-hi</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-cloud-starter-web</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>
</project>
复制代码

通过 注解 @EnableEurekaClient 表明自己是一个 Eureka Client

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {
    @Value("${server.port}")
    private String port;

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

    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "Hi " + name + ", I am from port: "  + port;
    }
}
复制代码

仅仅 @EnableEurekaClient 是不够的,还需要在 配置文件 中注明的 服务注册中心 的地址,application.yml 配置文件如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8763
spring:
  application:
    name: service-hi
复制代码

Eureka 客户端 需要指明 spring.application.name,用于服务的 唯一标识服务之间 相互调用会基于这个 name

启动并访问 Eureka Server 的地址 http://localhost:8761,会发现服务名称为 SERVICE-HI,端口为7862 的服务,已注册到 Eureka Server 的列表上。

3. 高可用Eureka Server

在一个 分布式系统 中,服务注册中心 是最重要的基础部分,必须处于 可以提供服务 的状态。为了维持其 可用性,使用 集群 是很好的解决方案。

Eureka 通过节点 对等注册 的方式实现 高可用的部署,所以只需要为每一个 Eureke Server 配置 其他可用的 Eureke ServerserviceUrl,就能实现高可用部署。

spring:
  profiles:
    active: peer1 #peer2

---

spring:
  profiles: peer1
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/

---

spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
复制代码

更改 Eureka Server 的配置文件,再分别以 spring.profiles.active=peer1spring.profiles.active=peer2 作为参数,启动两次 Eureka Server 即可。

  • 访问 http://localhost:8761/。可以发现,Eureka Client 已经向 端口号8761Eureka Server 发起注册。

  • 服务提供者配置文件 并没有向 端口号8762Eureka Server 注册。访问 http://localhost:8762/。可以发现,服务提供者 的信息已经向 8762 发起注册了,即 8761注册信息 已经同步到 8762 节点。

参考

  • 方志朋《深入理解Spring Cloud与微服务构建》

猜你喜欢

转载自juejin.im/post/5c4c1388e51d4551d045545c