Spring Cloud Hoxton 版本使用 Consul 服务注册发现与配置中心(Hoxton版本)
摘要
之前为大家分享了:Spring Cloud Hoxton 版本微服务项目搭建eureka注册中心 ,今天为大家分享一下:Spring Cloud Hoxton 版本使用 Consul 服务注册发现与配置中心(Hoxton版本)!
Spring Cloud Consul 为 SpringBoot 应用提供了 Consul的支持,Consul既可以作为注册中心使用,也可以作为配置中心使用,本文将对其用法进行详细介绍。
可以前往查看博主的文章,传送门:Spring Cloud Consul 中文参考手册!
Consul 简介
Consul是HashiCorp公司推出的开源软件,提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。
Spring Cloud Consul 具有如下特性:
- 支持服务治理:Consul作为注册中心时,微服务中的应用可以向Consul注册自己,并且可以从Consul获取其他应用信息;
- 支持客户端负载均衡:包括Ribbon和Spring Cloud LoadBalancer;
- 支持Zuul:当Zuul作为网关时,可以从Consul中注册和发现应用;
- 支持分布式配置管理:Consul作为配置中心时,使用键值对来存储配置信息;
- 支持控制总线:可以在整个微服务系统中通过 Control Bus 分发事件消息。
使用Consul作为注册中心
首先
官网下载Consul(自行选择合适的版本),地址:https://www.consul.io/downloads.html
下载完成后只有一个exe文件,双击运行;
请参照博主的历史文章进行安装:在Windows10环境下安装配置Consul!
在命令行中输入以下命令可以查看版本号:
consul --version
使用开发模式启动:
consul agent -dev
通过以下地址可以访问Consul的首页:http://localhost:8500
创建应用注册到Consul
我们通过改造user-service和ribbon-service来演示下服务注册与发现的功能,主要是将应用原来的Eureka注册中心支持改为Consul注册中心支持。
创建consul-user-service模块和consul-ribbon-service模块;
修改相关依赖,把原来的Eureka注册发现的依赖改为Consul的,并添加SpringBoot Actuator的依赖:
<!-- 健康检查 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Cloud Consul 依赖引入 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- Admin 2.2.1 版本客户端引入依赖 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.2.1</version>
</dependency>
配置yml文件如下:
server:
#项目端口号
port: 8889
tomcat:
max-connections: 200
max-threads: 300
min-spare-threads: 0
uri-encoding: UTF-8
logging:
pattern:
console: "%d - %msg%n"
#path: D:\Logback-Test\ #日志输出到指定文件夹下默认名为spring.log
file: D:\Logback-Test\wordimg.log #日志输出到指定文件
#level: debug #指定级别
level: #指定输出某个类的日志
com.cnooc.wordimg.LoggerTest2: debug
spring:
application:
name: consul-server
# 注册到Admin管理中心
boot:
admin:
client:
url: http://localhost:6010
cloud:
consul:
# 将服务注册到consul
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
# Admin 管理配置
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
启动类:
package com.cyj.consulcenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author ChenYongJia
* @Description: ConsulCenterApplication 服务注册中心和配置中心项目
* @ClassName: SpringCloudApplication.java
* @Date 2019年12月29日 晚上22:54
* @Email chen87647213@163.com
*/
@Slf4j
@EnableDiscoveryClient
@SpringBootApplication
public class ConsulCenterApplication {
public static void main(String[] args) {
log.info("启动 consul-center 服务注册中心和配置中心项目...");
SpringApplication.run(ConsulCenterApplication.class, args);
log.info("启动 consul-center 服务注册中心和配置中心项目成功...");
}
}
运行项目,在Consul页面上可以看到如下信息:
负载均衡功能
由于我们运行了一个服务,而另一个调用的服务默认会去调用它的接口,默认实现负载均衡功能。
使用Consul作为配置中心
我们通过创建consul-config-client模块,并在Consul中添加配置信息来演示下配置管理的功能。
创建consul-config-client模块
在pom.xml中添加相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</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-actuator</artifactId>
</dependency>
完整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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cyj</groupId>
<artifactId>Family</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.cyj</groupId>
<artifactId>consul-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consul-config-client</name>
<description>CYJ:Spring Cloud Consul 服务注册中心和配置中心</description>
<dependencies>
<!-- SpringCloud Eureka 注册中心依赖 -->
<!--<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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Cloud Consul 依赖引入 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- 引入consul的config 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<!-- Admin 2.2.1 版本客户端引入依赖 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
<!-- SpringCloud 所有子项目 版本集中管理.MS:统一所有SpringCloud依赖项目的版本 依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!-- SpringBoot 2.2.x 以上版本这样配置 -->
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- SpringBoot 项目打jar包的Maven插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- SpringBoot项目打包jar名称 -->
<finalName>consul-config-client</finalName>
</build>
<!-- SpringCloud 官方远程仓库.MS:部分本地仓库和镜像仓库没有SpringCloud子项目依赖。 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
添加配置文件application.yml,启用的是dev环境的配置:
spring:
profiles:
active: dev
添加配置文件bootstrap.yml,主要是对Consul的配置功能进行配置:
server:
#项目端口号
port: 8890
tomcat:
max-connections: 200
max-threads: 300
min-spare-threads: 0
uri-encoding: UTF-8
logging:
pattern:
console: "%d - %msg%n"
#path: D:\Logback-Test\ #日志输出到指定文件夹下默认名为spring.log
file: D:\Logback-Test\wordimg.log #日志输出到指定文件
#level: debug #指定级别
level: #指定输出某个类的日志
com.cnooc.wordimg.LoggerTest2: debug
spring:
application:
name: consul-config-client
# 注册到Admin管理中心
boot:
admin:
client:
url: http://localhost:6010
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: consul-config-client
config:
# 是否启用配置中心功能
enabled: true
# 设置配置值的格式
format: yaml
# 设置配置所在目录
prefix: config
# 设置配置的分隔符
profile-separator: ':'
# 配置key的名字,由于Consul是K/V存储,配置存储在对应K的V中
data-key: data
# Admin 管理配置
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
创建ConfigClientController,从Consul配置中心中获取配置信息:
package com.cyj.consulclient.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @Description: 创建ConfigClientController,从Consul配置中心中获取配置信息
* @BelongsProject: Family
* @BelongsPackage: com.cyj.consulclient.controller
* @Author: ChenYongJia
* @CreateTime: 2020-01-07 15:36
* @Email: chen87647213@163.com
* @Version: 1.0
*/
@Slf4j
@Configuration
@RefreshScope // 用于刷新配置
@RestController
public class ConfigClientController {
@Resource
@Value("${config.info}")
private String configInfo;
/**
* http://localhost:8890/configInfo
* @return
*/
@GetMapping("/configInfo")
public String getConfigInfo() {
log.info("configInfo=================>"+configInfo);
return configInfo;
}
}
在Consul中添加配置
在consul中添加配置存储的key为:
config/consul-config-client:dev/data
启动consul-config-client,调用接口查看配置信息:http://localhost:8890/configInfo
config info for dev
Consul的动态刷新配置
我们只要修改下Consul中的配置信息,再次调用查看配置的接口,就会发现配置已经刷新。但是如果使用Spring Cloud Config,我们需要调用接口,通过Spring Cloud Bus才能刷新配置。Consul使用其自带的Control Bus 实现了一种事件传递机制,从而实现了动态刷新功能。
最后
-
更多参考精彩博文请看这里:《陈永佳的博客》
-
喜欢博主的小伙伴可以加个关注、点个赞哦,持续更新嘿嘿!