|
| 1 | +# Spring Cloud 负载均衡 |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Netflix Ribbon |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +### 引入Maven 依赖 |
| 14 | + |
| 15 | +```xml |
| 16 | +<dependency> |
| 17 | + <groupId>org.springframework.cloud</groupId> |
| 18 | + <artifactId>spring-cloud-starter-ribbon</artifactId> |
| 19 | +</dependency> |
| 20 | +``` |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +### 激活 Ribbon 客户端 |
| 25 | + |
| 26 | +```java |
| 27 | +package com.segumentfault.springcloudlesson6; |
| 28 | + |
| 29 | +import org.springframework.boot.SpringApplication; |
| 30 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 31 | +import org.springframework.cloud.netflix.ribbon.RibbonClient; |
| 32 | +import org.springframework.cloud.netflix.ribbon.RibbonClients; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.ComponentScan; |
| 35 | +import org.springframework.web.client.RestTemplate; |
| 36 | + |
| 37 | +@SpringBootApplication |
| 38 | +// 多个 Ribbon 定义 |
| 39 | +@RibbonClients({ |
| 40 | + @RibbonClient(name = "spring-cloud-service-provider") |
| 41 | +}) |
| 42 | +public class SpringCloudLesson6Application { |
| 43 | + |
| 44 | + public static void main(String[] args) { |
| 45 | + SpringApplication.run(SpringCloudLesson6Application.class, args); |
| 46 | + } |
| 47 | + |
| 48 | + //声明 RestTemplate |
| 49 | + @Bean |
| 50 | + public RestTemplate restTemplate(){ |
| 51 | + return new RestTemplate(); |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +### 配置 Ribbon 客户端 |
| 59 | + |
| 60 | +application.properties |
| 61 | + |
| 62 | +```properties |
| 63 | +### 配置ribbon 服务地提供方 |
| 64 | +spring-cloud-service-provider.ribbon.listOfServers = \ |
| 65 | + http://${serivce-provider.host}:${serivce-provider.port} |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +### 调整 RestTemplate |
| 71 | + |
| 72 | +```java |
| 73 | +//声明 RestTemplate |
| 74 | +@LoadBalanced // RestTemplate 的行为变化 |
| 75 | +@Bean |
| 76 | +public RestTemplate restTemplate(){ |
| 77 | + return new RestTemplate(); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +## Neflix Ribbon 整合 Eureka |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +### 激活服务发现的客户端 |
| 90 | + |
| 91 | +```java |
| 92 | +package com.segumentfault.springcloudlesson6; |
| 93 | + |
| 94 | +import org.springframework.boot.SpringApplication; |
| 95 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 96 | +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; |
| 97 | +import org.springframework.cloud.client.loadbalancer.LoadBalanced; |
| 98 | +import org.springframework.cloud.netflix.ribbon.RibbonClient; |
| 99 | +import org.springframework.cloud.netflix.ribbon.RibbonClients; |
| 100 | +import org.springframework.context.annotation.Bean; |
| 101 | +import org.springframework.context.annotation.ComponentScan; |
| 102 | +import org.springframework.web.client.RestTemplate; |
| 103 | + |
| 104 | +@SpringBootApplication |
| 105 | +// 多个 Ribbon 定义 |
| 106 | +@RibbonClients({ |
| 107 | + @RibbonClient(name = "spring-cloud-service-provider") |
| 108 | +}) |
| 109 | +@EnableDiscoveryClient // 激活服务发现客户端 |
| 110 | +public class SpringCloudLesson6Application { |
| 111 | + |
| 112 | + public static void main(String[] args) { |
| 113 | + SpringApplication.run(SpringCloudLesson6Application.class, args); |
| 114 | + } |
| 115 | + |
| 116 | + //声明 RestTemplate |
| 117 | + @LoadBalanced // RestTemplate 的行为变化 |
| 118 | + @Bean |
| 119 | + public RestTemplate restTemplate(){ |
| 120 | + return new RestTemplate(); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +``` |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +### 创建并且启动 Eureka Server |
| 129 | + |
| 130 | +以`spring-cloud-lesson6-eureka-server` 为例 |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +#### 激活 Eureka Server |
| 135 | + |
| 136 | +```java |
| 137 | +package com.segumentfault.springcloudlesson6eurekaserver; |
| 138 | + |
| 139 | +import org.springframework.boot.SpringApplication; |
| 140 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 141 | +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; |
| 142 | + |
| 143 | +@SpringBootApplication |
| 144 | +@EnableEurekaServer |
| 145 | +public class SpringCloudLesson6EurekaServerApplication { |
| 146 | + |
| 147 | + public static void main(String[] args) { |
| 148 | + SpringApplication.run(SpringCloudLesson6EurekaServerApplication.class, args); |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +#### 配置 Eureka 服务器 |
| 156 | + |
| 157 | +```properties |
| 158 | +## Eureka Serer |
| 159 | +spring.application.name = spring-cloud-eureka-server |
| 160 | + |
| 161 | +## 服务端口 |
| 162 | +server.port = 10000 |
| 163 | + |
| 164 | +## Spring Cloud Eureka 服务器作为注册中心 |
| 165 | +## 通常情况下,不需要再注册到其他注册中心去 |
| 166 | +## 同时,它也不需要获取客户端信息 |
| 167 | +### 取消向注册中心注册 |
| 168 | +eureka.client.register-with-eureka = false |
| 169 | +### 取消向注册中心获取注册信息(服务、实例信息) |
| 170 | +eureka.client.fetch-registry = false |
| 171 | +## 解决 Peer / 集群 连接问题 |
| 172 | +eureka.instance.hostname = localhost |
| 173 | +eureka.client.serviceUrl.defaultZone = http://${eureka.instance.hostname}:${server.port}/eureka |
| 174 | +``` |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +#### 启动 Eureka Server |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +### 调整 Ribbon 客户端连接 Eureka Server |
| 183 | + |
| 184 | +`applicaiont.properties` |
| 185 | + |
| 186 | +```properties |
| 187 | +## 服务提供方 |
| 188 | +spring.application.name = spring-cloud-ribbon-client |
| 189 | + |
| 190 | +### 服务端口 |
| 191 | +server.port = 8080 |
| 192 | + |
| 193 | +### 管理安全失效 |
| 194 | +management.security.enabled = false |
| 195 | + |
| 196 | +### 暂时性关闭 Eureka 注册 |
| 197 | +## 当使用 Eureka 服务发现时,请注释掉一下配置 |
| 198 | +# eureka.client.enabled = false |
| 199 | + |
| 200 | +## 连接 Eureka Sever |
| 201 | +eureka.client.serviceUrl.defaultZone = http://localhost:10000/eureka/ |
| 202 | + |
| 203 | +### 服务提供方主机 |
| 204 | +serivce-provider.host = localhost |
| 205 | +### 服务提供方端口 |
| 206 | +serivce-provider.port = 9090 |
| 207 | + |
| 208 | +serivce-provider.name = spring-cloud-service-provider |
| 209 | + |
| 210 | +### 配置ribbon 服务地提供方 |
| 211 | +## 当使用 Eureka 服务发现时,请注释掉一下配置 |
| 212 | +# spring-cloud-service-provider.ribbon.listOfServers = \ |
| 213 | + http://${serivce-provider.host}:${serivce-provider.port} |
| 214 | +``` |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | +#### 调整服务提供方并且连接 Eureka Server |
| 219 | + |
| 220 | +```properties |
| 221 | +## 服务提供方 |
| 222 | +spring.application.name = spring-cloud-service-provider |
| 223 | + |
| 224 | +### 服务端口 |
| 225 | +server.port = 9090 |
| 226 | + |
| 227 | +### 管理安全失效 |
| 228 | +management.security.enabled = false |
| 229 | + |
| 230 | +### 暂时性关闭 Eureka 注册 |
| 231 | +## 当使用 Eureka 服务发现时,请注释掉一下配置 |
| 232 | +# eureka.client.enabled = false |
| 233 | + |
| 234 | +## 连接 Eureka Sever |
| 235 | +eureka.client.serviceUrl.defaultZone = http://localhost:10000/eureka/ |
| 236 | +``` |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | +再启动两台服务提供方实例 |
| 241 | + |
| 242 | +--server.port=9091 |
| 243 | + |
| 244 | +--server.port=9092 |
| 245 | + |
| 246 | + |
| 247 | + |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | + |
| 254 | + |
| 255 | + |
| 256 | +实际请求客户端 |
| 257 | + |
| 258 | +* LoadBalancerClient |
| 259 | + * RibbonLoadBalancerClient |
| 260 | + |
| 261 | +负载均衡上下文 |
| 262 | + |
| 263 | +* LoadBalancerContext |
| 264 | + * RibbonLoadBalancerContext |
| 265 | + |
| 266 | +负载均衡器 |
| 267 | + |
| 268 | +* ILoadBalancer |
| 269 | + * BaseLoadBalancer |
| 270 | + * DynamicServerListLoadBalancer |
| 271 | + * ZoneAwareLoadBalancer |
| 272 | + * NoOpLoadBalancer |
| 273 | + |
| 274 | +负载均衡规则 |
| 275 | + |
| 276 | +核心规则接口 |
| 277 | + |
| 278 | +* IRule |
| 279 | + * 随机规则:RandomRule |
| 280 | + * 最可用规则:BestAvailableRule |
| 281 | + * 轮训规则:RoundRobinRule |
| 282 | + * 重试实现:RetryRule |
| 283 | + * 客户端配置:ClientConfigEnabledRoundRobinRule |
| 284 | + * 可用性过滤规则:AvailabilityFilteringRule |
| 285 | + * RT权重规则:WeightedResponseTimeRule |
| 286 | + * 规避区域规则:ZoneAvoidanceRule |
| 287 | + |
| 288 | +PING 策略 |
| 289 | + |
| 290 | +核心策略接口 |
| 291 | + |
| 292 | +* IPingStrategy |
| 293 | + |
| 294 | +PING 接口 |
| 295 | + |
| 296 | +* IPing |
| 297 | + * NoOpPing |
| 298 | + * DummyPing |
| 299 | + * PingConstant |
| 300 | + * PingUrl |
| 301 | + |
| 302 | +Discovery Client 实现 |
| 303 | + |
| 304 | +* NIWSDiscoveryPing |
0 commit comments