Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】

Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】

上一篇主要介绍了相关理论,这一篇开始咱们来一个个的实践一下。html

Just code it.java

本系列介绍的配置均基于 Spring Boot 2.0.1.RELEASE 版本和 Spring Cloud Finchley.RC1 版本git

服务注册中心

Spring Cloud 已经帮咱们实现了服务注册中心,咱们只须要很简单的几个步骤就能够完成。github

首先咱们建立一个 Spring Boot 工程,名字就叫 eureka-server,能够直接使用 Spring Initializr 建立spring

也能够直接在 pom.xml 中引入如下依赖架构

复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.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>
<spring-cloud.version>Finchley.RC1</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>

 

经过@EnableEurekaServer注解启动一个服务注册中心提供给其余应用进行对话。这一步很是的简单,只须要在一个普通的 Spring Boot 应用中添加这个注解就能开启此功能,好比app

复制
1
2
3
4
5
6
7
8
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

在默认设置下,该服务注册中心也会将本身做为客户端来尝试注册它本身,因此咱们须要禁用它的客户端注册行为,只须要在 application.yml 配置文件中增长以下信息:maven

复制
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
application:
name: eureka-server
server:
port: 7000
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • server.port:为了与后续要进行注册的服务区分,这里将服务注册中心的端口设置为 7000。
  • eureka.client.register-with-eureka:表示是否将本身注册到 Eureka Server,默认为 true。
  • eureka.client.fetch-registry:表示是否从 Eureka Server 获取注册信息,默认为 true。
  • eureka.client.service-url.defaultZone:设置与 Eureka Server 交互的地址,查询服务和注册服务都须要依赖这个地址。默认是 http://localhost:8761/eureka ;多个地址可以使用英文逗号(,)分隔。

启动工程后,访问 http://localhost:7000/,能够看到下面的页面,其中尚未发现任何服务
分布式

集群

注册中心这么关键的服务,若是是单点话,遇到故障就是毁灭性的。在一个分布式系统中,服务注册中心是最重要的基础部分,理应随时处于能够提供服务的状态。为了维持其可用性,使用集群是很好的解决方案。Eureka 经过互相注册的方式来实现高可用的部署,因此咱们只须要将 Eureke Server 配置其余可用的 service-url 就能实现高可用部署。spring-boot

双节点注册中心

首先咱们尝试一下双节点的注册中心的搭建。

一、咱们将以前的 application.yml 复制一份并命名为 application-peer1.yml,做为 peer1 服务中心的配置,并将 service-url 指向 peer2

复制
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
application:
name: eureka-server
server:
port: 7001
eureka:
instance:
hostname: peer1
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://peer2:7002/eureka/

二、将以前的 application-peer1.yml 复制一份并命名为 application-peer2.yml,做为 peer2 服务中心的配置,并将 service-url 指向 peer1

复制
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
application:
name: eureka-server
server:
port: 7002
eureka:
instance:
hostname: peer2
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://peer1:7001/eureka/

三、配本地 host:
在 hosts 文件中加入以下配置

复制
1
127.0.0.1 peer1 peer2

四、打包启动
依次执行下面命令

复制
1
2
3
4
5
6
# 打包
mvn clean package -Dmaven.test.skip=true

# 分别以 peer1 和 peer2 配置信息启动 Eureka
java -jar target/eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar target/eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

在刚启动 peer1 的时候,启动完成后会在控制台看到一些异常信息,大体就是拒绝链接、请求超时这一类的,这个不用管,启动 peer2 后就行了。

依次启动完成后,访问 http://localhost:7001/,效果以下

根据图能够看出 peer1 的注册中心 DS Replicas 已经有了 peer2 的相关配置信息,而且出如今 available-replicas 中。咱们手动中止 peer2 来观察,发现 peer2 就会移动到 unavailable-replicas 一栏中,表示 peer2 不可用。

到此双节点的配置已经完成。

注意事项

  • 在搭建 Eureka Server 双节点或集群的时候,要把eureka.client.register-with-eurekaeureka.client.fetch-registry均改成true(默认)。不然会出现实例列表为空,且 peer2 不在 available-replicas 而在 unavailable-replicas 的状况(这时其实只是启动了两个单点实例)。若是是像我这样图省事把以前的单节点配置和双节点的配置放在一个工程里,双节点的配置里要显示设置以上两个参数,直接删除是用不了默认配置的——Spring profile 会继承未在子配置里设置的父配置(application.yml)中的配置。
  • 在注册的时候,配置文件中的spring.application.name必须一致,不然状况会是这样的

Eureka 集群使用

在生产中咱们可能须要三台或者大于三台的注册中心来保证服务的稳定性,配置的原理其实都同样,将注册中心分别指向其它的注册中心。这里只介绍三台集群的配置状况,其实和双节点的注册中心相似,每台注册中心分别又指向其它两个节点便可。

application-peer1.yml

复制
1
2
3
4
5
6
7
8
9
10
11
spring:
application:
name: eureka-server
server:
port: 7001
eureka:
instance:
hostname: peer1
client:
service-url:
defaultZone: http://peer2:7002/eureka/,http://peer3:7003/eureka/

 

application-peer2.yml

复制
1
2
3
4
5
6
7
8
9
10
11
spring:
application:
name: eureka-server
server:
port: 7002
eureka:
instance:
hostname: peer2
client:
service-url:
defaultZone: http://peer1:7001/eureka/,http://peer3:7003/eureka/

 

application-peer3.yml

复制
1
2
3
4
5
6
7
8
9
10
11
spring:
application:
name: eureka-server
server:
port: 7003
eureka:
instance:
hostname: peer3
client:
service-url:
defaultZone: http://peer1:7001/eureka/,http://peer2:7002/eureka/

 

修改 hosts 文件中的配置,添加 peer3

复制
1
127.0.0.1 peer1 peer2 peer3

分别以 peer一、peer二、peer3 的配置参数启动 Eureka 注册中心

复制
1
2
3
java -jar target/eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar target/eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar target/eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3

依次启动完成后,访问 http://localhost:7001/,效果以下

能够在 peer1 中看到了 peer二、peer3 的相关信息,至此 Eureka 集群也已经完成了。

注册中心 Eureka 就介绍到这里,下一节咱们将利用咱们搭建的 Eureka Server 来为服务提供者 / 调用者提供注册 / 发现服务

相关阅读

Spring Cloud(一):服务治理技术概览
Spring Cloud(二):服务注册与发现 Eureka
Spring Cloud(三):服务提供与调用 Eureka
Spring Cloud(四):服务容错保护 Hystrix
Spring Cloud(五):Hystrix 监控面板
Spring Cloud(六):Hystrix 监控数据聚合 Turbine
Spring Cloud(七):配置中心(Git 版与动态刷新)
Spring Cloud(八):配置中心(服务化与高可用)
Spring Cloud(九):配置中心(消息总线)
Spring Cloud(十):服务网关 Zuul(路由)
Spring Cloud(十一):服务网关 Zuul(过滤器)
Spring Cloud(十二):分布式链路跟踪(Sleuth 与 Zipkin)

示例代码:GitHub

参考

springcloud(二):注册中心 Eureka
Spring Cloud 构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston 版】
Spring Cloud 技术分析(1)——服务治理
Spring Cloud - Peer Awareness

相关文章
相关标签/搜索