| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,241 @@ | |||
| 1 | + Spring Cloud Consul 为 SpringBoot 应用提供了 Consul的支持,Consul既可以作为注册中心使用,也可以作为配置中心使用,本文将对其用法进行详细介绍。 | ||
| 2 | + | ||
| 3 | + # Consul 简介 | ||
| 4 | + | ||
| 5 | + Consul是HashiCorp公司推出的开源软件,提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。 | ||
| 6 | + | ||
| 7 | + Spring Cloud Consul 具有如下特性: | ||
| 8 | + | ||
| 9 | + - 支持服务治理:Consul作为注册中心时,微服务中的应用可以向Consul注册自己,并且可以从Consul获取其他应用信息; | ||
| 10 | + - 支持客户端负责均衡:包括Ribbon和Spring Cloud LoadBalancer; | ||
| 11 | + - 支持Zuul:当Zuul作为网关时,可以从Consul中注册和发现应用; | ||
| 12 | + - 支持分布式配置管理:Consul作为配置中心时,使用键值对来存储配置信息; | ||
| 13 | + - 支持控制总线:可以在整个微服务系统中通过 Control Bus 分发事件消息。 | ||
| 14 | + | ||
| 15 | + # 使用Consul作为注册中心 | ||
| 16 | + | ||
| 17 | + # 安装并运行Consul | ||
| 18 | + | ||
| 19 | + - 首先我们从官网下载Consul,地址:https://www.consul.io/downloads.html | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + - 下载完成后只有一个exe文件,双击运行; | ||
| 24 | + - 在命令行中输入以下命令可以查看版本号: | ||
| 25 | + | ||
| 26 | + | ||
| 27 | + | ||
| 28 | + consul --version | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + - 查看版本号信息如下: | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + | ||
| 36 | + Consul v1.6.1 | ||
| 37 | + Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents) | ||
| 38 | + | ||
| 39 | + | ||
| 40 | + - 使用开发模式启动: | ||
| 41 | + | ||
| 42 | + | ||
| 43 | + | ||
| 44 | + consul agent -dev | ||
| 45 | + | ||
| 46 | + | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + | ||
| 50 | + | ||
| 51 | + - 通过以下地址可以访问Consul的首页:http://localhost:8500 | ||
| 52 | + | ||
| 53 | + | ||
| 54 | + | ||
| 55 | + # 创建应用注册到Consul | ||
| 56 | + | ||
| 57 | + 我们通过改造user-service和ribbon-service来演示下服务注册与发现的功能,主要是将应用原来的Eureka注册中心支持改为Consul注册中心支持。 | ||
| 58 | + | ||
| 59 | + - 创建consul-user-service模块和consul-ribbon-service模块; | ||
| 60 | + - 修改相关依赖,把原来的Eureka注册发现的依赖改为Consul的,并添加SpringBoot Actuator的依赖: | ||
| 61 | + | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + <dependency> | ||
| 65 | + <groupId>org.springframework.cloud</groupId> | ||
| 66 | + spring-cloud-starter-consul-discovery | ||
| 67 | + </dependency> | ||
| 68 | + <dependency> | ||
| 69 | + <groupId>org.springframework.boot</groupId> | ||
| 70 | + spring-boot-starter-actuator | ||
| 71 | + </dependency> | ||
| 72 | + | ||
| 73 | + | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + - 修改配置文件application.yml,将Eureka的注册发现配置改为Consul的: | ||
| 77 | + | ||
| 78 | + | ||
| 79 | + | ||
| 80 | + server: | ||
| 81 | + port: 8206 | ||
| 82 | + spring: | ||
| 83 | + application: | ||
| 84 | + name: consul-user-service | ||
| 85 | + cloud: | ||
| 86 | + consul: #Consul服务注册发现配置 | ||
| 87 | + host: localhost | ||
| 88 | + port: 8500 | ||
| 89 | + discovery: | ||
| 90 | + service-name: ${spring.application.name} | ||
| 91 | + | ||
| 92 | + | ||
| 93 | + | ||
| 94 | + | ||
| 95 | + - 运行两个consul-user-service和一个consul-ribbon-service,在Consul页面上可以看到如下信息: | ||
| 96 | + | ||
| 97 | + | ||
| 98 | + | ||
| 99 | + # 负载均衡功能 | ||
| 100 | + | ||
| 101 | + 由于我们运行了两个consul-user-service,而consul-ribbon-service默认会去调用它的接口,我们调用consul-ribbon-service的接口来演示下负载均衡功能。 | ||
| 102 | + | ||
| 103 | + 多次调用接口:http://localhost:8308/user/1 ,可以发现两个consul-user-service的控制台交替打印如下信息。 | ||
| 104 | + | ||
| 105 | + | ||
| 106 | + | ||
| 107 | + 2019-10-20 10:39:32.580 INFO 12428 --- [io-8206-exec-10] c.macro.cloud.controller.UserController : 根据id获取用户信息,用户名称为:macro | ||
| 108 | + | ||
| 109 | + | ||
| 110 | + | ||
| 111 | + | ||
| 112 | + # 使用Consul作为配置中心 | ||
| 113 | + | ||
| 114 | + 我们通过创建consul-config-client模块,并在Consul中添加配置信息来演示下配置管理的功能。 | ||
| 115 | + | ||
| 116 | + # 创建consul-config-client模块 | ||
| 117 | + | ||
| 118 | + - 在pom.xml中添加相关依赖: | ||
| 119 | + | ||
| 120 | + | ||
| 121 | + | ||
| 122 | + <dependency> | ||
| 123 | + <groupId>org.springframework.cloud</groupId> | ||
| 124 | + spring-cloud-starter-consul-config | ||
| 125 | + </dependency> | ||
| 126 | + <dependency> | ||
| 127 | + <groupId>org.springframework.cloud</groupId> | ||
| 128 | + spring-cloud-starter-consul-discovery | ||
| 129 | + </dependency> | ||
| 130 | + | ||
| 131 | + | ||
| 132 | + | ||
| 133 | + - 添加配置文件application.yml,启用的是dev环境的配置: | ||
| 134 | + | ||
| 135 | + | ||
| 136 | + | ||
| 137 | + spring: | ||
| 138 | + profiles: | ||
| 139 | + active: dev | ||
| 140 | + | ||
| 141 | + | ||
| 142 | + | ||
| 143 | + | ||
| 144 | + - 添加配置文件bootstrap.yml,主要是对Consul的配置功能进行配置: | ||
| 145 | + | ||
| 146 | + | ||
| 147 | + | ||
| 148 | + server: | ||
| 149 | + port: 9101 | ||
| 150 | + spring: | ||
| 151 | + application: | ||
| 152 | + name: consul-config-client | ||
| 153 | + cloud: | ||
| 154 | + consul: | ||
| 155 | + host: localhost | ||
| 156 | + port: 8500 | ||
| 157 | + discovery: | ||
| 158 | + serviceName: consul-config-client | ||
| 159 | + config: | ||
| 160 | + enabled: true #是否启用配置中心功能 | ||
| 161 | + format: yaml #设置配置值的格式 | ||
| 162 | + prefix: config #设置配置所在目录 | ||
| 163 | + profile-separator: ':' #设置配置的分隔符 | ||
| 164 | + data-key: data #配置key的名字,由于Consul是K/V存储,配置存储在对应K的V中 | ||
| 165 | + | ||
| 166 | + | ||
| 167 | + - 创建ConfigClientController,从Consul配置中心中获取配置信息: | ||
| 168 | + | ||
| 169 | + | ||
| 170 | + | ||
| 171 | + /** | ||
| 172 | + * Created by macro on 2019/9/11. | ||
| 173 | + */ | ||
| 174 | + @RestController | ||
| 175 | + @RefreshScope | ||
| 176 | + public class ConfigClientController { | ||
| 177 | + | ||
| 178 | + @Value("${config.info}") | ||
| 179 | + private String configInfo; | ||
| 180 | + | ||
| 181 | + @GetMapping("/configInfo") | ||
| 182 | + public String getConfigInfo() { | ||
| 183 | + return configInfo; | ||
| 184 | + } | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + | ||
| 188 | + # 在Consul中添加配置 | ||
| 189 | + | ||
| 190 | + - 在consul中添加配置存储的key为: | ||
| 191 | + | ||
| 192 | + | ||
| 193 | + | ||
| 194 | + config/consul-config-client:dev/data | ||
| 195 | + | ||
| 196 | + | ||
| 197 | + | ||
| 198 | + | ||
| 199 | + | ||
| 200 | + - 在consul中添加配置存储的value为: | ||
| 201 | + | ||
| 202 | + | ||
| 203 | + | ||
| 204 | + config: | ||
| 205 | + info: "config info for dev" | ||
| 206 | + | ||
| 207 | + | ||
| 208 | + | ||
| 209 | + | ||
| 210 | + - 存储信息截图如下: | ||
| 211 | + | ||
| 212 | + | ||
| 213 | + | ||
| 214 | + - 启动consul-config-client,调用接口查看配置信息:http://localhost:9101/configInfo | ||
| 215 | + | ||
| 216 | + | ||
| 217 | + | ||
| 218 | + config info for dev | ||
| 219 | + | ||
| 220 | + | ||
| 221 | + | ||
| 222 | + | ||
| 223 | + # Consul的动态刷新配置 | ||
| 224 | + | ||
| 225 | + 我们只要修改下Consul中的配置信息,再次调用查看配置的接口,就会发现配置已经刷新。回想下在使用Spring Cloud Config的时候,我们需要调用接口,通过Spring Cloud Bus才能刷新配置。Consul使用其自带的Control Bus 实现了一种事件传递机制,从而实现了动态刷新功能。 | ||
| 226 | + | ||
| 227 | + # 使用到的模块 | ||
| 228 | + | ||
| 229 | + | ||
| 230 | + | ||
| 231 | + springcloud-learning | ||
| 232 | + ├── consul-config-client -- 用于演示consul作为配置中心的consul客户端 | ||
| 233 | + ├── consul-user-service -- 注册到consul的提供User对象CRUD接口的服务 | ||
| 234 | + └── consul-service -- 注册到consul的ribbon服务调用测试服务 | ||
| 235 | + | ||
| 236 | + | ||
| 237 | + | ||
| 238 | + | ||
| 239 | + # 项目源码地址 | ||
| 240 | + | ||
| 241 | + https://github.com/macrozheng/springcloud-learning | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments