ribbon 负载均衡(Feign内置负载均衡)

有点简单,以后会补充

1.1 Feign整合 负载均衡

  • SpringCloud完成远程调用,共使用2种技术
    • RestTemplate
      • 步骤一:提供配置类,并提供RestTemplate实例,同时添加注解@LoadBalanced
      • 步骤二:修改yml文件,配置ribbon负载均衡 (可选)
    • Feign ,Feign内置Ribbon
      • 步骤一:修改yml文件,配置ribbon负载均衡 (可选)

1.2 Feign整合 熔断Hystrix

  • 步骤一:修改yml文件,开启feign熔断
  • 步骤二:编写feign接口实现类
  • 步骤三:修改Feign客户端注解,声明超时错误处理回调实现类(第二步)
  • 步骤一:修改yml文件,开启feign熔断

    feign:
      hystrix:
        enabled: true   #feign开启熔断
    
  • 步骤二:编写feign接口实现类

    package com.czxy.feign;
    
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Component;
    
    /**
     * Created by liangtong.
     */
    @Component
    public class DataFeignFallback implements DataFeign {
        @Override
        public ResponseEntity<String> test() {
            return ResponseEntity.ok("Feign模拟数据");
        }
    }
    
    
  • 步骤三:修改Feign客户端注解,声明超时错误处理回调实现类(第二步)

    • @FeignClient(value=“服务名”,path=“路径前缀”,fallback = 熔断处理类.class )
    package com.czxy.feign;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    
    /**
     * Created by liangtong.
     */
    @FeignClient(value="service4",path="/test",fallback = DataFeignFallback.class)
    public interface DataFeign {
        @GetMapping
        public ResponseEntity<String> test() ;
    }
    
    
server:
 port: 9090
spring:
 application:
   name: client4
 cloud:
   loadbalancer:
     retry:
       enabled: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
service4:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule    #随机策略
    #NFLoadBalancerRuleClassName : com.netflix.loadbalancer.BestAvailableRule           #并发最少
    #NFLoadBalancerRuleClassName : com.netflix.loadbalancer.WeightedResponseTimeRule    #请求时间权重
    ConnectTimeout: 250               # Ribbon的连接超时时间
    ReadTimeout: 1000                 # Ribbon的数据读取超时时间
    OkToRetryOnAllOperations: true  # 是否对所有操作都进行重试
    MaxAutoRetriesNextServer: 1     # 切换实例的重试次数
    MaxAutoRetries: 1                # 对当前实例的重试次数
feign:
  hystrix:
    enabled: true   #feign开启熔断



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值