What does 1.eureka do?
The last article said that the microservices need to communicate with each other, then the communication requires a variety of network information, we can use hard-coded way to communicate, but this is obviously inappropriate, it is impossible to say that a microservices address changes, then the entire system of all microservices are redeployed, obviously inappropriate, Then we need a service discovery mechanism, the service consumer through this mechanism to obtain the service provider's network information, and the service provider's network information, even if the change, service consumers do not have to change the configuration. Eureka provides a service registration and discovery function, that is, all services at the start of time need to put their own network information to Eureka for all services to use
Relationship between service providers, service consumers, and Eureka
(1) When the service provider starts, it registers its network information with the Eureka,eureka to store the information.
(2) The time between the service provider and the Eureka Heartbeat tells Eureka I am still, if the heartbeat is not sent for a long time, then it means that the service has been stopped, it will log off the service
(3) The service consumer can query the network address of the service provider through the Eureka, and call the service provider's interface through the address.
2. Writing Eureka Server
We all use MAVEN to manage dependencies.
1. Create a MAVEN project and add the dependent
I am using the latest version, for different Springcloud version and Springboot compatibility, see more 5
1<!--add Eureka-server dependency2<dependency>3<groupId>org.springframework.cloud</groupId>4<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>5</dependency><!--Add eureka-client dependencies, this dependency can be used here without adding, if but if multiple Eureka servers need to be added, because Eureka need to be registered with each other--6<dependency>7<groupId>org.springframework.cloud</groupId>8<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>9</dependency>Ten<!--introduction of Springcloud dependency-- One<dependencyManagement> A<dependencies> -<dependency> -<groupId>org.springframework.cloud</groupId> the<artifactId>spring-cloud-dependencies</artifactId> -<version>Finchley.SR1</version> -<type>pom</type> -<scope>Import</scope> +</dependency> -</dependencies> +</dependencyManagement> A at<!--Note: This must be added here, otherwise the problem with various dependencies is that the current partial dependency is not uploaded to the central warehouse-- -<repositories> -<repository> -<id>spring-milestones</id> -<name>spring milestones</name> -<url>http://repo.spring.io/milestone</url> in<snapshots> -<enabled>false</enabled> to</snapshots> +</repository> -</repositories>
Click to view Code2. Writing the configuration
1Server2port:80803Tomcat:4Uri-encoding:utf-85Eureka6Client7#是否将自己注册到Eureka Server, Default True, set to False because the current application is Eureka8Register-with-eureka:false9#表示是否从Eueka Server Gets the registration information, by default, is set to False because it is currently a single-node Eureka server and no other Eureka serverTenFetch-registry:false OneService-url: A#这里是交互的地址, all service registration and query services need to pass this address, where you can set multiple addresses, separated by commas -Defaultzone:http://localhost:8080/eureka/ -Server theEnableselfpreservation:false#关闭做我保护模式 -
3. Writing the Startup class
@SpringBootApplication @enableeurekaserver // Start a service registry to provide conversations for other apps Public class Touristregistercenter { publicstaticvoid main (string[] args) { Springapplication.run (touristregistercenter. class , args);} }
The whole startup class just adds a @enableeurekaserver annotation.
4. Start
And then we can see this page, which means congratulations on your success.
But pay attention to see instances currently registered with Eureka This is our registered service, this is empty (nonsense, I have not registered it), the page content English a little better look should understand, really can't Baidu translate it
5. About versions
About the Springcloud version
This is only the Springcloud version, because Springcloud is based on Springboot, so the Springcloud and springboot versions best correspond, the details can be seen on GitHub springcloud information, Attached address https://github.com/spring-projects/spring-cloud/wiki/Spring-Cloud-Finchley-Release-Notes
6. End
In fact, the summary of Eureka Server is
1. Add dependencies
2. Configuration
3. Add annotations to the startup class stating that this is a Eureka server
It is so simple ...
Springcloud (ii) service registration and Discovery Eureka