Self-praise: Dubbo Implementation Principles and Source Code Parsing — The Best Collection
Praise yourself: D Database Entity Design Collection
Abstract: the original source www.iocoder.cn/Eureka/buil… “Taro source” welcome to reprint, keep the summary, thank you!
This article is mainly based on Eureka version 1.8.X
- 1. Rely on tools
- 2. Source code pull
- 3. Eureka – Server startup
- 3.1 MockRemoteEurekaServer
- Eureka – 3.2 Server war file
- 3.3 Eureka-Server starts directly
- 4. Eureka – Client startup
- 666. The eggs
πππ follow wechat public number:
- RocketMQ/MyCAT/Sharding-JDBC all source code analysis article list
- RocketMQ/MyCAT/Sharding-JDBC δΈζ 解 ε³ source GitHub address
- Any questions you may have about the source code will be answered carefully. Even do not know how to read the source can also ask oh.
- New source code parsing articles are notified in real time. It’s updated about once a week.
- Serious source communication wechat group.
1. Rely on tools
- Gradle
- JDK
- IntelliJ IDEA
Recommended Spring Cloud books:
- Please support the legal version. Download piracy, is equal to the initiative to write low-level bugs.
- DD — Spring Cloud Micro Services
- Zhou Li — “Spring Cloud and Docker Micro-service Architecture Combat”
- Buy two books together, jingdong free delivery.
Spring Cloud
- Java Microservices Practices – Spring Boot
- Java Microservices Practices – Spring Cloud
- Java Microservices Practices – Spring Boot/Spring Cloud
2. Source code pull
From the official warehouse github.com/Netflix/eur… Fork out your own warehouse. Why Fork? Now that we’ve started reading and debugging the source code, we’ll probably write some comments, have our own repository, and be free to commit. π
Pull code from the repository Fork using IntelliJ IDEA. After the pull is complete, Gradle will download the dependencies. This may take some time.
This article is based on the Master branch.
3. Eureka – Server startup
There are three ways to debug eureka-Server. Let’s try each one.
3.1 MockRemoteEurekaServer
Com.net flix. Eureka. AbstractTester, testing an abstract class, has the following implementation subclass:
Use any a subclass of unit test execution can perform Eureka – Server logic to debug, here at com.net flix. Eureka. Resources. ApplicationsResourceTest as an example.
Debug runs the ApplicationsResourceTest#testFullAppsGetJson() unit test. Before the method is executed, ApplicationsResourceTest#setUp() runs to initialize the eureka-server emulation, for example: Com.net flix. Eureka. Mock. MockRemoteEurekaServer (analog eureka – Server).
Because it is a simulated environment, eureka-server is not operated by eureka-client in the way of requesting Eureka-Server, but directly invokes the corresponding method of unit test. Such as:
// ApplicationsResourceTest.java @Test public void testFullAppsGetJson() throws Exception { Response response = applicationsResource.getContainers( Version.V2.name(), MediaType.APPLICATION_JSON, null, // encoding EurekaAccept.full.name(), null, // uriInfo null // remote regions ); String json = String.valueOf(response.getEntity()); DecoderWrapper decoder = CodecWrappers.getDecoder(CodecWrappers.LegacyJacksonJson.class); Applications decoded = decoder.decode(json, Applications.class); // test per app as the full apps list include the mock server that is not part of the test apps for (Application application : testApplications.getRegisteredApplications()) { Application decodedApp = decoded.getRegisteredApplications(application.getName()); assertThat(EurekaEntityComparators.equal(application, decodedApp), is(true)); }}Copy the code
- Direct call
ApplicationsResource#getContainers(...)
Methods.
Conclusion: This way, simple and rough, easy to use. The downside, of course, is simulation. Try this approach when you start debugging Eureka-Server.
Eureka – 3.2 Server war file
First, compile the Eureka-Server war package. This step may take a long time. If the operation fails, try again. The command is as follows:
cd eureka
./gradlew clean build
Copy the code
The second step, the Debug run com.net flix. Eureka. Resources. EurekaClientServerRestIntegrationTest arbitrary unit test method.
Summary: This way, the compilation process is more painful, do not rule out the possibility of failure. Each time you add a registration to your code, you need to recompile the package. Therefore, it is not recommended. So what do we do? Go to the third. Conscience such as the blogger, hurriedly pay attention to the micro channel public number of the blogger: [taro source].
3.3 Eureka-Server starts directly
The first step is to modify the EurekaClientServerRestIntegrationTest# startServer () method, to solve the second way to use the war package run every time modify the code needs to be compiled, implementation code is as follows:
// EurekaClientServerRestIntegrationTest.java private static void startServer() throws Exception { server = new Server(8080); // TODO thread.currentThread ().getContextClassLoader(); WebAppContext webAppCtx = new WebAppContext(new File("./eureka-server/src/main/webapp").getAbsolutePath(), "/"); webAppCtx.setDescriptor(new File("./eureka-server/src/main/webapp/WEB-INF/web.xml").getAbsolutePath()); webAppCtx.setResourceBase(new File("./eureka-server/src/main/resources").getAbsolutePath()); webAppCtx.setClassLoader(Thread.currentThread().getContextClassLoader()); server.setHandler(webAppCtx); server.start(); eurekaServiceUrl = "http://localhost:8080/v2"; }Copy the code
- I am not familiar with Gradle packaging and use
Thread.currentThread().getContextClassLoader().getResource()
Method, has been unable to get the path, there are students who know the trouble to inform.
The second step, the Debug run com.net flix. Eureka. Resources. EurekaClientServerRestIntegrationTest arbitrary unit test method. TODO[0003] : Thread.currentThread().getContextClassLoader()
Conclusion: This way, perfect. You are advised to use this debugging mode.
4. Eureka – Client startup
We use com.net flix. Eureka. ExampleEurekaClient as an example.
The first step, Thread is added to the end EurekaClientServerRestIntegrationTest# setUp () method. The sleep (Long. MAX_VALUE) code.
Step 2 Start the Eureka-server by referring to 3.3 Directly Starting the Eureka-server.
The third step, will EurekaClientServerRestIntegrationTest# injectEurekaConfiguration copied to ExampleEurekaClient class.
The fourth step, in ExampleEurekaClient# main () method of the first row, add injectEurekaConfiguration () code.
Fifth, Debug runs the ExampleEurekaClient#main() method.
The Eureka-examples module provides additional examples that can be debugged one by one.
666. The eggs
This article is written, relatively concise, if you have any questions, you can leave a message to my public number.
Next update? Well, I haven’t figured it out yet, cataloging the entire Eureka source code parsing series.
Fat friends, share a wave of friends can be good!