美文网首页
2018-06-23

2018-06-23

作者: 毛子果 | 来源:发表于2018-06-23 15:58 被阅读0次

Ribbon--学习笔记(2)



目录

一、参考spring cloud的官方文档
--1、客户端负载平衡器:Ribbon
--2、如何加入Ribbon
--3、自定义Ribbon客户端
--4、使用属性自定义Ribbon客户端
--5、如何使用Ribbon不使用Eureka
--6、在Ribbon中禁用Eureka使用
二、实操
--1、在这个示例中有三个角色:注册中心、服务提供方、服务消费方
--2、消费方是如何配置的
--3、启动的效果
--4、原理图



一、参考spring cloud的官方文档


1、客户端负载平衡器:Ribbon

ribbon简介.PNG

Ribbon是一个客户端负载均衡器,它可以很好地控制HTTP和TCP客户端的行为。Feign已经使用Ribbon,所以如果您使用@FeignClient,则本节也适用。

Ribbon中的中心概念是指定客户端的概念。每个负载平衡器是组合的组合的一部分,它们一起工作以根据需要联系远程服务器,并且集合具有您将其作为应用程序开发人员(例如使用@FeignClient注释)的名称。Spring Cloud使用RibbonClientConfiguration为每个命名的客户端根据需要创建一个新的合奏作为ApplicationContext。这包含(除其他外)ILoadBalancerRestClientServerListFilter

2、如何加入Ribbon

要在项目中包含Ribbon,请使用组org.springframework.cloud和工件ID spring-cloud-starter-ribbon的起始器。

3、自定义Ribbon客户端

您可以使用<client>.ribbon.*中的外部属性来配置Ribbon客户端的某些位,这与使用Netflix API本身没有什么不同,只能使用Spring Boot配置文件。本机选项可以在CommonClientConfigKey(功能区内核心部分)中作为静态字段进行检查。

Spring Cloud还允许您通过使用@RibbonClient声明其他配置(位于RibbonClientConfiguration之上)来完全控制客户端。例:

@Configuration
@RibbonClient(name = "foo", configuration = FooConfiguration.class)
public class TestConfiguration {
}

在这种情况下,客户端由RibbonClientConfiguration中已经存在的组件与FooConfiguration中的任何组件组成(后者通常会覆盖前者)。

警告:
FooConfiguration必须是@Configuration,但请注意,它不在主应用程序上下文的@ComponentScan中,否则将由所有@RibbonClients共享。如果您使用@ComponentScan(或@SpringBootApplication),则需要采取措施避免包含(例如将其放在一个单独的,不重叠的包中,或者指定要在@ComponentScan)。

Spring Cloud Netflix默认情况下为Ribbon(BeanType beanName:ClassName)提供以下bean:

  • IClientConfig ribbonClientConfig:DefaultClientConfigImpl

  • IRule ribbonRule:ZoneAvoidanceRule

  • IPing ribbonPing:NoOpPing

  • ServerList<Server> ribbonServerList:ConfigurationBasedServerList

  • ServerListFilter<Server> ribbonServerListFilter:ZonePreferenceServerListFilter

  • ILoadBalancer ribbonLoadBalancer:ZoneAwareLoadBalancer

  • ServerListUpdater ribbonServerListUpdater:PollingServerListUpdater

创建一个类型的bean并将其放置在@RibbonClient配置(例如上面的FooConfiguration)中)允许您覆盖所描述的每个bean。例:

@Configuration
public class FooConfiguration {
    @Bean
    public IPing ribbonPing(IClientConfig config) {
        return new PingUrl();
    }
}

这用PingUrl代替NoOpPing

4、使用属性自定义Ribbon客户端

从版本1.2.0开始,Spring Cloud Netflix现在支持使用属性与Ribbon文档兼容来自定义Ribbon客户端。

这允许您在不同环境中更改启动时的行为。

支持的属性如下所示,应以<clientName>.ribbon.为前缀:

  • NFLoadBalancerClassName:应实施ILoadBalancer

  • NFLoadBalancerRuleClassName:应实施IRule

  • NFLoadBalancerPingClassName:应实施IPing

  • NIWSServerListClassName:应实施ServerList

  • NIWSServerListFilterClassName应实施ServerListFilter

