5、Spring-Cloud-声明式调用 Feign(上)
5.1、写一个 Feign 害户端
依赖:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
关于服务中心使用的是8762(之前的案列写过)
地址:https://www.cnblogs.com/Mrchengs/p/10645911.html
- spring.application.name=feign
- server.port=
- eureka.client.service-url.defaultZone=http://localhost:8762/eureka/
@EnableFeignClients:开启Feign Client功能
- @EnableFeignClients(basePackages = "com.cr.eurekafeignclient.feign")
- @EnableDiscoveryClient
- @SpringBootApplication
- public class EurekaFeignClientApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaFeignClientApplication.class, args);
- }
- }
EurekaClientFeign.java
- @FeignClient(value = "CLINET",configuration = feignconfig.class)
- public interface EurekaClientFeign {
- @GetMapping("/port")
- String port();
- }
feignconfig.java
- @Configuration
- public class feignconfig {
- @Bean
- public Retryer feignRetryer(){
- return new Retryer.Default(, TimeUnit.SECONDS.toMillis(1L),);
- }
- }
FeignService.java
- @Service
- public class FeignService {
- @Autowired
- EurekaClientFeign eurekaClientFeign;
- public String port(){
- return eurekaClientFeign.port();
- }
- }
FeignController.java
- @RestController
- public class FeignController {
- @Autowired
- FeignService feignService;
- @GetMapping("/feign")
- public String sayPort(){
- return feignService.port();
- }
- }
spring-cloud-starter-openfeign 的porn 文件
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-openfeign-core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-commons</artifactId>
- </dependency>
- <dependency>
- <groupId>io.github.openfeign</groupId>
- <artifactId>feign-core</artifactId>
- </dependency>
- <dependency>
- <groupId>io.github.openfeign</groupId>
- <artifactId>feign-slf4j</artifactId>
- </dependency>
- <dependency>
- <groupId>io.github.openfeign</groupId>
- <artifactId>feign-hystrix</artifactId>
- </dependency>
- <dependency>
- <groupId>io.github.openfeign</groupId>
- <artifactId>feign-java8</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-archaius</artifactId>
- <optional>true</optional>
- </dependency>
5.2、FeignClient详解
@FeignClient注解源码
- //
- // Source code recreated from a .class file by IntelliJ IDEA
- // (powered by Fernflower decompiler)
- //
- package org.springframework.cloud.openfeign;
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- import org.springframework.core.annotation.AliasFor;
- @Target({ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- public @interface FeignClient {
- @AliasFor("name")
- String value() default "";
- /** @deprecated */
- @Deprecated
- String serviceId() default "";
- String contextId() default "";
- @AliasFor("value")
- String name() default "";
- String qualifier() default "";
- String url() default "";
- boolean decode404() default false;
- Class<?>[] configuration() default {};
- Class<?> fallback() default void.class;
- Class<?> fallbackFactory() default void.class;
- String path() default "";
- boolean primary() default true;
- }
5.3、FeignClient的配置
- @Configuration
- public class FeignClientsConfiguration {
- @Autowired
- private ObjectFactory<HttpMessageConverters> messageConverters;
- @Autowired(
- required = false
- )
- private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList();
- @Autowired(
- required = false
- )
- private List<FeignFormatterRegistrar> feignFormatterRegistrars = new ArrayList();
- @Autowired(
- required = false
- )
- private Logger logger;
- public FeignClientsConfiguration() {
- }
- @Bean
- @ConditionalOnMissingBean
- public Decoder feignDecoder() {
- return new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)));
- }
- @Bean
- @ConditionalOnMissingBean
- @ConditionalOnMissingClass({"org.springframework.data.domain.Pageable"})
- public Encoder feignEncoder() {
- return new SpringEncoder(this.messageConverters);
- }
- @Bean
- @ConditionalOnClass(
- name = {"org.springframework.data.domain.Pageable"}
- )
- @ConditionalOnMissingBean
- public Encoder feignEncoderPageable() {
- return new PageableSpringEncoder(new SpringEncoder(this.messageConverters));
- }
- @Bean
- @ConditionalOnMissingBean
- public Contract feignContract(ConversionService feignConversionService) {
- return new SpringMvcContract(this.parameterProcessors, feignConversionService);
- }
- @Bean
- public FormattingConversionService feignConversionService() {
- FormattingConversionService conversionService = new DefaultFormattingConversionService();
- Iterator var2 = this.feignFormatterRegistrars.iterator();
- while(var2.hasNext()) {
- FeignFormatterRegistrar feignFormatterRegistrar = (FeignFormatterRegistrar)var2.next();
- feignFormatterRegistrar.registerFormatters(conversionService);
- }
- return conversionService;
- }
- @Bean
- @ConditionalOnMissingBean
- public Retryer feignRetryer() {
- return Retryer.NEVER_RETRY;
- }
- @Bean
- @Scope("prototype")
- @ConditionalOnMissingBean
- public Builder feignBuilder(Retryer retryer) {
- return Feign.builder().retryer(retryer);
- }
- @Bean
- @ConditionalOnMissingBean({FeignLoggerFactory.class})
- public FeignLoggerFactory feignLoggerFactory() {
- return new DefaultFeignLoggerFactory(this.logger);
- }
- @Bean
- @ConditionalOnClass(
- name = {"org.springframework.data.domain.Page"}
- )
- public Module pageJacksonModule() {
- return new PageJacksonModule();
- }
- @Configuration
- @ConditionalOnClass({HystrixCommand.class, HystrixFeign.class})
- protected static class HystrixFeignConfiguration {
- protected HystrixFeignConfiguration() {
- }
- @Bean
- @Scope("prototype")
- @ConditionalOnMissingBean
- @ConditionalOnProperty(
- name = {"feign.hystrix.enabled"}
- )
- public Builder feignHystrixBuilder() {
- return HystrixFeign.builder();
- }
- }
- }
- @Configuration
- public class feignconfig {
- @Bean
- public Retryer feignRetryer(){
- return new Retryer.Default(, TimeUnit.SECONDS.toMillis(1L),);
- }
- }
5、Spring-Cloud-声明式调用 Feign(上)的更多相关文章
- spring cloud服务间调用feign
参考文章:Spring Cloud Feign设计原理 1.feign是spring cloud服务间相互调用的组件,声明式.模板化的HTTP客户端.类似的HttpURLConnection.Apac ...
- Spring Cloud声明式调用Feign负载均衡FeignClient详解
为了深入理解Feign,下面将从源码的角度来讲解Feign.首先来看看FeignClient注解@FeignClient的源码,代码如下: FeignClient注解被@Target(ElementT ...
- spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)
前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...
- Spring Cloud Config整合Spring Cloud Kubernetes,在k8s上管理配置
1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Kubernetes有专门的ConfigMap和Secret来管理配置,但它也有一些局限性,所以还是希望通过Spring C ...
- 【spring cloud】【IDEA】【maven】spring cloud多模块在idea上使用maven插件打包报错:程序包XXX不存在
>>>>spring cloud 多模块 >>>>在idea上使用maven插件打包,欲打包成jar包后 进行部署 >>>> 报 ...
- Spring Cloud项目中通过Feign进行内部服务调用发生401\407错误无返回信息的问题
问题描述 最近在使用Spring Cloud改造现有服务的工作中,在内部服务的调用方式上选择了Feign组件,由于服务与服务之间有权限控制,发现通过Feign来进行调用时如果发生了401.407错误时 ...
- 【spring cloud】spring cloud2.X spring boot2.0.4调用feign配置Hystrix Dashboard 和 集成Turbine 【解决:Hystrix仪表盘Unable to connect to Command Metric Stream】【解决:Hystrix仪表盘Loading...】
环境: <java.version>1.8</java.version><spring-boot.version>2.0.4.RELEASE</spring- ...
- Spring-Cloud之Feign声明式调用-4
一.Feign受Retrofit.JAXRS-2.0和WebSocket影响,采用了声明式API 接口的风格,将Java Http 客户端绑定到它的内部. Feign 首要目的是将 Java Http ...
- Spring Cloud系列之使用Feign进行服务调用
在上一章的学习中,我们知道了微服务的基本概念,知道怎么基于Ribbon+restTemplate的方式实现服务调用,接着上篇博客,我们学习怎么基于Feign实现服务调用,请先学习上篇博客,然后再学习本 ...
- Spring Cloud Alibaba Sentinel 整合 Feign 的设计实现
作者 | Spring Cloud Alibaba 高级开发工程师洛夜 来自公众号阿里巴巴中间件投稿 前段时间 Hystrix 宣布不再维护之后(Hystrix 停止开发...Spring Cloud ...
随机推荐
- 一、hive安装(内置数据库derby)
hive是一个数据仓库工具,建立在hadoop之上,它的存在是为了让大数据的查询和分析更加的方便.hive提供简单的sql查询功能,并最终转换为mapreduce任务执行. 一.环境 JDK1.8+官 ...
- java自学-编程入门
java语言写的代码需要先编译为可执行文件,才能被jvm执行.在下载的jdk安装目录下的bin目录,有两个可执行程序java.exe和javac.exe,javac就是用来编译的,java是执行编译后 ...
- HDU 1757 矩阵求第n的递推式
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- spss C# 二次开发 学习笔记(一)——配置数据源
由于项目的需要,使用Spss进行数据统计分析. Spss对于数据统计分析的功能有多强主要是客户关注的事情,我所主要关注的是,Spss的二次开发有多复杂. 学习的基本思路是: (1)首先了解统计基本知识 ...
- Jquery获取radio选中的值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 使用PuTTy在CentOS下安装web.py与简单的文件传输
两周前,出于帮朋友忙的目的,尝试了一下微信公众号的菜单自定义与自动回复功能的实现,成了. 两周后,需要将代码转移至朋友新购的服务器上,发现基本操作全忘记了,麻瓜!所以记一笔,希望也能对大家也有帮助. ...
- Raspberry Pi - Huawei HiLink E3256 3G modem to ethernet adapter
Raspberry Pi - Huawei HiLink E3256 3G modem to ethernet adapter This page documents how to configure ...
- win7 远程连接服务器出现身份验证错误,且找不到加密Oracle修正
用远程桌面连接登录服务器,结果,弹出一个错误的提示框:发生身份验证错误,要求的函数不受支持. 然后在网上找了相关的教程,基本上所有的方法都是如下所示: 策略路径:"计算机配置"-& ...
- ListView中Item与Checkable子类控件抢焦点问题
Android开发中,经常需要为ListView定制Adapter,绑定各种子类控件.如果Item包含Button等Checkable的控件,那么就会发生点击Item无法响应的问题.原因是自己定义的I ...
- [学习] nofollow
[来源:百度百科 http://baike.baidu.com/view/1584081.htm] 简介 nofollow[1]是一个HTML标签的属性值.它的出现为网站管理员提供了一种方式,即告诉搜 ...