Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】

释放双眼,带上耳机,听听看~!

已经有非常长的时间没有更新《Spring Cloud构建微服务架构》系列文章了,自从开始写Spring Cloud的专题内容开始就获得了不少的阅读量和认可,当然也有一些批评,其中也不乏一些很中肯的意见和深度的问题,对我来说也是进一步提高的契机,在此感谢所有关注我博客的读者们。

由于之前主要精力都花在的编写《Spring Cloud微服务实战》一书上,所以该系列文章就没有得到持续的维护和更新。由于漫长的写书过程和繁琐的出版流程,在本书一面世的时候,在版本上已经落后于当前的最新版本。虽然在书中前前后后加入了一些版本更新的注意事项,但是认识过程不是一蹴而就的,总是随着实践的深入慢慢发现的。所以,决定重写一下该系列文章,一方面将Spring Cloud的版本更新到Dalston,另一方面重新组织内容并增加一些之前没有写过的重要组件。希望通过这个系列,来帮助准备使用Spring Cloud的朋友们快速入门。同时,也是作为《Spring Cloud微服务实战》一书对最新版本做一些不同内容的补充。

Spring Cloud简介

Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud0 CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等项目。

微服务架构

“微服务架构”在这几年非常的火热,以至于关于微服务架构相关的开源产品被反复的提及(比如:netflix、dubbo),Spring Cloud也因Spring社区的强大知名度和影响力也被广大架构师与开发者备受关注。

那么什么是“微服务架构”呢?简单的说,微服务架构就是将一个完整的应用从数据存储开始垂直拆分成多个不同的服务,每个服务都能独立部署、独立维护、独立扩展,服务与服务间通过诸如RESTful API的方式互相调用。

对于“微服务架构”,大家在互联网可以搜索到很多相关的介绍和研究文章来进行学习和了解。也可以阅读始祖Martin Fowler的《Microservices》(中文版翻译点击查看),本文不做更多的介绍和描述。

服务治理

在简单介绍了Spring Cloud和微服务架构之后,下面回归本文的主旨内容,如何使用Spring Cloud来实现服务治理。

由于Spring Cloud为服务治理做了一层抽象接口,所以在Spring Cloud应用中可以支持多种不同的服务治理框架,比如:Netflix Eureka、Consul、Zookeeper。在Spring Cloud服务治理抽象层的作用下,我们可以无缝地切换服务治理实现,并且不影响任何其他的服务注册、服务发现、服务调用等逻辑。

所以,下面我们通过介绍两种服务治理的实现来体会Spring Cloud这一层抽象所带来的好处。

Spring Cloud Eureka

首先,我们来尝试使用Spring Cloud Eureka来实现服务治理。

Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。

下面,就来具体看看如何使用Spring Cloud Eureka实现服务治理。

创建“服务注册中心”

创建一个基础的Spring Boot工程,命名为eureka-server,并在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
1<parent>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-parent</artifactId>
4    <version>1.5.4.RELEASE</version>
5    <relativePath/>
6</parent>
7
8<dependencies>
9    <dependency>
10        <groupId>org.springframework.cloud</groupId>
11        <artifactId>spring-cloud-starter-eureka-server</artifactId>
12    </dependency>
13</dependencies>
14
15<dependencyManagement>
16    <dependencies>
17        <dependency>
18           <groupId>org.springframework.cloud</groupId>
19           <artifactId>spring-cloud-dependencies</artifactId>
20           <version>Dalston.SR1</version>
21           <type>pom</type>
22           <scope>import</scope>
23        </dependency>
24    </dependencies>
25</dependencyManagement>
26

通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。这一步非常的简单,只需要在一个普通的Spring Boot应用中添加这个注解就能开启此功能,比如下面的例子:


1
2
3
4
5
6
7
8
9
10
1@EnableEurekaServer
2@SpringBootApplication
3public class Application {
4
5    public static void main(String[] args) {
6        new SpringApplicationBuilder(Application.class)
7                    .web(true).run(args);
8    }
9}
10

在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties配置文件中增加如下信息:


1
2
3
4
5
6
7
1spring.application.name=eureka-server
2server.port=1001
3
4eureka.instance.hostname=localhost
5eureka.client.register-with-eureka=false
6eureka.client.fetch-registry=false
7

