Feign测试

open-feign
DerekYRC 2 years ago
parent f0f796ee20
commit d7b0b5378b

@ -912,7 +912,7 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
}
```
- reconstructURI方法重建请求URI将服务名称替换为服务实例的IP:端口例如http://provider-application/echo被重建为http://192.168.100.1:8888/echo
- reconstructURI方法重建请求URI将服务名称替换为服务实例的IP:端口,例如```http://provider-application/echo``` 被重建为```http://192.168.100.1:8888/echo```
- execute方法处理http请求
有了RibbonLoadBalancerClient的reconstructURI和execute方法将所有http请求委托给RibbonLoadBalancerClient即可。其实spring-cloud-commons已经帮我们配置好拦截RestTemplate的http请求委托给RibbonLoadBalancerClient的拦截器LoadBalancerInterceptor配置类如下:

@ -18,6 +18,12 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github</groupId>
<artifactId>mini-spring-cloud-tutu-discovery</artifactId>
@ -27,6 +33,19 @@
<groupId>com.github</groupId>
<artifactId>mini-spring-cloud-load-balancer</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

@ -0,0 +1,52 @@
package com.github.cloud.examples;
import feign.Feign;
import feign.RequestLine;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.web.bind.annotation.GetMapping;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author derek()
* @date 2022/3/27
*/
public class FeignTest {
private static Logger logger = LoggerFactory.getLogger(FeignTest.class);
interface HelloService {
@RequestLine("GET /hello")
String hello();
}
@Test
public void testOpenFeign() {
HelloService helloService = Feign.builder()
.target(HelloService.class, "http://localhost:8080");
String response = helloService.hello();
logger.info("response: {}", response);
boolean succ = response.startsWith("Port of the service provider");
assertThat(succ).isTrue();
}
interface WorldService {
@GetMapping("/world")
String world();
}
@Test
public void testSpringCloudOpenFeign() {
WorldService worldService = Feign.builder()
.contract(new SpringMvcContract())
.target(WorldService.class, "http://localhost:8080");
String response = worldService.world();
logger.info("response: {}", response);
boolean succ = response.startsWith("Port of the service provider");
assertThat(succ).isTrue();
}
}
Loading…
Cancel
Save