|
| 1 | +# Spring Cloud 高可用服务治理 |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +## Spring Cloud Eureka |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +### Eureka 客户端 |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +#### 配置多Eureka 注册中心 |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +```properties |
| 18 | +## 应用名称 |
| 19 | +spring.application.name = spring-cloud-eureka-client |
| 20 | + |
| 21 | +## 客户端 端口随即可用 |
| 22 | +server.port = 0 |
| 23 | + |
| 24 | +## 配置连接 Eureka 服务器 |
| 25 | +## 配置多个 Eureka 注册中心,以"," 分割 |
| 26 | +eureka.client.serviceUrl.defaultZone = \ |
| 27 | + http://localhost:9090/eureka,\ |
| 28 | + http://localhost:9091/eureka |
| 29 | +``` |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +#### 激活 :`@EnableEurekaClient` 或者 `@EnableDiscoveryClient` |
| 34 | + |
| 35 | +```java |
| 36 | +package com.segumentfault.springcloudlesson5eurekaclient; |
| 37 | + |
| 38 | +import org.springframework.boot.SpringApplication; |
| 39 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 40 | +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; |
| 41 | +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; |
| 42 | + |
| 43 | +@SpringBootApplication |
| 44 | +@EnableDiscoveryClient |
| 45 | +//@EnableEurekaClient |
| 46 | +public class SpringCloudLesson5EurekaClientApplication { |
| 47 | + |
| 48 | + public static void main(String[] args) { |
| 49 | + SpringApplication.run(SpringCloudLesson5EurekaClientApplication.class, args); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +### Eureka 服务器 |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +#### 配置高可用 Eureka 服务器 |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +##### 设置公用 Eureka 服务器配置 |
| 67 | + |
| 68 | +`application.properties`: |
| 69 | + |
| 70 | +```properties |
| 71 | +## 定义 应用名称 |
| 72 | +spring.applicaiton.name = spring-cloud-eureka-server |
| 73 | + |
| 74 | +## 管理端安全失效 |
| 75 | +management.security.enabled = false |
| 76 | + |
| 77 | +## 公用 Eureka 配置 |
| 78 | +### 向注册中心注册 |
| 79 | +eureka.client.register-with-eureka = true |
| 80 | +### 向获取注册信息(服务、实例信息) |
| 81 | +eureka.client.fetch-registry = true |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +##### 配置 Peer 1 Eureka 服务器 |
| 87 | + |
| 88 | +`application-peer1.properties`:(单机情况相当于 profile = "peer1") |
| 89 | + |
| 90 | +```properties |
| 91 | + |
| 92 | +# peer 1 完整配置 |
| 93 | + |
| 94 | +## 配置 服务器端口 |
| 95 | +## peer 1 端口 9090 |
| 96 | +server.port = 9090 |
| 97 | + |
| 98 | +## peer 2 主机:localhost , 端口 9091 |
| 99 | +peer2.server.host = localhost |
| 100 | +peer2.server.port = 9091 |
| 101 | + |
| 102 | +# Eureka 注册信息 |
| 103 | +eureka.client.serviceUrl.defaultZone = http://${peer2.server.host}:${peer2.server.port}/eureka |
| 104 | +``` |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +##### 启动 Peer 1 Eureka 服务器 |
| 109 | + |
| 110 | +通过启动参数 `—spring.profiles.active=peer1` ,相当于读取了 `application-peer1.properties `和 `application.properties` |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +##### 配置 Peer 2 Eureka 服务器 |
| 115 | + |
| 116 | +`application-peer2.properties`:(单机情况相当于 profile = "peer2") |
| 117 | + |
| 118 | +```properties |
| 119 | +# peer 2 完整配置 |
| 120 | + |
| 121 | +## 配置 服务器端口 |
| 122 | +## peer 2 端口 9091 |
| 123 | +server.port = 9091 |
| 124 | + |
| 125 | +## peer 1 主机:localhost , 端口 9090 |
| 126 | +peer1.server.host = localhost |
| 127 | +peer1.server.port = 9090 |
| 128 | + |
| 129 | +# Eureka 注册信息 |
| 130 | +eureka.client.serviceUrl.defaultZone = http://${peer1.server.host}:${peer1.server.port}/eureka |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +##### 启动 Peer 2 Eureka 服务器 |
| 136 | + |
| 137 | +通过启动参数 `—spring.profiles.active=peer2` ,相当于读取了 `application-peer2.properties `和 `application.properties` |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +### Spring Cloud Consul |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +#### 引入依赖 |
| 146 | + |
| 147 | +```xml |
| 148 | +<dependency> |
| 149 | + <groupId>org.springframework.cloud</groupId> |
| 150 | + <artifactId>spring-cloud-starter-consul-discovery</artifactId> |
| 151 | +</dependency> |
| 152 | +``` |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +#### 激活服务发现客户端 |
| 157 | + |
| 158 | +```java |
| 159 | +package com.segumentfault.springcloudlesson5consulclient; |
| 160 | + |
| 161 | +import org.springframework.boot.SpringApplication; |
| 162 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 163 | +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; |
| 164 | + |
| 165 | +@SpringBootApplication |
| 166 | +@EnableDiscoveryClient |
| 167 | +public class SpringCloudLesson5ConsulClientApplication { |
| 168 | + |
| 169 | + public static void main(String[] args) { |
| 170 | + SpringApplication.run(SpringCloudLesson5ConsulClientApplication.class, args); |
| 171 | + } |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | +#### 利用服务发现API 操作 |
| 178 | + |
| 179 | +##### 配置应用信息 |
| 180 | + |
| 181 | +```properties |
| 182 | +## 应用名称 |
| 183 | +spring.application.name = spring-cloud-consul |
| 184 | + |
| 185 | +## 服务端口 |
| 186 | +server.port = 8080 |
| 187 | + |
| 188 | +## 管理安全失效 |
| 189 | +management.security.enabled = false |
| 190 | + |
| 191 | +## 连接 Consul 服务器的配置 |
| 192 | +### Consul 主机地址 |
| 193 | +spring.cloud.consul.host = localhost |
| 194 | +### Consul 服务端口 |
| 195 | +spring.cloud.consul.port = 8500 |
| 196 | +``` |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | +##### 编写 `DiscoveryClient` Controller |
| 201 | + |
| 202 | +```java |
| 203 | +package com.segumentfault.springcloudlesson5consulclient.web.controller; |
| 204 | + |
| 205 | +import org.springframework.beans.factory.annotation.Autowired; |
| 206 | +import org.springframework.beans.factory.annotation.Value; |
| 207 | +import org.springframework.cloud.client.ServiceInstance; |
| 208 | +import org.springframework.cloud.client.discovery.DiscoveryClient; |
| 209 | +import org.springframework.web.bind.annotation.GetMapping; |
| 210 | +import org.springframework.web.bind.annotation.RestController; |
| 211 | + |
| 212 | +import java.util.LinkedList; |
| 213 | +import java.util.List; |
| 214 | + |
| 215 | +/** |
| 216 | + * {@link DiscoveryClient} {@link RestController} |
| 217 | + * |
| 218 | + * @author <a href="mailto:[email protected]">Mercy</a> |
| 219 | + * @since 1.0.0 |
| 220 | + */ |
| 221 | +@RestController |
| 222 | +public class DiscoveryClientController { |
| 223 | + |
| 224 | + |
| 225 | + private final DiscoveryClient discoveryClient; |
| 226 | + |
| 227 | + private final String currentApplicationName; |
| 228 | + |
| 229 | + @Autowired |
| 230 | + public DiscoveryClientController(DiscoveryClient discoveryClient, |
| 231 | + @Value("${spring.application.name}") String currentApplicationName) { |
| 232 | + this.discoveryClient = discoveryClient; |
| 233 | + this.currentApplicationName = currentApplicationName; |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * 获取当前应用信息 |
| 238 | + * |
| 239 | + * @return |
| 240 | + */ |
| 241 | + @GetMapping("/current/service-instance") |
| 242 | + public ServiceInstance getCurrentServiceInstance() { |
| 243 | +// return discoveryClient.getInstances(currentApplicationName).get(0); |
| 244 | + return discoveryClient.getLocalServiceInstance(); |
| 245 | + |
| 246 | + } |
| 247 | + |
| 248 | + |
| 249 | + /** |
| 250 | + * 获取所有的服务名 |
| 251 | + * |
| 252 | + * @return |
| 253 | + */ |
| 254 | + @GetMapping("/list/services") |
| 255 | + public List<String> listServices() { |
| 256 | + return discoveryClient.getServices(); |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * 获取所有的服务实例信息 |
| 261 | + * |
| 262 | + * @return |
| 263 | + */ |
| 264 | + @GetMapping("/list/service-instances") |
| 265 | + public List<ServiceInstance> listServiceInstances() { |
| 266 | + List<String> services = listServices(); |
| 267 | + List<ServiceInstance> serviceInstances = new LinkedList<>(); |
| 268 | + |
| 269 | + services.forEach(serviceName -> { |
| 270 | + serviceInstances.addAll(discoveryClient.getInstances(serviceName)); |
| 271 | + }); |
| 272 | + |
| 273 | + return serviceInstances; |
| 274 | + } |
| 275 | +} |
| 276 | +``` |
| 277 | + |
| 278 | + |
| 279 | + |
| 280 | + |
| 281 | + |
0 commit comments