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

相关

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

  2. Spring Cloud实战系列(二) - 客户端调用Rest + Ribbon java

  3. Spring Cloud实战系列(三) - 声明式客户端Feign git

  4. Spring Cloud实战系列(四) - 熔断器Hystrix github

  5. Spring Cloud实战系列(五) - 服务网关Zuul web

  6. Spring Cloud实战系列(六) - 分布式配置中心Spring Cloud Configspring

  7. Spring Cloud实战系列(七) - 服务链路追踪Spring Cloud Sleuthapache

  8. Spring Cloud实战系列(八) - 微服务监控Spring Boot Admin编程

  9. Spring Cloud实战系列(九) - 服务认证受权Spring Cloud OAuth 2.0 后端

  10. Spring Cloud实战系列(十) - 单点登陆JWT与Spring Security OAuth 浏览器

前言

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交互地址查询注册服务 都依赖这个地址,若是有多个可使用 英文逗号分隔

而后再把注解 @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与微服务构建》

相关文章

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

  2. Spring Cloud实战系列(二) - 客户端调用Rest + Ribbon

  3. Spring Cloud实战系列(三) - 声明式客户端调用Feign


欢迎关注技术公众号: 零壹技术栈

零壹技术栈

本账号将持续分享后端技术干货,包括虚拟机基础,多线程编程,高性能框架,异步、缓存和消息中间件,分布式和微服务,架构学习和进阶等学习资料和文章。

相关文章
相关标签/搜索