Original: Yuantiandi (WeChat public account ID: cxytiandi), welcome to share, please keep the source for reprinting.
At work, I believe that many people have the following feelings:
- Whose code is this, I can't stand it anymore
- This is broken code, there is no comment
- This code is not as good as I wrote
- There is a bug in this code
- This code,. . . . . . .
Is it true? We often have these ideas when we look at other people's code. I think the main reason is that most of the business code is seen, and many of them are accumulated over the years without refactoring. Then the stacking logic year after year will eventually become a shishan mountain.
Of course, there are many people who write code really very well, concise and easy to understand. When we look at other people’s code, we have to look at it with a learning attitude. The same logic is to see how other people write it, why write it like this, if It's how you would write it. By comparison, you will gain something in this way.
The topic I want to talk to you today is mainly to look at the source code of an open source project, because everyone can read the business code every day. So often only use certain frameworks, and ignore its inherent.
Looking at the source code of an open source project is a good learning opportunity, especially when you encounter a problem, or when you want to do a function, if there are similar functions in other frameworks, then you know how to do it.
Case number one
For example, I am working on a function that needs to integrate multiple configuration centers. If you rely on Nacos, use Nacos, and if you rely on Apollo, then use Apollo. This kind of non-dependency situation should be dealt with in the automatic assembly class. The first thing I thought was to deal with it like this:
@ConditionalOnClass(value = com.alibaba.nacos.api.config.ConfigService.class)
@ConditionalOnMissingClass(value = { "com.alibaba.cloud.nacos.NacosConfigProperties" })
@Bean
public NacosConfigUpdateListener nacosConfigUpdateListener() {
return new NacosConfigUpdateListener();
}
Then the test found that if the project does not rely on Nacos, an error will be reported here, although the judgment will not work. At this time, I will think again, how is it implemented in some other frameworks?
At this time, I thought of looking at Zuul's source code before, and there are similar requirements in it. Different Clients will be used to make calls, such as ApacheHttpClient, OkHttpClient.
It is found that Zuul has added a static class for judgment, which will not report an error. as follows:
@Configuration
@ConditionalOnClass(value = com.alibaba.nacos.api.config.ConfigService.class)
@ConditionalOnMissingClass(value = { "com.alibaba.cloud.nacos.NacosConfigProperties" })
protected static class NacosConfiguration {
@Bean
public NacosConfigUpdateListener nacosConfigUpdateListener() {
return new NacosConfigUpdateListener();
}
}
Case two
When I needed to control Feign's calling logic and replace the calling URL, I thought of seeing the source code of Sleuth before. As a link tracking framework, Sleuth integrates many frameworks internally.
For remote calls like Feign, it needs to be extended, and then the link tracking data is transparently transmitted. So when I have similar needs, I can refer to the implementation of Sleuth.
The source code of TracingFeignClient in Sleuth is posted above. TracingFeignClient is an extension of Feign Client in Sleuth, adding some of Sleuth's own logic. Then this TracingFeignClient will eventually replace Feign's default Client when it starts.
Case three
When I needed to monitor Redis, I remembered that I had seen the Redis monitoring code in opentracing before, and I could learn from the method inside.
address:
https://github.com/opentracing-contrib/java-spring-cloud/
Inside, AOP is used to replace RedisConnectionFactory and RedisConnection, without having to move the underlying code of the framework, just extend it.
to sum up
The purpose of writing this article is to tell you that in addition to learning the use of some frameworks, you must also go through the source code when there is nothing to do. Although it may not be used at the time, when you encounter similar problems in the future, you will have a reflection that I have seen similar solutions in the XX framework at that time. This is your knowledge accumulation.
Another point is that some good designs are used in these frameworks, which are also examples that we can learn from.
Finally, in the interview, I also encountered the question: Have you seen the source code of the framework?
If you have read it and remembered it, then you can talk to the interviewer and call it a brother.
About the author : Yin Jihuan, a simple technology enthusiast, author of "Spring Cloud Microservices-Full Stack Technology and Case Analysis", "Introduction to Spring Cloud initiator.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。