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;}}}
推荐阅读
-
-
泡菜■家里种的桔梗盆栽还能做泡菜?饭店超受欢迎,咱自己也能做!
-
游戏的使者TB:AG梦泪排第一,寂然超越剑仙,第二有些意外了,国服四大野王
-
-
明星八卦|吉娜郎朗婚礼太甜蜜,镜头一扫而过,周杰伦昆凌同框我没眼花吧?
-
大学|大学里面含金量高的4个专业,一个比一个吃香,毕业不愁找工作
-
华为|500元档真无线全能王者 专为年轻人打造的华为FreeBuds 5i来了
-
大众新闻不必等到618,6月6日1MORE耳机爆品直降
-
失踪女子家属蹲化粪池井盖旁大哭|白发人送黑发人!失踪女子家属蹲化粪池井盖旁大哭,背后真相到底是什么?
-
中国新闻网|8月英仙座流星雨将亮相,观测指南来了!
-
诸葛小彻|美本土又传出一个坏消息,军方下达死命令,一夜之间损失40亿之后
-
:什么藤结什么瓜:大金虎3月有“大囍”指引,必定后福无穷
-
-
-
-
新车|奇瑞改装车要量产?艾瑞泽GX平台打造,网友:奇瑞版兰博基尼
-
東哥说体育|超级外援加三国手阵容,广东队在CBA已没有对手?,深析:24连胜
-
cnBeta微软研究人员攻克了两个已有20年历史的量子计算问题
-
「挖贝网」持有公司64.28%股份,ST量子花任命恽为民为公司董事长兼总经理
-