Spring使用Feign代理Http请求

前言

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
/**
* @since 2020-11-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
/**
* 消费方
* @since 2020-11-18
*/
public interface TestService {

//直接url path中添加参数
@RequestLine("GET /feign/get/{id}")
String test1(@Param("id") Integer id);

//url带参数
@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
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-okhttp -->
<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
/**
* @since 2020-11-18
*
* 调用方
*/
@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
/**
* 调用方
* @since 2020-11-18
*/
@Controller
@RequestMapping("/demo")
public class FeignDemoController {

@Autowired
FeignClientProxy feignClientProxy;

// http://localhost:8080/demo/feign?id=3
@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);

}
}