ZonePreferenceServerListFilter: 主动首选本地区域的过滤器 。 ServerListSubSetFilter: 服务器列表过滤器,将负载均衡器使用的服务器数量限制为所有服务器的子集 。ServerListUpdater更新服务器列表 。
- EurekaNotificationServerListUpdater: 利用Eureka的时间监听器触发LB缓存更新 。
- PollingServerListUpdater: 默认的策略动态更新服务器列表 。
IClientConfig
- IClientConfig的实现类为DefaultClientConfigImpl 。DefaultClientConfigImpl是默认的客户端配置,可以从Archaius ConfigurationManager加载属性 。
ILoadBalancerLoadBalancer的组成:
- 一个基于特定条件可能进行存储的服务器列表 。
- 一个类:通过IRule实现并定义LoadBalancing策略 。
- 该类定义并实现一种机制,用户确定列表中节点/服务器的实用性/可用性 。
LoadBalancer的实现类:
- BaseLoadBalancer: 基本的实现,ping确定存活的服务器列表 。
- DynamicServerListLoadBalancer: 动态获取的服务器列表 。
- ZoneAwareLoadBalancer: LoadBalancer将计算并检查所有可用区域的区域统计信息 。如果任何区域的“平均活动请求数”已达到配置的阈值,
则该区域将从活动服务器列表中删除 。如果多个区域已达到阈值,则将删除每台服务器上最活跃请求的区域 。一旦删除了最坏的区域,将在其余区域中选择一个区域,其概率与其实例数成正比 。服务器将从具有指定规则的选定区域返回 。每个区域相关的负载平衡决策都是在最新统计信息的帮助下实时做出的 。
代码实践pom.xml增加SpringCloud Gateway和Ribbon依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version><relativePath/> </parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency></dependencies>代码方式配置网关
@SpringBootApplication@RestControllerpublic class DemoGatewayApplication {public static void main(String[] args) {SpringApplication.run(DemoGatewayApplication.class, args);}@Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder builder) {return builder.routes().route(r -> r.path("/refund/**").filters(f -> f.addRequestHeader("Hello", "World")).uri("http://manage-test.payplatform.speiyou.cn/")).build();}}yml方式配置网关
spring:application:name: gateway-servicecloud:gateway:routes:- id: merchanturi: lb://merchant-load-balanced-servicepredicates:- Path=/merchant/**- Method=POST- id: splituri: lb://split-load-balanced-servicepredicates:- Path=/split/**filters:- RewritePath=/split, /ledger-split #重写url- id: cashieruri: lb://cashier-load-balanced-servicepredicates:- Path=/cashier/**filters:- StripPrefix=1 #将cashier过滤掉#ribbon全局配置ribbon:ConnectTimeout: 1000 #服务请求连接超时时间(毫秒)ReadTimeout: 3000 #服务请求处理超时时间(毫秒)OkToRetryOnAllOperations: true #对超时请求启用重试机制MaxAutoRetriesNextServer: 1 #切换重试实例的最大个数MaxAutoRetries: 1 # 切换实例后重试最大次数NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载均衡算法NFLoadBalancerPingClassName: com.talpay.gateway.config.HealthCheck#健康检查NFLoadBalancerPingInterval: 20#设置健康检查间隔,单位秒,默认30秒merchant-load-balanced-service:ribbon:listOfServers: 192.168.xxx.xxx:8080split-load-balanced-service:ribbon:listOfServers: 192.168.xxx.xxx:8081cashier-load-balanced-service:ribbon:listOfServers: 192.168.xxx.xxx:8080Ribbon服务健康检查
@Slf4j@Componentpublic class HealthCheck implements IPing{@Autowiredprivate RestTemplate restTemplate;@Value("${dingtalk.url}")private String dingtalkURL; //钉钉报警url@Overridepublic boolean isAlive(Server server) {String url = "http://"+ server.getId()+ "/actuator/health";try {ResponseEntity<String> heath = restTemplate.getForEntity(url, String.class);if (heath.getStatusCode() == HttpStatus.OK) {log.info("ping " + url + " success ");return true;}log.info("ping " + url + " error and response is " + heath.getBody());return false;} catch (Exception e) {log.error("ping " + url + " failed");DingRebotSendUtil.send(dingtalkURL,new TextMessage("网关|ping:" + url + " failed"));return false;}}}
推荐阅读
-
-
-
霸气的小猪佩琦|生死战23分惨案!湖人连赢4场太狠,休城面临重建
-
「久期财经」展望“稳定”,惠誉:确认蓝色光标(300058.SZ)“B+”长期本外币发行人评级
-
|84岁老爷爷参加高考,考前自估630,得知成绩后欲哭无泪
-
-
-
好声音■《好声音》导师阵容确定,没了沈腾贾玲搭档,他能适应吗?
-
利利讲快乐|东方卫视新综艺亮相,放弃《欢乐喜剧人》了吗,90%喜剧大咖聚齐
-
特斯拉|金钱在燃烧 马斯克:特斯拉柏林、德州超级工厂正亏损数十亿美元
-
开屏|杨丽萍到公园偶遇孔雀,孔雀纷纷开屏迎接,万物皆有灵!
-
-
尤文图斯|虽然来得晚些,尤文图斯仍如愿捧杯,C罗续写着他的辉煌
-
睡前常做4种行为,容易诱发失眠,做的越多,失眠的情况越严重
-
演唱会|经典音乐人:开演唱会都不用自己开口的人!全是观众在唱——伍佰
-
-
-
-
-