Spring Cloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix),针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
什么是Feign
Feign 入门
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix //开启熔断器
@EnableFeignClients //开启Feign客户端
public class Client4Application {
public static void main(String[] args) {
SpringApplication.run(Client4Application.class,args);
}
}

@FeignClient(value="服务名",path="controller前缀")
public interface 接口名{
//与controller方法一致
}

package com.czxy.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
@FeignClient(value="service4",path="/test")
public interface DataFeign {
@GetMapping
public ResponseEntity<String> test() ;
}
package com.czxy.controller;
import com.czxy.dao.DataDao;
import com.czxy.feign.DataFeign;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/data")
public class DataController {
@Resource
//private DataDao dataDao;
private DataFeign dataFeign;
@GetMapping
public ResponseEntity<String> data(){
//return dataDao.data();
return dataFeign.test();
}
}
Feign 整合 负载均衡Ribbon
#负载均衡器策略配置
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
feign:
hystrix:
enabled: true #开启feign熔断
package com.czxy.feign;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
@Component
public class DataFeignFallback implements DataFeign {
@Override
public ResponseEntity<String> test() {
return ResponseEntity.ok("feign备选方案");
}
}

@FeignClient(value="服务名",path="前缀路径",fallback=备选方案类.class)
public interface 接口名 {
package com.czxy.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
@FeignClient(value="service4",path="/test",fallback=DataFeignFallback.class)
public interface DataFeign {
@GetMapping
public ResponseEntity<String> test() ;
}
关于Spring Cloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix)问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注天达云行业资讯频道了解更多相关知识。