前言
在 spring-cloud
中,大家会用 feign
远程调用,以下记录下在 springboot
中代理http简单调用
实现1
pom.xml
1 2 3 4 5
| <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-core</artifactId> <version>11.0</version> </dependency>
|
服务提供方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@RestController @RequestMapping("/feign") public class FeignController {
@GetMapping("/get/{id}") public String test1(@PathVariable Integer id) { return "id1=" + id; }
@GetMapping("/get") public String test2(@RequestParam Integer id) { return "id2=" + id; } }
|
服务消费方
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public interface TestService {
@RequestLine("GET /feign/get/{id}") String test1(@Param("id") Integer id);
@RequestLine("GET /feign/get?id={id}") String test2(@Param("id") Integer id); }
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class TestServiceTest {
@Test void test1() { TestService testService = Feign.builder().target(TestService.class, "http://127.0.0.1:8080"); String ret = testService.test1(1); System.out.println(ret); }
@Test void test2() { TestService testService = Feign.builder().target(TestService.class, "http://127.0.0.1:8080"); String ret = testService.test2(2); System.out.println(ret); } }
|
实现2
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.6.RELEASE</version> </dependency>
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-okhttp</artifactId> <version>11.0</version> </dependency>
|
application.properties
1 2
| feign.httpclient.enabled=false feign.okhttp.enabled=true
|
1 2
| // 启动 @EnableFeignClients
|
feign
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Configuration @ConditionalOnClass(Feign.class) @AutoConfigureBefore(FeignAutoConfiguration.class) public class FeignOkHttpConfig {
@Bean public okhttp3.OkHttpClient okHttpClient(){ return new okhttp3.OkHttpClient.Builder() .readTimeout(60, TimeUnit.SECONDS) .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(120, TimeUnit.SECONDS) .connectionPool(new ConnectionPool()) .build(); } }
|
feign代理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
@FeignClient(name = "feignClientProxy",url = "http://localhost:8080") public interface FeignClientProxy {
@RequestMapping(value = "/feign/get", method = RequestMethod.GET) @ResponseBody String invoke(@RequestParam(value="id") Integer id);
@Component class DefaultFallback implements FeignClientProxy { @Override public String invoke(@RequestParam(value="id") Integer id){ return "err"; } } }
|
调用方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
@Controller @RequestMapping("/demo") public class FeignDemoController {
@Autowired FeignClientProxy feignClientProxy;
@RequestMapping(value = "feign") @ResponseBody public String testFeign(@RequestParam(name = "id") Integer id) throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); String respResponseBody = feignClientProxy.invoke(id); return "调用结果:" + objectMapper.writeValueAsString(respResponseBody);
} }
|