Spring Cloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix),针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
目前创新互联公司已为数千家的企业提供了网站建设、域名、虚拟主机、网站托管维护、企业网站设计、龙口网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
Feign 是spring cloud全家桶一个成员,用于远程调用。
特点:声明式、模板化HTTP客户端。使远程调用,在使用时,感觉像“本地方法”
步骤一:修改pom文件,添加Feign依赖
步骤二:修改启动类,添加开启Feign注解
步骤三:编写Feign接口,完成远程调用,取代dao层
步骤四:修改controller, 将调用dao修改成feign
步骤一:修改pom文件,添加Feign依赖
org.springframework.cloud spring-cloud-starter-openfeign

步骤二:修改启动类,添加开启Feign注解
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);
}
}
步骤三:编写Feign接口,完成远程调用,取代dao层
@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 test() ;
} 步骤四:修改controller, 将调用dao修改成feign
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 data(){
//return dataDao.data();
return dataFeign.test();
}
} Spring Cloud 完成远程调用,并进行负载均衡
方式1:使用RestTemplate,并添加额外注解 @LoadBalanced
方式2:使用Feign,集成Ribbon,自动负载均衡
如果需要单独给服务配置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 # 对当前实例的重试次数
步骤一:修改yml文件,开启feign熔断机制
步骤二:创建feign接口实现类,提供备选方案
步骤三:feign调用,指定fallback确定备选方案
步骤一:修改yml文件,开启feign熔断机制
feign: hystrix: enabled: true #开启feign熔断
步骤二:创建feign接口实现类,提供备选方案
package com.czxy.feign;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
@Component
public class DataFeignFallback implements DataFeign {
@Override
public ResponseEntity test() {
return ResponseEntity.ok("feign备选方案");
}
} 
步骤三:feign调用,指定fallback确定备选方案
@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 test() ;
} 关于Spring Cloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix)问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。