Spring Cloud uses Eureka to do service registration and discovery to unify the management of microservices instances.
1. Create an empty MAVEN project with idea to do the parent module
(You can also use a parallel structure for all modules without the parent project)
Delete Parent Module src folder
can be used Spring Initializr to create a module or create a Maven Project to manually add dependencies
2. Create Eureka Server using spring INITIALIZR
Select SDK 1.8
Artifact fill in the name of the corresponding module
Select Cloud Discovery->eureka server,spring boot by default is 2.0.4
You can refer to Spring Cloud's official version of the corresponding components of the spring cloud version, you can see that the FINCHLEY.SR1 corresponds to spring Boot version 2.0.4.RELEASE
projects.spring.io/spring-cloud/
It will take a while to restore the dependencies of the MAVEN project when it is created.
2.1 Configuring Eureka Server annotations
After a successful restore, a module name +application startup class is generated, and a @enableeurekaserver annotation is added to the class name to assert that it is a Eureka server
2.2 Configuring the Eureka server configuration file
The configuration file can refer to the official document, find the corresponding spring cloud version, click Reference.
Http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html#spring-cloud-eureka-server
Search Find Standalone Mode standalone
Change the application.properties suffix name in the project to YML and copy the contents of the APPLICATION.YML configuration file into the project.
Port for service, can be customized, I changed the port here to 8881
Idea after using debug to start successfully, in the browser input: http://localhost:8881/There is a content that indicates that the service started successfully.
3. Creating a Eureka Server with a maven project
Creates an empty Maven project that is created in the parent module and automatically selects the parent module
Remove all the parent nodes of the Pom files in the project to the spring cloud website to copy the dependencies of the MAVEN project to the project node of the Pom file
The reliance file on the official website is the reliance of the Eureka customer, which removes the dependencies under the Dependencies node.
To add a dependency on Eureka server:
< Dependency > < groupId >org.springframework.cloud</groupId> < Artifactid>spring-cloud-starter-netflix-eureka-server</artifactid > </ Dependency >
Add Spring Cloud Version Properties
<Properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.sr1</spring-cloud.version></Properties>
Add the Application.yml file to the Resources folder and set it to the resource root, the configuration file contents and the first, the lower port is: 8882
Create a new startup class (project name +application.java), write a main method to start the spring boot program, and add Eureka server annotations
Configuring spring Boot Boot Configuration items
Select the corresponding startup class
Start the module, and then browser input: http://localhost:8882/, Eureka Server project created with Maven also started successfully
4. Create a Eureka server cluster
The role of a cluster is to replicate multiple instances of a service, and when one instance hangs up without affecting the entire system, each instance in the Eureka server cluster is replicated to ensure that the services that are registered are consistent.
In the actual production environment, as long as one Eureka server is configured with different ports for each instance, for demonstration purposes, I create the cluster here using the two different Eureka servers that I just created.
Search in Official documents: Peer Awareness
Http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html#spring-cloud-eureka-server-peer-awareness
Find the corresponding cluster configuration
Modify the host file, add 127.0.0.1 peer1 peer2, and set the service name as
Service 1 configuration:
Server: Port:8881eureka: instance: hostname:peer1 client: registerwitheureka:true # Register yourself as a client to another Eureka server fetchregistry:true #在本地缓存所有实例注册信息 serviceurl: #defaultZone: http://${ eureka.instance.hostname}:${server.port}/eureka/the address #设置与Eureka server interaction, both the query service and the registration service need to rely on this address. # #指向另一个注册中心 defaultzone:http://peer2:8882/eureka/spring: #profiles: Peer1 #没做环境配置, commented out Application: name:tmsapi-discovery-server
Service 2 configuration:
Server: Port:8882eureka: instance: hostname:peer2 client: registerwitheureka:true # Register yourself as a client to another Eureka server fetchregistry:true #在本地缓存所有实例注册信息 serviceurl: #defaultZone: http://${ eureka.instance.hostname}:${server.port}/eureka/the address #设置与Eureka server interaction, both the query service and the registration service need to rely on this address. # #指向另一个注册中心 defaultzone:http://peer1:8881/eureka/spring: #profiles: Peer2 #没做环境配置, commented out Application: name:tmsapi-discovery-server
Launch two services, successful browser input: http://peer1:8881/and http://peer2:8882/, there are two instances in the same application.
Peer1:
Peer2:
This Eureka server cluster was built successfully.
Eureka Related information:
53131023