注意:
在这些属性中定义的类优先于使用@RibbonClient(configuration=MyRibbonConfig.class)定义的bean和由Spring Cloud Netflix提供的默认值。

要设置服务名称usersIRule,您可以设置以下内容:

application.yml

users:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule

5、如何使用Ribbon不使用Eureka

Eureka是一种方便的方式来抽象远程服务器的发现,因此您不必在客户端中对其URL进行硬编码,但如果您不想使用它,Ribbon和Feign仍然是适用的。假设您已经为“商店”申请了@RibbonClient,并且Eureka未被使用(甚至不在类路径上)。Ribbon客户端默认为已配置的服务器列表,您可以提供这样的配置

application.yml

stores:
  ribbon:
    listOfServers: example.com,google.com

6、在Ribbon中禁用Eureka使用

设置属性ribbon.eureka.enabled = false将明确禁用在Ribbon中使用Eureka。

application.yml

ribbon:
  eureka:
   enabled: false

二、实操


1、在这个示例中有三个角色:注册中心、服务提供方、服务消费方

提供者-注册中心-消费者.PNG

2、消费方是如何配置的

pom.xml

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

server:
  port: 8082

spring:
  application:
    name: ribbon-demo-consumer

启动类

@SpringBootApplication
public class RibbonDemoApplication {

    @Bean
    @LoadBalanced   //负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(RibbonDemoApplication.class, args);
    }
}

调用类

@RestController
@RequestMapping("/call")
public class CallController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello")
    public String hello(){
        return restTemplate.getForObject("http://EUREKA-DEMO-CLIENT-1/",String.class);
    }

    @GetMapping("/hi/{name}")
    public String hi(@PathVariable("name") String name){
        return restTemplate.getForObject("http://EUREKA-DEMO-CLIENT-1/test/say/hello/" + name,String.class);
    }
}

Ps:提供者(eureka-demo-client)的调用类参考

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("say/hello/{name}")
    public String sayHello(@PathVariable("name") String name ){
        return "hello " + name;
    }
}

3、启动的效果

UI界面显示了提供者和消费者

监控.PNG

浏览器中输入http://localhost:8082/call/hello/

ribbon-result1.PNG

浏览器中输入http://localhost:8082/call/hi/caowo

ribbon-result2.PNG

4、原理图

发布订阅.PNG

相关文章

  • 华为

    华为公司有那些部门 数据大学 2018-06-23 11:04 · 字数 1501 · 阅读 2 · 日记本 1、...

  • 日精进打卡(第351天)

    2018-06-23 姓名:李义 公司:........ 组别:259期利他二组 【知~学习】 背诵 六项精进大纲...

  • 世上好人那么少,不要随便对人推心置腹

    对人推心置腹 薇薇姐pmm 2018-06-23 18:48 · 字数 1123 · 阅读 0 · 日记本 世上好...

  • 面朝大海的博客

    多点客观,多点敬畏,多点爱;少点责问,少点功利,少点私。 2018-06-23 13:49阅读:702 最近,时不...

  • 2018-06-24

    《番茄工作法图解读》后感 晓春的蜗居 2018-06-23 22:35 · 字数 272 · 阅读 1 · 日记本...

  • 2018-06-23

    2018-06-23 字数 476· 阅读 114· 日记本 姓名:周富强 公司:厦门大科机械有限公司 日精进打卡...

  • 电影记录

    碟中谍6:全面瓦解 2018-09-15 我不是妖神 208-07-17 超人总动员2 2018-06-23 头...

  • 投射+感赏226感觉源点丰盛与否决定事情成败

    曾婧_六中换玩 字数 2045 · 阅读 517 2018-06-23 23:52 知道儿子近期情绪不是很好,我主...

  • 2018-06-23

    2018-06-23 姓名:苏晶晶 单位:中天建设集团有限公司万科小镇项目部 【日精进打卡第20天】 【知~学习】...

  • 红魔冲击连胜 卢卡库破纪录

    太子体育太子体育今天 世界杯:比利时(中)VS突尼斯 开赛时间:2018-06-23 20:00 杯赛积分排名: ...

网友评论

      本文标题:2018-06-23

      本文链接:https://www.haomeiwen.com/subject/iznryftx.html