<spring-cloud-openfeign.version>2.2.6.RELEASE</spring-cloud-openfeign.version>
对应的SpringBoot
<version>2.3.0.RELEASE</version>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>${spring-cloud-openfeign.version}</version> <exclusions> <exclusion> <artifactId>archaius-core</artifactId> <groupId>com.netflix.archaius</groupId> </exclusion> </exclusions> </dependency>
简单调用
//定义一个拦截器 public class FeignInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { requestTemplate.header("AppId", "AppId"); requestTemplate.header("AppKey", "myuser1"); requestTemplate.header("AppSecret", "mypassword"); } } //接口类 @Component @FeignClient(name = "demo-feign", url = "https://www.fastmock.site/mock/e5738f58a04967320a772f1d69aa4a41/mp/", configuration = FeignInterceptor.class) public interface IFeignTestService { @GetMapping(value = "/GetUser") String getUser(@RequestBody String words); } //调用 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class FeignTests { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private IFeignTestService feignTestService; @Test void searchTest() { String result = feignTestService.getUser("VipSoft"); logger.info(result); } }
OpenFeign 动态URL
//定义一个拦截器 public class FeignInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { requestTemplate.header("AppId", "AppId"); requestTemplate.header("AppKey", "myuser1"); requestTemplate.header("AppSecret", "mypassword"); } } //接口类 @Component @FeignClient(name = "demo-feign", url = "url必须有值,这里随便写,但不能为空", configuration = FeignInterceptor.class) public interface IFeignTestService { @PostMapping("/") String getWebsite(URI uri, @RequestBody String words); } //调用 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class FeignTests { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private IFeignTestService feignTestService; @Test void searchTest() { String ss1 = feignBuilderService.getWebsite(new URI("https://www.fastmock.site/mock/e5738f58a04967320a772f1d69aa4a41/mp/GetUser"), ""); logger.error(ss1); String ss2 = feignBuilderService.getWebsite(new URI("https://www.fastmock.site/mock/e5738f58a04967320a772f1d69aa4a41/mp/hospital"), ""); logger.error(ss2); String ss3 = feignBuilderService.getWebsite(new URI("https://www.fastmock.site/mock/e5738f58a04967320a772f1d69aa4a41/mp/device"), ""); logger.error(ss3); } }