日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Ribbon-3使用配置文件自定义Ribbon Client

發(fā)布時間:2024/4/13 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ribbon-3使用配置文件自定义Ribbon Client 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我們討論一下怎么使用配置文件,這是從SpringCloud Netflix1.0開始的,6.4 Customizing the Ribbon Client by Setting Propertieshttps://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.htmlStarting with version 1.2.0, Spring Cloud Netflix now supports customizing Ribbon clients by setting properties to be compatible with the Ribbon documentation.This lets you change behavior at start up time in different environments.The following list shows the supported properties>:<clientName>.ribbon.NFLoadBalancerClassName: Should implement ILoadBalancer<clientName>.ribbon.NFLoadBalancerRuleClassName: Should implement IRule<clientName>.ribbon.NFLoadBalancerPingClassName: Should implement IPing<clientName>.ribbon.NIWSServerListClassName: Should implement ServerList<clientName>.ribbon.NIWSServerListFilterClassName: Should implement ServerListFilterclientName是我要請求的serviceId,這邊有一個注意點(diǎn)Classes defined in these properties have precedence over beans defined by using @RibbonClient(configuration=MyRibbonConfig.class) and the defaults provided by Spring Cloud Netflix.這個配置文件里面定義的類,用JAVA代碼的優(yōu)先級要高,同時高于Spring Cloud Netflix的默認(rèn)配置,也就是優(yōu)先級配置文件NO.1,java代碼No2,默認(rèn)的配置No3,這邊給了一個demo,如果你想配置IRULE的話To set the IRule for a service name called users, you could set the following properties:負(fù)載均衡的規(guī)則的話,你可以這么玩users:ribbon:NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerListNFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule非常簡單,/*** A loadbalacing strategy that randomly distributes traffic amongst existing* servers.* * @author stonse* */ public class RandomRule extends AbstractLoadBalancerRule {根據(jù)響應(yīng)時間加權(quán),這個是加權(quán),不是優(yōu)先級,/** * Rule that use the average/percentile response times* to assign dynamic "weights" per Server which is then used in * the "Weighted Round Robin" fashion. * <p>* The basic idea for weighted round robin has been obtained from JCS* The implementation for choosing the endpoint from the list of endpoints* is as follows:Let's assume 4 endpoints:A(wt=10), B(wt=30), C(wt=40), * D(wt=20). * <p>* Using the Random API, generate a random number between 1 and10+30+40+20.* Let's assume that the above list is randomized. Based on the weights, we* have intervals as follows:* <p>* 1-----10 (A's weight)* <br>* 11----40 (A's weight + B's weight)* <br>* 41----80 (A's weight + B's weight + C's weight)* <br>* 81----100(A's weight + B's weight + C's weight + C's weight)* <p>* Here's the psuedo code for deciding where to send the request:* <p>* if (random_number between 1 &amp; 10) {send request to A;}* <br>* else if (random_number between 11 &amp; 40) {send request to B;}* <br>* else if (random_number between 41 &amp; 80) {send request to C;}* <br>* else if (random_number between 81 &amp; 100) {send request to D;}* <p>* When there is not enough statistics gathered for the servers, this rule* will fall back to use {@link RoundRobinRule}. * @author stonse*/ public class WeightedResponseTimeRule extends RoundRobinRule {其實(shí)Ribbon支持各種各樣的rule,重試的Rule,* @deprecated Use {@link WeightedResponseTimeRule}* * @see WeightedResponseTimeRule* */ public class ResponseTimeWeightedRule extends RoundRobinRule {這兩個其實(shí)是一樣的,只不過之前的名稱取得不好,現(xiàn)在改名了,see WeightedResponseTimeRule,我們可以看一下IRURE的實(shí)現(xiàn)類,響應(yīng)時間加權(quán)的rule,RoundRobinRule,是繼承了RoundRobinRule,還有重試的Rule,RandomRule,最高可用性的Rule,BestAvaliableRule,/*** A rule that skips servers with "tripped" circuit breaker and picks the* server with lowest concurrent requests.* <p>* This rule should typically work with {@link ServerListSubsetFilter} which puts a limit on the * servers that is visible to the rule. This ensure that it only needs to find the minimal * concurrent requests among a small number of servers. Also, each client will get a random list of * servers which avoids the problem that one server with the lowest concurrent requests is * chosen by a large number of clients and immediately gets overwhelmed.* * @author awang**/ public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule {我很難做到一個節(jié)點(diǎn)比另一個節(jié)點(diǎn)響應(yīng)時間長,所以我還是使用RandomRule,microservice-simple-provider-user.ribbon.NFLoadBalancerRuleClassName= com.netflix.loadbalancer.RandomRule隨機(jī)的規(guī)則去負(fù)載均衡localhost:8010/movie/1localhost:8010/test <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>microservice-consumer-movie-ribbon-properties-customizing</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>microservice-simple-consumer-movie</name><description>Demo project for Spring Boot</description><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project> #debug=true server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=microservice-consumer-movie-ribbon eureka.instance.prefer-ip-address=true eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} eureka.client.healthcheck.enabled=true spring.redis.host=10.40.8.152 spring.redis.password=1234 spring.redis.port=6379microservice-simple-provider-user.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule package com.learn.cloud.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;import com.learn.cloud.entity.User;@RestController public class MovieController {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate LoadBalancerClient loadBalancerClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {// http://localhost:7900/simple/// VIP virtual IP// HAProxy HeartbeatServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-simple-provider-user");System.out.println("==============" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());return this.restTemplate.getForObject("http://microservice-simple-provider-user/simple/" + id, User.class);}@GetMapping("/test")public String test() { // ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-simple-provider-user"); // System.out.println("111" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("microservice-simple-provider-user2");System.out.println("222" + ":" + serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":" + serviceInstance2.getPort());return "1";}} package com.learn.cloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;@SpringBootApplication @EnableEurekaClient public class ConsumerMovieRibbonApplication {@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(ConsumerMovieRibbonApplication.class, args);} }

?

總結(jié)

以上是生活随笔為你收集整理的Ribbon-3使用配置文件自定义Ribbon Client的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。