温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-10-21 13:54:36 来源:亿速云 阅读:162 作者:柒染 栏目:大数据

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

什么是Feign

  • Feign 是spring cloud全家桶一个成员,用于远程调用。

  • 特点:声明式、模板化HTTP客户端。使远程调用,在使用时,感觉像“本地方法”

Feign 入门

  • 步骤一:修改pom文件,添加Feign依赖

  • 步骤二:修改启动类,添加开启Feign注解

  • 步骤三:编写Feign接口,完成远程调用,取代dao层

  • 步骤四:修改controller, 将调用dao修改成feign

  • 步骤一:修改pom文件,添加Feign依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

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

  • 步骤二:修改启动类,添加开启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);
}
}

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

  • 步骤三:编写Feign接口,完成远程调用,取代dao层

@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<String> 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<String> data(){
        //return dataDao.data();
        return dataFeign.test();
    }
}

Feign 整合 负载均衡Ribbon

  • 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                 # 对当前实例的重试次数

Feign 整合 熔断器 Hystrix

  • 步骤一:修改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<String> test() {
        return ResponseEntity.ok("feign备选方案");
    }
}

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

  • 步骤三: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<String> test() ;
}

关于Spring Cloud如何远程调用Feign和整合负载均衡Ribbon熔断器Hystrix)问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI