我们看怎么去定制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();}}这样指定的就是随机的规则了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依赖包的导入处理 --><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";}}

Ribbon-2通过代码自定义配置ribbon相关推荐

  1. 自定义配置节与配置节的读取

    一.引子 你是否也遇到过这样的问题:项目很多配置都写到了App.Config或Web.Config的AppSettings内,每个人都加了几条,到最后囤积了大量的配置,分不清哪个是有用的.哪个是没用的 ...

  2. SpringCloud:Ribbon负载均衡(基本使用、 负载均衡、自定义配置、禁用 Eureka 实现 Ribbon 调用)

    现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现在的问题,所有的微服务汇集到了 Eureka 之 ...

  3. SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)...

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册 ...

  4. ribbon源码分析之自定义配置、全局配置

    在上一文EnableDiscoveryClient没用了?Zookeeper是怎么和springboot配合做服务注册中心的?讲过了zk是怎么做服务注册和服务发现的,同时在spring.factori ...

  5. Ribbon自定义配置--RibbonClientSpecification

    目录 NamedContextFactory SpringClientFactory RibbonClientConfiguration PropertiesFactory RibbonClientS ...

  6. logback日志pattern_Logback日志基础及自定义配置代码实例

    Logback日志基础配置 logback日志配置有很多介绍,但是有几个非常基础的,容易忽略的.下面是最简单的一个配置,注意加粗的描述 ${log.path}/errorlog.log %d{yyyy ...

  7. 自定义报错返回_Spring Cloud Feign的使用和自定义配置

    在上一篇文章 null:Spring Cloud 自定义Eureka Ribbon负载均衡策略​zhuanlan.zhihu.com 中,我们使用Ribbon自定义的策略实现了负载均衡,接下来我们介绍 ...

  8. Spring Cloud Alibaba - 14 OpenFeign自定义配置 + 调用优化 + 超时时间

    文章目录 打印Feign调用日志 日志级别 三部曲 step1 添加Feign的自定义配置 step2 声明式接口指定配置 Step3 声明式接口包日志级别调整为DEBUG 验证 基于yml文件细粒度 ...

  9. VS Code 安装插件、自定义模板、自定义配置参数、自定义主题、配置参数说明、常用的扩展插件

    1. 下载和官网教程 下载地址:https://code.visualstudio.com/ 官方教程:https://code.visualstudio.com/docs 2. 安装插件 安装扩展插 ...

最新文章

  1. 【PM模块】外包服务、工作清场管理、预防性维护
  2. 软件开发中部分代码的注解
  3. Android NDK调试定位错误
  4. 通向架构师的道路(第七天)之漫谈使用ThreadLocal改进你的层次的划分
  5. createprocess失败代码2_极客战记[森林]:边地之叉-通关代码及讲解
  6. java 优酷视频缩略图_java获取优酷等视频缩略图
  7. mybatis 配置 mysql连接池_spring 5.x 系列第5篇 —— 整合 mybatis + druid 连接池 (xml配置方式)...
  8. 二级高级应用计算机考试环境,1.2 上机考试环境免费阅读_全国计算机等级考试无纸化真考题库二级MS Office高级应用免费全文_百度阅读...
  9. python怎么测试uwsgi并发量_nginx + uWSGI 为 django 提供高并发
  10. 国家广电总局:立即停播“椰树牌椰汁”等部分版本广告
  11. PAPI性能测试工具的安装、使用及实例
  12. 土建中级工程师考试用书电子版_建筑工程中级职称考试试卷教学教材
  13. 5369. 统计作战单位数
  14. 一文曝光字节跳动薪资职级,资深开发的收入你意想不到~
  15. 民宿平台airbnb是如何动态定价的
  16. kettle中报org.gjt.mm.mysql.Driver 解决办法
  17. 微信二维码扫一扫的实现
  18. nexues vpc 角色切换,引起staick 重置引起业务中断问题
  19. vue $route及$router的区分
  20. 信息在计算机中的表示(一)

热门文章

  1. [Leetcode] Binary Tree Maximum Path Sum
  2. OnPaint()函数的作用原理
  3. ORACLE多表查询优化(引)
  4. matlab函数每天进步一点点
  5. base64文件上传后台处理
  6. 薛老师软考高项学员:2016年4月6日作业
  7. Linux SendMail服务启动慢总结
  8. HMAILSERVER集成WEB邮件系统(ROUNDCUBE WEBMAIL)
  9. [跟我一起涨姿势]未注册服务的RHEL6.4使用网易的CentOS源
  10. 链路层的网卡聚合-基于Linux bonding