有点简单,以后会补充
1.1 Feign整合 负载均衡
- SpringCloud完成远程调用,共使用2种技术
- RestTemplate
- 步骤一:提供配置类,并提供RestTemplate实例,同时添加注解@LoadBalanced
- 步骤二:修改yml文件,配置ribbon负载均衡 (可选)
- Feign ,Feign内置Ribbon
- 步骤一:修改yml文件,配置ribbon负载均衡 (可选)
- RestTemplate
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开启熔断