集成Feign简化调用方式(一)

open-feign
DerekYRC 3 years ago
parent 41b51974db
commit 2dd322f07f

@ -16,7 +16,7 @@
* [服务注册](https://github.com/DerekYRC/mini-spring-cloud/blob/main/changelog.md#服务注册)
* [服务发现](https://github.com/DerekYRC/mini-spring-cloud/blob/main/changelog.md#服务发现)
* [负载均衡](https://github.com/DerekYRC/mini-spring-cloud/blob/main/changelog.md#集成ribbon实现客户端负载均衡)
* [集成Feign简化调用]()
* [集成Feign简化调用方式]()
* [流量控制]()
* [熔断降级]()
* [API 网关]()

@ -34,6 +34,11 @@
<artifactId>mini-spring-cloud-load-balancer</artifactId>
</dependency>
<dependency>
<groupId>com.github</groupId>
<artifactId>mini-spring-cloud-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>

@ -1,5 +1,6 @@
package com.github.cloud.examples;
import com.github.cloud.openfeign.EnableFeignClients;
import com.github.cloud.tutu.discovery.TutuDiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
@ -20,6 +21,7 @@ import java.util.List;
* @author derek()
* @date 2022/3/20
*/
@EnableFeignClients
@SpringBootApplication
public class ConsumerApplication {
@ -54,6 +56,9 @@ public class ConsumerApplication {
@Autowired
private RestTemplate loadBalancedRestTemplate;
@Autowired
private EchoService echoService;
private RestTemplate restTemplate = new RestTemplate();
@GetMapping("/hello")
@ -85,6 +90,11 @@ public class ConsumerApplication {
public String foo() {
return loadBalancedRestTemplate.postForObject("http://provider-application/echo", null, String.class);
}
@GetMapping("/bar")
public String bar() {
return echoService.echo();
}
}
}

@ -0,0 +1,15 @@
package com.github.cloud.examples;
import com.github.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
/**
* @author derek()
* @date 2022/4/9
*/
@FeignClient("provider-application")
public interface EchoService {
@PostMapping("echo")
String echo();
}

@ -1,11 +1,16 @@
package com.github.cloud.openfeign;
import java.lang.annotation.*;
/**
* Feign
*
* @author derek()
* @date 2022/4/7
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {
/**

@ -1,5 +1,7 @@
package com.github.cloud.openfeign;
import com.github.cloud.openfeign.support.SpringMvcContract;
import feign.Feign;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
@ -21,7 +23,9 @@ public class FeignClientFactoryBean implements FactoryBean<Object>, ApplicationC
@Override
public Object getObject() throws Exception {
return null;
return Feign.builder()
.contract(new SpringMvcContract())
.target(type, "http://localhost:1234");
}
@Override

@ -0,0 +1,46 @@
package com.github.cloud.openfeign.support;
import feign.Contract;
import feign.MethodMetadata;
import feign.Request;
import org.springframework.web.bind.annotation.PostMapping;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
/**
* feignSpring MVC
*
* @author derek()
* @date 2022/4/9
*/
public class SpringMvcContract extends Contract.BaseContract {
@Override
protected void processAnnotationOnClass(MethodMetadata data, Class<?> clz) {
//TODO 解析接口注解
}
@Override
protected void processAnnotationOnMethod(MethodMetadata data, Annotation annotation, Method method) {
//解析方法注解
//解析PostMapping注解
if (annotation instanceof PostMapping) {
PostMapping postMapping = (PostMapping) annotation;
data.template().method(Request.HttpMethod.POST);
String path = postMapping.value()[0];
if (!path.startsWith("/") && !data.template().path().endsWith("/")) {
path = "/" + path;
}
data.template().uri(path, true);
}
//TODO 解析其他注解
}
@Override
protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations, int paramIndex) {
//TODO 解析参数
return true;
}
}

@ -63,6 +63,12 @@
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github</groupId>
<artifactId>mini-spring-cloud-openfeign</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>

Loading…
Cancel
Save