五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

如何閱讀 Spring Cloud OpenFein 源碼

2022-10-22 07:32 作者:Erwin_Feng  | 我要投稿

背景

一直以來,使用 Spring Cloud OpenFeign 都是閱讀官方文檔,雖然也大概知道其實現(xiàn)原理,但終究是沒有"證據(jù)"的。關(guān)于 Spring 的源碼閱讀,自認為是一件十分令人頭疼的事情。最近,在學(xué)習(xí) Feign 的原生 API,乘此機會,也就閱讀一下 Spring Cloud OpenFeign 的源碼,并將分享出來,希望能幫到有需要的人吧。

概述

關(guān)于 Spring Cloud OpenFeign 源碼的博客有很多,但是,不知道為什么,照著博客,一邊讀博客,一邊讀源碼,還一邊 debug,總是認為還有很多不清楚的地方。究其原因,我認為,博客都是按照源碼的流程講解,雖然附上了大段代碼,可能還是無法清晰的理解。不知道你們是不是,反正我是這樣的。

目標(biāo)

首先,我們明確一下今天探究的問題:

  1. 我們知道,當(dāng)我們使用?@FeignClient,是使用了JDK動態(tài)代理,那么是如何實現(xiàn)的,那一步創(chuàng)建的代理類。

  2. 當(dāng)我們知道第一個問題后,我們就基本清楚整個流程了,那么,我們就可以手寫一個簡易的入門測試了。

源碼

啟動流程

  • org.springframework.cloud.openfeign.EnableFeignClients

  • org.springframework.cloud.openfeign.FeignAutoConfiguration

  • org.springframework.cloud.openfeign.FeignClientsRegistrar

  • org.springframework.cloud.openfeign.FeignClientsRegistrar#registerBeanDefinitions

  • org.springframework.cloud.openfeign.FeignClientsRegistrar#registerFeignClient

  • org.springframework.cloud.openfeign.FeignClientFactoryBean#getObject

  • org.springframework.cloud.openfeign.Targeter

  • feign.Feign.Builder#build

  • feign.SynchronousMethodHandler.Factory

  • feign.ReflectiveFeign.ParseHandlersByName

  • feign.ReflectiveFeign#ReflectiveFeign

  • feign.Feign#newInstance

  • feign.ReflectiveFeign#newInstance

  • feign.InvocationHandlerFactory#create

  • feign.InvocationHandlerFactory.Default#create

  • feign.ReflectiveFeign.FeignInvocationHandler#FeignInvocationHandler

貼上圖吧,看看完整版的

注冊流程:

注冊流程

自動配置:

自動配置

調(diào)用流程

  • feign.ReflectiveFeign.FeignInvocationHandler#invoke

  • feign.InvocationHandlerFactory.MethodHandler#invoke

  • feign.SynchronousMethodHandler#invoke

調(diào)用流程

MyRpc

經(jīng)過上面的流程,我們手寫一個 RPC。

下面給出主要代碼。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import({MyRpcRegister.class, MyRpcAutoConfig.class})
public @interface EnableMyRpc {
}

getObject()

