Use Eureka to build a registry
- Introduction to SpringCloud and Maven
Maven: Two core functions of Maven: dependency management (jar package management); building projects (project packaging).
Products of the same type: ant and gradle. Ant has basically died out. Gradle and maven coexist, the number of gradle users has been increasing, and the domestic environment is still dominated by maven.
Core products: SpringBoot and SpringCloud. SpringBoot is a single application development framework. SpringCloud is a microservice framework for managing multiple SpringBoot applications. - Use spring initializr to create spring cloud project
The difference between Spring Initializr and Maven: Use "spring initializr" to create a project, you can intuitively manually check the dependencies, after the project is successfully created, the pom.xml will have its own dependencies (as shown below);
use "Maven" to create a project, you need to Add dependencies to the pom.xml folder (suitable for use in poor network environments).
- Transform the project into a Maven parent-child structure
Give it to the git management page, start the SpringBoot project, and find that there is an error.
We are now a single SpringBoot project, we want to transform our Maven into a parent-child project.
- New module
- Create the eureka module.
- Move the eureka module of the parent pom to the child module.
- Finally, start it and you can successfully use Eureka to build a registry.
Maven parent-child module structure: The parent framework only needs one pom.xml, and the sub-module is a normal maven project. Then the submodule is a springboot project
Note:
If com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect is reported.
- This is because eureka will register itself as a client in the default configuration. At startup, it will register itself as a client at the same time. And in the process of starting. Even if the server itself is also the client. The server hasn't been started yet, and the client will definitely report an error if it finds it!
spring.application.name=eureka
server.port=8761 #Whether to
register yourself to Eureka Server, the default is true
eureka.client.register-with-eureka=false #Whether
to obtain registration information from Eureka Server, the default is true
eureka .client.fetch-registry=false
The solution is to add this code in application.properties.
And add @EnableEurekaServer annotation to the startup class
There is one thing to pay attention to. If a plurality of sub-modules, it needs to be registered with the service registry, Step 2: Add Configuration, point to registry address
eureka.client.service-url.defaultZone = http: // localhost : 8761 / eureka /
but also in Add the annotation
@EnableEurekaClient to the startup class