为了与后续要进行注册的服务区分,这里将服务注册中心的端口通过server.port属性设置为1001。启动工程后,访问:http://localhost:1001/,可以看到下面的页面,其中还没有发现任何服务。

Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】

创建“服务提供方”

下面我们创建提供服务的客户端,并向服务注册中心注册自己。本文我们主要介绍服务的注册与发现,所以我们不妨在服务提供方中尝试着提供一个接口来获取当前所有的服务信息。

首先,创建一个基本的Spring Boot应用。命名为eureka-client,在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
1<parent>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-parent</artifactId>
4    <version>1.5.4.RELEASE</version>
5    <relativePath/> <!-- lookup parent from repository -->
6</parent>
7
8<dependencies>
9    <dependency>
10        <groupId>org.springframework.cloud</groupId>
11        <artifactId>spring-cloud-starter-eureka</artifactId>
12    </dependency>
13    <dependency>
14        <groupId>org.springframework.boot</groupId>
15        <artifactId>spring-boot-starter-web</artifactId>
16    </dependency>
17</dependencies>
18
19<dependencyManagement>
20    <dependencies>
21        <dependency>
22           <groupId>org.springframework.cloud</groupId>
23           <artifactId>spring-cloud-dependencies</artifactId>
24           <version>Dalston.SR1</version>
25           <type>pom</type>
26           <scope>import</scope>
27        </dependency>
28    </dependencies>
29</dependencyManagement>
30

其次,实现/dc请求处理接口,通过DiscoveryClient对象,在日志中打印出服务实例的相关内容。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1@RestController
2public class DcController {
3
4    @Autowired
5    DiscoveryClient discoveryClient;
6
7    @GetMapping("/dc")
8    public String dc() {
9        String services = "Services: " + discoveryClient.getServices();
10        System.out.println(services);
11        return services;
12    }
13
14}
15

最后在应用主类中通过加上@EnableDiscoveryClient注解,该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出。


1
2
3
4
5
6
7
8
9
10
1@EnableDiscoveryClient
2@SpringBootApplication
3public class Application {
4    public static void main(String[] args) {
5        new SpringApplicationBuilder(
6            ComputeServiceApplication.class)
7            .web(true).run(args);
8    }
9}
10

我们在完成了服务内容的实现之后,再继续对application.properties做一些配置工作,具体如下:


1
2
3
4
1spring.application.name=eureka-client
2server.port=2001
3eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
4

通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。为了在本机上测试区分服务提供方和服务注册中心,使用server.port属性设置不同的端口。

启动该工程后,再次访问:http://localhost:1001/。可以如下图内容,我们定义的服务被成功注册了。

Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】

当然,我们也可以通过直接访问eureka-client服务提供的/dc接口来获取当前的服务清单,只需要访问:http://localhost:2001/dc,我们可以得到如下输出返回:


1
2
1Services: [eureka-client]
2

其中,方括号中的eureka-client就是通过Spring Cloud定义的DiscoveryClient接口在eureka的实现中获取到的所有服务清单。由于Spring Cloud在服务发现这一层做了非常好的抽象,所以,对于上面的程序,我们可以无缝的从eureka的服务治理体系切换到consul的服务治理体系中区。

Spring Cloud Consul

Spring Cloud Consul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,它包含多个组件,但是作为一个整体,在微服务架构中为我们的基础设施提供服务发现和服务配置的工具。它包含了下面几个特性:

  • 服务发现

  • 健康检查

  • Key/Value存储

  • 多数据中心

由于Spring Cloud Consul项目的实现,我们可以轻松的将基于Spring Boot的微服务应用注册到Consul上,并通过此实现微服务架构中的服务治理。

以之前实现的基于Eureka的示例(eureka-client)为基础,我们如何将之前实现的服务提供者注册到Consul上呢?方法非常简单,我们只需要在pom.xml中将eureka的依赖修改为如下依赖:


1
2
3
4
5
1<dependency>
2  <groupId>org.springframework.cloud</groupId>
3  <artifactId>spring-cloud-starter-consul-discovery</artifactId>
4</dependency>
5

