springcloud中使用consul作为注册中心

Consul 介绍

Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其它分布式服务注册与发现的方案,Consul 的方案更“一站式”,内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value 存储、多数据中心方案,不再需要依赖其它工具(比如 ZooKeeper 等)。使用起来也较 为简单。Consul 使用 Go 语言编写,因此具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与 Docker 等轻量级容器可无缝配合。

特性:

服务发现
健康检查
Key/Value 存储
多数据中心

Consul 角色

client: 客户端, 无状态, 将 HTTP 和 DNS 接口请求转发给局域网内的服务端集群。
server: 服务端, 保存配置信息, 高可用集群, 在局域网内与本地客户端通讯, 通过广域网与其它数据中心通讯。 每个数据中心的 server 数量推荐为 3 个或是 5 个。

consul工作原理
在这里插入图片描述
1、当 Producer 启动的时候,会向 Consul 发送一个 post 请求,告诉 Consul 自己的 IP 和 Port
2、Consul 接收到 Producer 的注册后,每隔10s(默认)会向 Producer 发送一个健康检查的请求,检验Producer是否健康
3、当 Consumer 发送 GET 方式请求 /api/address 到 Producer 时,会先从 Consul 中拿到一个存储服务 IP 和 Port 的临时表,从表中拿到 Producer 的 IP 和 Port 后再发送 GET 方式请求 /api/address
4、该临时表每隔10s会更新,只包含有通过了健康检查的 Producer
Spring Cloud Consul 项目是针对 Consul 的服务治理实现。Consul 是一个分布式高可用的系统,它包含多个组件,但是作为一个整体,在微服务架构中为我们的基础设施提供服务发现和服务配置的工具。

Consul 安装

Consul 不同于 Eureka 需要单独安装,访问Consul 官网下载 Consul 的最新版本,我这里是 consul_1.2.1。

根据不同的系统类型选择不同的安装包,从下图也可以看出 Consul 支持所有主流系统。
在这里插入图片描述
我这里以 Windows 为例,下载下来是一个 consul_1.2.1_windows_amd64.zip 的压缩包,解压是一个 consul.exe 的执行文件。
在这里插入图片描述
cd 到对应的目录下,使用 cmd 启动 Consul

cd D:\Common Files\consul
#cmd启动:
consul agent -dev        # -dev表示开发模式运行,另外还有-server表示服务模式运行

启动结果如下:
在这里插入图片描述
启动成功之后访问:http://localhost:8500,可以看到 Consul 的管理界面
在这里插入图片描述
这样就意味着我们的 Consul 服务启动成功了。

Consul 服务端

也意味着我们自己编写的微服务作为服务端提供服务,注册到consul

接下来我们开发 Consul 的服务端,我们创建一个 spring-cloud-consul-producer 项目

添加依赖包
依赖包如下:

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-consul-discovery</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

spring-boot-starter-actuator 健康检查依赖于此包。
spring-cloud-starter-consul-discovery Spring Cloud Consul 的支持。
Spring Boot 版本使用的是 2.0.3.RELEASE,Spring Cloud 最新版本是 Finchley.RELEASE 依赖于 Spring Boot 2.x.

配置文件

spring.application.name=spring-cloud-consul-producer
server.port=8501
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=service-producer

Consul 的地址和端口号默认是 localhost:8500 ,如果不是这个地址可以自行配置。spring.cloud.consul.discovery.serviceName 是指注册到 Consul 的服务名称,后期客户端会根据这个名称来进行服务调用。

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulProducerApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(ConsulProducerApplication.class, args);
	}
}

添加了 @EnableDiscoveryClient 注解表示支持服务发现。
提供服务
我们在创建一个 Controller,推文提供 hello 的服务。

@RestController
public class HelloController {
 
    @RequestMapping("/hello")
    public String hello() {
        return "helle consul";
    }
}

为了模拟注册均衡负载复制一份上面的项目重命名为 spring-cloud-consul-producer-2 ,修改对应的端口为 8502,修改 hello 方法的返回值为:”helle consul two”,修改完成后依次启动两个项目。

这时候我们再次在浏览器访问地址:http://localhost:8500,显示如下:
在这里插入图片描述
我们发现页面多了 service-producer 服务,点击进去后页面显示有两个服务提供者:
在这里插入图片描述
这样服务提供者就准备好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值