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

Spring Cloud OpenFeign

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

Spring Cloud OpenFeign

Spring Cloud OpenFeign整合了Spring Cloud Ribbon、Spring Cloud Hystrix,同时还实现重试机制。

maven依赖

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

例子

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {

    public static void main(String[] args){
        SpringApplication.run(ConsumerApplication.class, args);
    }
}
@FeignClient(value = "hello-service")
public interface HelloWorld {

    @RequestMapping("/hello")
    String remoteSayHello();

    @PostMapping("/printAddr")
    String remotePrintAddr(@RequestParam("province") String provinceName, @RequestParam("city") Long cityName);

    @GetMapping("/get-one-student/{id}")
    StudentBO remoteGetStudent(@PathVariable("id") Long id);
}
@RestController
public class ConsumerController {

    @Autowired
    private HelloWorld helloWorld;

    @GetMapping(value="/remoteSayHello")
    public String remoteSayHello() {
        return helloWorld.remoteSayHello();
    }
}

使用@EnableFeignClients 开启Spring Cloud OpenFeign
使用@FeignClient 声明服务

@FeignClient的常用属性
value: 指定服务名,不区分大小写
path: 指定请求路径的前缀
url: 指定请求的绝对路径,不会使用负载均衡,直连,本地调试时或其它不需要负载均衡的场景下可使用
configuration: 为单个服务指定配置(注:当同一个服务有多个FeignClient时,configuration配置可能无法生效,需要注意版本)

FeignClientsConfiguration 是Feign默认的全局配置

 	@Bean
	@ConditionalOnMissingBean
	public Retryer feignRetryer() {
		return Retryer.NEVER_RETRY;
	}

默认不进行重试

底层支持 HttpURLConnection 、HttpClient、OkHttp 三种
默认为HttpURLConnection 其中 connectTimeoutMillis默认为10s readTimeoutMillis 默认为60s
可通过下列方式修改默认值

@Bean
public Requst.Options options(){
	Request.Options options = new Request.Options(10000, 60000);
	return options;
}

若需要使用HttpClient、OkHttp 则需要导入相应的依赖
feign.httpclient.enabled 表示开启HttpClient的支持 当该属性不存在或属性值为true时为开启
feign.okhttp.enabled 表示开启OkHttp的支持 当该属性值为true时为开启

主要配置类
org.springframework.cloud.openfeign.FeignClientsConfiguration
org.springframework.cloud.openfeign.FeignAutoConfiguration
org.springframework.cloud.openfeign.FeignClientsRegistrar

使用案例
Spring Cloud OpenFeign 入参对象、返回值对象 无需和提供者一致
Spring Cloud OpenFeign 文件上传

Spring Cloud OpenFeign 3.1.0 官方文档

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

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

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