接下来再修改一下application.properites,将consul需要的配置信息加入即可,比如:(下面配置是默认值)


1
2
3
1spring.cloud.consul.host=localhost
2spring.cloud.consul.port=8500
3

到此为止,我们将eureka-client转换为基于consul服务治理的服务提供者就完成了。前文我们已经有提到过服务发现的接口DiscoveryClient是Spring Cloud对服务治理做的一层抽象,所以可以屏蔽Eureka和Consul服务治理的实现细节,我们的程序不需要做任何改变,只需要引入不同的服务治理依赖,并配置相关的配置属性就能轻松的将微服务纳入Spring Cloud的各个服务治理框架中。

下面可以尝试让consul的服务提供者运行起来。这里可能读者会问,不需要创建类似eureka-server的服务端吗?由于Consul自身提供了服务端,所以我们不需要像之前实现Eureka的时候创建服务注册中心,直接通过下载consul的服务端程序就可以使用。

我们可以用下面的命令启动consul的开发模式:


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
1$consul agent -dev
2==> Starting Consul agent...
3==> Starting Consul agent RPC...
4==> Consul agent running!
5           Version: 'v0.7.2'
6         Node name: 'Lenovo-zhaiyc'
7        Datacenter: 'dc1'
8            Server: true (bootstrap: false)
9       Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
10      Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
11    Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
12             Atlas: <disabled>
13
14==> Log data will now stream in as it occurs:
15
16    2017/06/22 07:50:54 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:127.0.0.1:8300 Address:127.0.0.1:8300}]
17    2017/06/22 07:50:54 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
18    2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc 127.0.0.1
19    2017/06/22 07:50:54 [INFO] consul: Adding LAN server Lenovo-zhaiyc (Addr: tcp/127.0.0.1:8300) (DC: dc1)
20    2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc.dc1 127.0.0.1
21    2017/06/22 07:50:54 [INFO] consul: Adding WAN server Lenovo-zhaiyc.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)
22    2017/06/22 07:51:01 [ERR] agent: failed to sync remote state: No cluster leader
23    2017/06/22 07:51:02 [WARN] raft: Heartbeat timeout from "" reached, starting election
24    2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
25    2017/06/22 07:51:02 [DEBUG] raft: Votes needed: 1
26    2017/06/22 07:51:02 [DEBUG] raft: Vote granted from 127.0.0.1:8300 in term 2. Tally: 1
27    2017/06/22 07:51:02 [INFO] raft: Election won. Tally: 1
28    2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
29    2017/06/22 07:51:02 [INFO] consul: cluster leadership acquired
30    2017/06/22 07:51:02 [INFO] consul: New leader elected: Lenovo-zhaiyc
31    2017/06/22 07:51:02 [DEBUG] consul: reset tombstone GC to index 3
32    2017/06/22 07:51:02 [INFO] consul: member 'Lenovo-zhaiyc' joined, marking health alive
33    2017/06/22 07:51:02 [INFO] agent: Synced service 'consul'
34    2017/06/22 07:51:02 [DEBUG] agent: Node info in sync
35

consul服务端启动完成之后,我们再将之前改造后的consul服务提供者启动起来。consul与eureka一样,都提供了简单的ui界面来查看服务的注册情况:

Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】

更多关于Consul的使用指南,读者可查看官方文档:https://www.consul.io/

更多Spring Cloud内容请持续关注我的博客更新或在《Spring Cloud微服务实战》中获取。

代码示例

样例工程将沿用之前在码云和GitHub上创建的SpringCloud-Learning项目,重新做了一下整理。通过不同目录来区分Brixton和Dalston的示例。

  • 码云:点击查看

  • GitHub:点击查看

具体工程说明如下:

  • eureka的服务注册中心:eureka-server

  • eureka的服务提供方:eureka-client

  • consul的服务提供方:consul-client

相关阅读

  • eureka的高可用服务注册中心

  • eureka源码分析

  • 基于consul的分布式锁

  • 基于consul的分布式信号量

  • 更多Spring Cloud内容…

给TA打赏
共{{data.count}}人
人已打赏
安全经验

Google Adsense(Google网站联盟)广告申请指南

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索