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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Ribbon-2通过代码自定义配置ribbon

發(fā)布時間:2024/4/13 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ribbon-2通过代码自定义配置ribbon 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我們看怎么去定制Ribbon Client他的配置https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html6.2 Customizing the Ribbon Client@RibbonClient(name = "microservice-simple-provider-user", configuration = TestConfiguration.class)spring.application.name=microservice-simple-provider-user這個一定要是注冊的名稱,這個有一個警告The CustomConfiguration clas must be a @Configuration class, but take care that it is not in a @ComponentScan for the main application context. Otherwise, it is shared by all the @RibbonClients. If you use @ComponentScan (or @SpringBootApplication), you need to take steps to avoid it being included (for instance, you can put it in a separate, non-overlapping package or specify the packages to scan explicitly in the @ComponentScan)./*** A custom <code>@Configuration</code> for the ribbon client. Can contain override* <code>@Bean</code> definition for the pieces that make up the client, for instance* {@link ILoadBalancer}, {@link ServerListFilter}, {@link IRule}.** @see RibbonClientConfiguration for the defaults*/ Class<?>[] configuration() default {};@Bean @ConditionalOnMissingBean public IRule ribbonRule(IClientConfig config) {if (this.propertiesFactory.isSet(IRule.class, name)) {return this.propertiesFactory.get(IRule.class, config, name);}ZoneAvoidanceRule rule = new ZoneAvoidanceRule();rule.initWithNiwsConfig(config);return rule; }@RibbonClient(name = "microservice-simple-provider-user", configuration = TestConfiguration.class) public class TestConfiguration {// @Autowired// IClientConfig config;@Beanpublic IRule ribbonRule() {return new RandomRule();}}這樣指定的就是隨機的規(guī)則了localhost: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-simple-consumer-movie</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><dependencyManagement><dependencies><dependency> <!-- 進行SpringCloud依賴包的導(dǎo)入處理 --><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency><dependency> <!-- SpringCloud離不開SpringBoot,所以必須要配置此依賴包 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.12.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><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-simple-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=6379 package com.learn.cloud;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule;@Configuration @ExcludeFromComponentScan public class TestConfiguration {// @Autowired// IClientConfig config;@Beanpublic IRule ribbonRule() {return new RandomRule();}} package com.learn.cloud;public @interface ExcludeFromComponentScan {} 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.cloud.netflix.ribbon.RibbonClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; import org.springframework.web.client.RestTemplate;@EnableEurekaClient @SpringBootApplication @RibbonClient(name = "microservice-simple-provider-user", configuration = TestConfiguration.class) @ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = ExcludeFromComponentScan.class) }) public class MicroserviceSimpleConsumerMovieApplication {@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args);} } 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 Heartbeatreturn 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";}}

?

總結(jié)

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

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