Spring Cloud (学习四)----(Feign(远程调用)+整合负载均衡Ribbon--熔断器Hystrix)

空扰寡人 提交于 2020-01-07 03:50:31

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

什么是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>

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

  • 步骤三: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() ;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!