spring clund feign 负载均衡与ribbon 负载均衡 使用手册

本文描述在没有时用Eruka 注册中心如何使用ribbon ,feign分别进行负载均衡的使用方法,文中有个服务

http://localhost:8081/ribbon/hello

分别输出

{"id":null,"username":"8081usename","name":"8081name","age":1,"balance":1}

http://localhost:8084/ribbon/hello

{"id":null,"username":"8084usename","name":"8084name","age":1,"balance":1}

代码在

https://gitee.com/chinadeng/ribbontest.git

https://gitee.com/chinadeng/proxytest.git

一,ribbon 负载均衡

1.在application.properties 配置ribbon 获取服务地址

#配置Ribbon能访问 的微服务节点,多个节点用逗号隔开
microservice-provider-user.ribbon.listOfServers=localhost:8081,localhost:8084

2 配置ribbon 策略,使用 RestTemplate 进行http请求

 

@Configuration
public  class FooConfiguration {
   @Bean
   public ZonePreferenceServerListFilter serverListFilter() {
      ZonePreferenceServerListFilter filter = new ZonePreferenceServerListFilter();
      filter.setZone("myTestZone");
      return filter;
   }
 
   @Bean
   public IPing ribbonPing() {
      return new PingUrl();
   }
//
   @Bean
   @LoadBalanced
   public RestTemplate restTemplate() {
      return new RestTemplate();
   }
}

3. 在相应请求中注入RestTemplate 实列进行请求

User use =this.restTemplate.getForObject(
                "http://microservice-provider-user/ribbon/hello",
                User.class);*/

这是请求 尼就会发现可以负载均衡了,客户端自动路由了

二 使用feign 进行请求. 

1.在application.properties 配置ribbon 获取服务地址

#配置Ribbon能访问 的微服务节点,多个节点用逗号隔开
microservice-provider-user.ribbon.listOfServers=localhost:8081,localhost:8084

2.配置feign 接口

@FeignClient(value = "person-service")
public interface PersonClient {
    @RequestMapping(method = RequestMethod.POST, value = "/person")
    String person(@RequestParam(value = "firstname") String firstname, @RequestParam(value = "lastname") String lastname);
}

接入完毕,你会发现完全可以自动路由了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值