400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

SpringCloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix)

Spring Cloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix),针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

目前创新互联公司已为数千家的企业提供了网站建设、域名、虚拟主机、网站托管维护、企业网站设计、龙口网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

什么是Feign

Feign 入门

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

Spring Cloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix)

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);
}
}

Spring Cloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix)

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

Spring Cloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix)

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() ;
}
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();
    }
}

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 test() {
        return ResponseEntity.ok("feign备选方案");
    }
}

Spring Cloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix)

@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)问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


分享标题:SpringCloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix)
当前网址:http://mbwzsj.com/article/pcggij.html

其他资讯

让你的专属顾问为你服务