做海鲜团购网站,视频制作公司排行,科技狂人,html代码是什么意思Feign 简介
Spring Cloud Feign 是一个 HTTP 请求调用的轻量级框架#xff0c;可以以 Java 接口注解的方式调用 HTTP 请求#xff0c;而不用通过封装 HTTP 请求报文的方式直接调用
Feign 通过处理注解#xff0c;将请求模板化#xff0c;当实际调用的时候传入参数#x…Feign 简介
Spring Cloud Feign 是一个 HTTP 请求调用的轻量级框架可以以 Java 接口注解的方式调用 HTTP 请求而不用通过封装 HTTP 请求报文的方式直接调用
Feign 通过处理注解将请求模板化当实际调用的时候传入参数根据参数再应用到请求上进而转化成真正的请求 第一个 Feign 程序
本小节介绍如何通过 NacosFeign 实现服务之间的调用新建 server-01、server-02 项目并分别注册 Nacos
server-01 引入依赖
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId
/dependency
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-loadbalancer/artifactId
/dependency新建 Server02FeignClient 接口用于调用 server-02 提供的对外接口
// name:要调用的服务名
FeignClient(name server-02)
public interface Server02FeignClient {GetMapping(/test/getConfig)void getConfig();
}启动类加上注解 EnableFeignClients
EnableFeignClients
EnableDiscoveryClient
SpringBootApplication
public class Server01Application {public static void main(String[] args) {SpringApplication.run(Server01Application.class, args);}
}使用 Server02FeignClient 调用 server-02 的接口
Slf4j
RestController
public class TestCon {Autowiredprivate Server02FeignClient server02FeignClient;GetMapping(/test/getConfigByFeign)public void getConfigByFeign() {server02FeignClient.getConfig();}
}在 server-02 创建接口
Slf4j
RestController
public class TestCon {Value(${test.value})private String testValue;Value(${spring.application.name})private String applicationName;Value(${server.port})private String port;GetMapping(/test/getConfig)public void getConfig() {log.info(testValue: {} by {}-{}, testValue, applicationName, port);}
}调用 server-01 的 /test/getConfigByFeign 接口就会通过 Feign 调用 server-02 的 /test/getConfig 接口
FeignClient 注解可作用在类、接口、枚举上主要包含如下属性 name/valuename 是 vaue 的别名value 也是 name 的别名两者的作用是一致的用指定 FeignClient 的名称如果配合注册中心使用则作为微服务的名称用于服务发现 url主要用于调试可以手动指定 FeignClient 调用的地址 pathpath 用于定义当前 FeignClient 的统一前缀 contextld如果要创建多个具有相同名称或 URL 的 Feign 客户端以便它们指向同一台服务器但是每个客户端具有不同的自定义配置则必须使用 contextId 属性以避免这些配置的名称冲突
FeignClient(contextId fooClient, name stores, configurationFooConfiguration.class)
public interface FooClient {...}FeignClient(contextId barClient, name stores, configurationBarConfiguration.class)
public interface BarClient {...}fallback/fallbackFactory fallback定义容错的处理类当调用远程接口失败或超时时就会调用对应接口的容错逻辑falback 指定的类必须实现 FeignClient 标记的接口 fallbackFactory工厂类用于生成 fallback 类通过该属性可以实现每个接口通用的容错逻辑减少重复的代码 decode404当发生 404 错误时如果该字段为 true就会调用 decoder 进行解码否则抛出异常 ConfigurationFeign 配置类可以自定义 Feign 的 Encoder、Decoder、LogLevel、Contract 等OpenFeign 默认为 Feign 提供以下对象bean 类型 bean 名称 : 类名称 Decoder feignDecoder : ResponseEntityDecoder Encoder feignEncoder : SpringEncoder Logger feignLogger : Slf4jLogger Contract feignContract : SpringMvcContract FeignBuilder feignBuilder : HystrixFeignBuilder
spring-cloud-starter-openfeign 支持 spring-cloud-starter-netflix-ribbon 和 spring-cloud-starter.loadbalancer如果 Ribbon 在类路径中且已启用则 Client feignClient 是 LoadBalancerFeignClient如果 SpringCloud LoadBalancer 在类路径中则使用 FeignBlockingLoadBalancerClient
默认情况下Spring Cloud OpenFeign 不会为 Feign 提供以下 bean 对象但是仍然会从应用程序上下文中查找这些类型的 bean 以创建 Feign 客户端
Logger.LevelRetryerErrorDecoderRequest.OptionsCollectionRequestInterceptorSetterFactoryQueryMapEncoder
以上是通过注解 FeignClient 的配置属性进行配置的我们也可以使用配置文件进行配置
feign:client:config:feignName:connectTimeout: 5000readTimeout:5000loggerLevel: fullerrorDecoder: com.example.SimpleErrorDecoderretryer: com.example,SimpleRetryerrequestInterceptors:- com.example.FooRequestInterceptor- com.example,BarRequestInterceptordecode404: falseencoder: com.example.SimpleEncoderdecoder: com.example.SimpleDecodercontract: com.example.SimpleContract可以在 EnableFeignClients 属性 defaultConfiguration 中指定默认配置不同之处在于此配置将适用于所有 Feign 客户端
如果希望使用配置文件来配置所有 FeignClient则可以使用默认 Feign 名称创建配置属性例如
feign:client:config:default:connectTimeout: 5000readTimeout: 5000loggerLevel: basic如果同时创建 Configuration bean 和配置文件则配置文件将覆盖 Configuration 值如果要将优先级更改为 Configuration就可以将 feign.client.default-to-properties 更改为 false Feign 传参
以下服务端接口可通过 Get 或 Post 请求调用并接收参数
RequestMapping(/test/testFeignWithParam)
public void testFeignWithParam(RequestParam String name,RequestParam int age) {log.info(testFeignWithParam: name-{}, age-{}, name, age);
}通过在 Url 拼接请求传参如下
FeignClient(name server-02, path server-02)
public interface Server02FeignClient {GetMapping(/test/testFeignWithParam?namezhanghsanage66)//PostMapping(/test/testFeignWithParam?namezhanghsanage66)void testFeignWithParam();
}使用 RequestParam 传参如下
FeignClient(name server-02, path server-02)
public interface Server02FeignClient {//GetMapping(/test/testFeignWithParam)PostMapping(/test/testFeignWithParam)void testFeignWithParam(RequestParam(name) String name,RequestParam(age) int age);
}也可以使用 OpenFeign 的 QueryMap 将请求实体作为参数的映射不过由于 QueryMap 注解与 Spring 不兼容所以 OpenFeign 提供了等效的 SpringQueryMap 注解
FeignClient(name server-02, path server-02)
public interface Server02FeignClient {//GetMapping(/test/testFeignWithQueryMap)PostMapping(/test/testFeignWithQueryMap)void testFeignWithQueryMap(SpringQueryMap FeignParam param);
}