Ribbon 学习应用

感觉没啥说的,源码还没看,基本浅显的原理估计大家都知道,所直接来个应用吧。

1 在服务程序调用端加入ribbon依赖

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

2 使用 RestTemplate 进行程序的调用,创建一个返回配置类

@Configuration
public class BeanConfigration {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }

}

3、 使用RestTemplate进行程序的调用

@RestController
@RequestMapping
public class TestController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("ckientOneName")
    public String getClinetOneProjectName(){
        for(int i = 0; i <10; i++){
            String str=restTemplate.getForObject("http://client03/test/projectName",String.class);
            System.out.println(str);
        }

        return "ok";
    }

}

4 执行结果

client03:8912
client03:8911
client03:8912
client03:8911
client03:8912
client03:8911
client03:8912
client03:8911
client03:8912
client03:8911

5、 开启饥饿加载, 这个主要目的是,解决 当服务过多、网络不好的情况下,第一次加载出现超时的问题。

ribbon:
  eager-load:
    enabled: true    #开启饥饿加载
    clients: client03 # 开启饥饿加载的服务

出现一下代码就表示开启了:

c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client client03 initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=client03,current list of Servers=[10.3.135.143:8911, 10.3.135.143:8912],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;	Instance count:2;	Active connections count: 0;	Circuit breaker tripped count: 0;	Active connections per server: 0.0;]
},Server stats: [[Server:10.3.135.143:8912;	Zone:defaultZone;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
, [Server:10.3.135.143:8911;	Zone:defaultZone;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@1511d157

6、 注意在第三步中,我们采取的方法是getForObject,而且没有传递参数,其实通过方法的名称就知道, 我们采用的get的方式进行请求,所以在传参的时候有两种形式,一个是拼接,一个是restfu风格的url 后直接 传递参数。 同样,当服务需要很多的参数的时候,我们可以使用post方式进行请求, 用postForObject,没啥可讲的,自己一看就会。

7、 选用负载均衡策略,默认的轮询机制,现改为  随机机制,在 配置文件中加入一下配置:

client03: #要调用的服务名称
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
client03:8911
client03:8912
client03:8911
client03:8911
client03:8911
client03:8911
client03:8911
client03:8912
client03:8911
client03:8912

8、 也可以通过代码进行配置,思路是创建一个配置类,用于配置 负载均衡的策略, 如下:

@Configuration
public class RibbonRuleStrategy{

    @Bean
    public Rule rule(){
    
        return new Rule();
    }

}

在启动类上加上如下注解:

@RibbonClient(name="服务名称",configuration="刚刚的配置类")
public class RibbonClientConfig{
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值