Directory
One: Build the project
Second: Service Registration and discovery
Why choose Eureka, please see a blog Eureka--talking about Eureka
Project Building
Idea Select New Project to select Spring initializr
Fill in group, artifice project name, tick type Gradle Config (this is based on Gradle implementation)
Select components: According to their own needs to choose, here need Eureka to do as a registration center, so I first ticked the line of the Eureka two service dependencies
Use subprojects in Bulid.gradle to unify the configuration of all child items after creation is complete
subprojects{} role: When using multi-module projects, different modules have the same configuration, resulting in duplicate configuration, you can extract the same parts, using configuration injection technology to complete the configuration of the sub-project. The root project is like a container, and the subprojects method iterates through all the elements of the container and injects the specified configuration.
Service Registration and Discovery Registration Center
First, create a modules module, which operates as follows: Right-click Project New Module
Choose Gradle (If you build with Maven, choose Maven)
Input module Name Register-center
Complete the module creation and add the Eureka Registration service dependency in the Bulid.gradle of the Submodule
Dependencies { compile (' org.springframework.cloud:spring-cloud-starter-netflix-eureka-server ') }
Create a startup class in main registerapplication (note adding @enableeurekaserver annotations to indicate that you are a registry)
@SpringBootApplication @enableeurekaserver // indicate that you are a registered center Public class registerapplication { publicstaticvoid main (string[] args) { Springapplication.run (registerapplication. class , args);} }
Add a APPLICATION.YML profile to resources
Spring: application: name:register-centerserver: Port:10000eureka: client: service-url: defaultzone:http://localhost:10000/eureka# Default address register-with-eureka:false# not registered at startup indicates that you are a registry Fetch-registry:false
View Code
The Eureka Port address is customized here
Its Eureka default port address is 8761
Open Eureka Address Find yourself (register-center) has been registered into, and we initially just let it as a registry is mismatched
Can be found now is not configured Register-with-eureka:false Fetch-registry:false these two lines of code Eureka also register themselves in, add the two lines of code into the configuration file again refresh Eureka found that they are no longer the note Book Center.
Service discovery
Ibid. create a module (order-client) to add a dependency of Eureka client in its Bulid.gradle
Dependencies { compile (' org.springframework.cloud:spring-cloud-starter-netflix-eureka-client ') }
Create a Startup class Orderclientapplication (add @enablediscoveryclient//to represent yourself as a Eureka client)
// indicate that you are a Eureka client Public class orderclientapplication { publicstaticvoid main (string[] args) { Springapplication.run (orderclientapplication. class , args);} }
Add APPLICATION.YML configuration There is no need to write Register-with-eureka:false Fetch-registry:false
It should be noted here that starting the Eureka Server (Register-center) starts, and then starting the client (order-client), if the server is not started first (you can also add Register-with-eureka:false Fetch-registry:false resolution But this becomes the service side, not what we want, the client will report the following error
Here is involved in the characteristics of Eureka: that is, the service side, but also the client, follow-up will be on the Eureka registration process for an analysis to learn, please pay attention to Bo Master O, here first do not explain
Start the server and start the client, refresh the registry, and discover that the client is already registered.
At this point, Eureka's service registration has been implemented.
Springcloud Learning--eureka Service Registration and discovery