@Data
public class MyRpcFactoryBean implements FactoryBean<Object> {

? private String url;

? private String contextPath;

? private String name;

? private Class<?> type;

? private BeanFactory beanFactory;

? private MyClient myClient;

? @Override
? public Object getObject() {

? ? ? Map<Method, RpcBean> map = new HashMap<>();

? ? ? Method[] methods = type.getMethods();

? ? ? myClient = beanFactory.getBean(MyClient.class);

? ? ? for (Method method : methods) {
? ? ? ? ? Annotation[] annotations = method.getAnnotations();
? ? ? ? ? String httpMethod = "";
? ? ? ? ? String path = "";
? ? ? ? ? for (Annotation annotation : annotations) {
? ? ? ? ? ? ? if (annotation.annotationType() == PostMapping.class) {
? ? ? ? ? ? ? ? ? httpMethod = "POST";
? ? ? ? ? ? ? ? ? path = ((PostMapping) annotation).value()[0];
? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? } else if (annotation.annotationType() == GetMapping.class) {
? ? ? ? ? ? ? ? ? httpMethod = "GET";
? ? ? ? ? ? ? ? ? path = ((GetMapping) annotation).value()[0];
? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? } else if (annotation.annotationType() == RequestMapping.class) {
? ? ? ? ? ? ? ? ? RequestMapping requestMapping = ((RequestMapping) annotation);
? ? ? ? ? ? ? ? ? httpMethod = requestMapping.method()[0].name();
? ? ? ? ? ? ? ? ? path = requestMapping.value()[0];
? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ? RpcBean rpcBean = new RpcBean()
? ? ? ? ? ? ? ? ? .setUrl(url + contextPath)
? ? ? ? ? ? ? ? ? .setPath(path)
? ? ? ? ? ? ? ? ? .setHttpMethod(httpMethod)
? ? ? ? ? ? ? ? ? .setMyClient(myClient)
? ? ? ? ? ? ? ? ? ;
? ? ? ? ? map.put(method, rpcBean);
? ? ? }

? ? ? ClassLoader loader = type.getClassLoader();

? ? ? return Proxy.newProxyInstance(loader, new Class<?>[] {type}, new MyRpcInvocationHandler(map));
? }

? @Override
? public Class<?> getObjectType() {
? ? ? return type;
? }

}

handler

@Slf4j
public class MyRpcInvocationHandler implements InvocationHandler {

? private final Map<Method, RpcBean> map;

? public MyRpcInvocationHandler(Map<Method, RpcBean> map) {
? ? ? this.map = map;
? }

? @Override
? public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

? ? ? log.info("proxy handler");

? ? ? return request(method, args);
? }

? public Object request(Method method, Object[] args) {
? ? ? String result = "";
? ? ? RpcBean rpcBean = map.get(method);
? ? ? Parameter[] parameters = method.getParameters();
? ? ? Class<?> returnType = method.getReturnType();
? ? ? String url = rpcBean.getUrl() + rpcBean.getPath();
? ? ? String httpMethod = rpcBean.getHttpMethod();
? ? ? String param = getParam(httpMethod, parameters, args);
? ? ? log.info("url: [{}], param: [{}]", url, param);
? ? ? MyClient myClient = rpcBean.getMyClient();
? ? ? if ("POST".equals(httpMethod)) {
? ? ? ? ? result = myClient.post(url, param);
? ? ? } else if ("GET".equals(httpMethod)) {
? ? ? ? ? result = myClient.get(url, param);
? ? ? }
? ? ? if (StringUtils.hasText(result)) {
? ? ? ? ? return JsonUtils.convertObject(result, returnType);
? ? ? }
? ? ? return "";
? }

? public String getParam(String httpMethod, Parameter[] parameters, Object[] args) {
? ? ? if ("POST".equals(httpMethod)) {
? ? ? ? ? return JsonUtils.convertString(args[0]);
? ? ? } else if ("GET".equals(httpMethod)) {
? ? ? ? ? if (Objects.isNull(parameters) || parameters.length == 0
? ? ? ? ? ? ? ? ? || Objects.isNull(args) || args.length == 0) {
? ? ? ? ? ? ? return "";
? ? ? ? ? }
? ? ? ? ? String param = "";
? ? ? ? ? StringBuilder urlBuilder = new StringBuilder(param);
? ? ? ? ? for (int i = 0; i < parameters.length; i++) {
? ? ? ? ? ? ? if (Objects.nonNull(args[i])) {
? ? ? ? ? ? ? ? ? urlBuilder.append(String.format("%s=%s&", parameters[i].getName(), args[i]));
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ? param = urlBuilder.toString();
? ? ? ? ? param = param.substring(0, param.length() - 1);
? ? ? ? ? return param;
? ? ? }
? ? ? return "";
? }
}

[完整代碼](https://github.com/fengwenyi/spring-cloud-demo/tree/2021.0.x/demo-spring-cloud-rpc)

如何閱讀 Spring Cloud OpenFein 源碼的評論 (共 條)

分享到微博請遵守國家法律
淮北市| 阳曲县| 蓬溪县| 新绛县| 师宗县| 白水县| 永靖县| 铜陵市| 肇东市| 达州市| 贞丰县| 鹤峰县| 交口县| 南皮县| 连山| 金阳县| 南丹县| 建瓯市| 延津县| 桐乡市| 江西省| 绵阳市| 利辛县| 鹰潭市| 漳平市| 厦门市| 海门市| 方正县| 板桥市| 鹤峰县| 射洪县| 福贡县| 界首市| 五常市| 托里县| 莒南县| 福海县| 连山| 彩票| 莆田市| 白城市|