赞
踩
@AuthorizedFeignClient(name = "unit", url = "${microservices.unit}") @Component public interface UnitClient { /** * 上传数据 */ @GetMapping("/uploadAllUnit/{unitId}") ResponseEntity<ResponseBean> uploadAllUnit(@PathVariable String unitId); /** * 关灯 */ @PostMapping("/lampOffControl/{unitId}") ResponseEntity<RequestResult> lampOffControl(@PathVariable String unitId); } // 其他省略
package com.newatc.core.client; import java.lang.annotation.*; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClientsConfiguration; import org.springframework.core.annotation.AliasFor; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @FeignClient public @interface AuthorizedFeignClient { @AliasFor(annotation = FeignClient.class, attribute = "name") String name() default ""; /** * A custom {@code @Configuration} for the feign client. * * Can contain override {@code @Bean} definition for the pieces that * make up the client, for instance {@link feign.codec.Decoder}, * {@link feign.codec.Encoder}, {@link feign.Contract}. * * @return the custom {@code @Configuration} for the feign client. * @see FeignClientsConfiguration for the defaults. */ @AliasFor(annotation = FeignClient.class, attribute = "configuration") Class<?>[] configuration() default OAuth2InterceptedFeignConfiguration.class; /** * An absolute URL or resolvable hostname (the protocol is optional). * @return the URL. */ String url() default ""; /** * Whether 404s should be decoded instead of throwing FeignExceptions. * @return true if 404s will be decoded; false otherwise. */ boolean decode404() default false; /** * Fallback class for the specified Feign client interface. The fallback class must * implement the interface annotated by this annotation and be a valid Spring bean. * @return the fallback class for the specified Feign client interface. */ Class<?> fallback() default void.class; /** * Path prefix to be used by all method-level mappings. * @return the path prefix to be used by all method-level mappings. */ String path() default ""; }
package com.newatc.core.client;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
public class OAuth2InterceptedFeignConfiguration {
@Bean(name = "oauth2RequestInterceptor")
public RequestInterceptor getOAuth2RequestInterceptor() {
return new TokenRelayRequestInterceptor();
}
}
template.target(host)
template.feignTarget(new Target.HardCodedTarget<>(target.type(), target.name(), host);
@FeignClient
注解的类,有没有指定配置和拦截器,找到拦截器,在拦截器里重新指定RequestTemplate的目标路由即可OpenFeign是一个基于Java的声明式HTTP客户端,它简化了编写基于HTTP的API的代码。它使用了注解来定义和配置HTTP API,并且自动地将这些API转换为相应的HTTP请求。
OpenFeign的特点包括:
声明式API:使用Java接口和注解定义和配置HTTP API,无需手动拼接URL和参数。
支持多种编码器:支持多种编码器,包括常见的JSON和XML编码器,以及自定义的编码器。
内置负载均衡:集成了Ribbon负载均衡器,可以轻松地对请求进行负载均衡。
支持熔断器:集成了Hystrix熔断器,可以在服务不可用时阻止不稳定的网络请求。
易于集成:可以轻松地集成到Spring应用中,也可以独立使用。
通过使用OpenFeign,开发人员可以更加简单地与其他服务进行通信,而不需要手动编写大量的HTTP请求代码。这使得开发人员可以更加专注于业务逻辑的实现,提高了开发效率。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。