栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot简单整合Open Feign

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot简单整合Open Feign

Spring Boot简单整合Open Feign
  • 一、使用Open Feign
    • 1、引入依赖
    • 2、添加Open Feign
    • 3、添加配置文件application.yml
  • 二、Open Feign的调用
    • 1、模拟一个服务的提供者(假设为student)
    • 2、模拟一个服务的调用者(假设为classes)

一、使用Open Feign 1、引入依赖
  
       
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
            
            2.2.1.RELEASE
        
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
            
            2.2.1.RELEASE
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
            
            2.2.1.RELEASE
        
		
		 
		 
        
            io.github.openfeign
            feign-httpclient
        
		 
        
            org.apache.httpcomponents
            httpclient
        

2、添加Open Feign

在项目的启动类上添加Open Feign的启动注解==@EnableFeignClients和服务熔断的注解@EnableHystrix==
例如,第三行第四行

@SpringBootApplication()
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class ClassesApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClassesApplication.class, args);
    }
}
3、添加配置文件application.yml

在服务调用方配置文件如下

#feign客户端配置
feign:
  #设置feign开启hystrix
  hystrix:
    enabled: true
  #适配feign发送get请求
  httpclient:
    enabled: true
#ribbon配置
ribbon:
  #ribbon的超时时间要大于hystrix的超时时间,否则 hystrix自定义的超时时间毫无意义
  ReadTimeout: 5000
  ConnectTimeout: 5000
#hystrix配置
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            #feign整合hystrix 光设置Hystrix 超时没用的要配合ribbon超时
            timeoutInMilliseconds: 3000
      circuitBreaker:
        #默认20 ,熔断的阈值,如何user服务报错满足3次,熔断器就会打开,就算order之后请求正确的数据也不行。
        requestVolumeThreshold: 3
        #默认5S , 等5S之后熔断器会处于半开状态,然后下一次请求的正确和错误讲决定熔断器是否真的关闭和是否继续打开
        sleepWindowInMilliseconds: 8000
二、Open Feign的调用

假设当前场景有班级和学生两个对象,两个对象分别在ClassesApplication和SudentApplication两个服务中,现要求学生服务为班级服务提供查询所有学生的接口

1、模拟一个服务的提供者(假设为student)

1、学生服务的启动类,服务地址为localhost:9001,服务名称为service-student

@SpringBootApplication()
@EnableDiscoveryClient
@EnableFeignClients
public class SudentApplication{
    public static void main(String[] args) {
        SpringApplication.run(SudentApplication.class, args);
}

2、学生服务查询所有学生的接口

@RestController
@RequestMapping(value = "/api/v1/student")
public class StudentController{

	//假设为学生接口服务层
	@Autowired
	private StudentService studentService;
	
	//假设为查询所有学生的接口
	@RequestMapping(value = "/list", method = RequestMethod.GET)
    public List findAll() {
       return student.findAll();
    }
}
2、模拟一个服务的调用者(假设为classes)

1、班级服务的启动类,服务地址为localhost:9002,服务名称为service-classes

@SpringBootApplication()
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class ClassesApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClassesApplication.class, args);
    }
}

2、创建服务调用service

@FeignClient(name = "service-student", url = "localhost:9001/api/v1/student", fallback = "StudentFeignFallBack.class)
@Componet
public interface StudentFeignService{
	//复制StudentController的查询所有学生的接口
	@RequestMapping(value = "/list", method = RequestMethod.GET)
    public List findAll();
}

@FeignClient即是我们常说的Feign注释,其中,name为调用的服务的服务名称,url为调用服务的服务地址,fallback为调用StudentFeignService方法失败后用来兜底的类

3、创建服务熔断类(兜底)

@Component
public class StudentFeignFallBack implements StudentFeignService{
	 
    private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	 public List findAll(){
		logger.error("获取学生列表失败", e);
		retun null;
	}
}

兜底类实现了调用类的方法,当Feign调用不通时就会执行兜底类的兜底方法。
自此,Spring Boot简单整合Open Feig告一段落。


参考:springboot整合hystrix和feign

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/583941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号