first, Introduction
Service consumers need a robust service discovery mechanism that service consumers use to obtain network information from service Providers. Even if the service Provider's information changes, The service consumer does not need to modify the Configuration.
The relationships between service providers, service consumers, and service discovery components are as Follows:
1. at startup, each microservices registers information such as its network address in the service discovery component, which is stored by the service discovery Component.
2. The service consumer can query the service Provider's network address from the service Discovery component and use that address to invoke the interface of the service Provider.
3. Each microservices and services discovery component uses a mechanism (such as a heartbeat) to communicate that a service discovery component will log off the instance if it cannot communicate with a micro service instance for a long time.
4. When a microservices network address is changed, it is re-registered to the service discovery Component.
1.1. Service Discovery Component
Service discovery components should have the following Features: service registry, service registration and service discovery, service check
1.1.1, Service Registration Form
Is the core of the service discovery component, which is used to record the information of each micro-service, such as service name, IP, port, etc. The Service registry provides the query API (for querying available MicroServices instances) and the management API (registration and logoff of user services)
1.1.2, Service registration and service discovery
Service registration refers to the process by which a microservices registers its own information with the service Discovery component at Startup. Service discovery refers to the mechanism of querying the list of available microservices and their network addresses.
1.1.3, Service Check
The service discovery component uses a mechanism to periodically detect registered services, removing instances from the service registry if an instance is found to be inaccessible for an extended period of time
Spring Cloud provides support for a variety of service discovery components, such as Eureka, zookeeper, and More.
second, Eureka
Eureka is the Netflix open source service Discovery component, which is itself a rest-based service. It contains both server and client Parts. Spring Cloud integrates it into the subproject spring cloud Netflix to enable the registration and discovery of Microservices.
2.1. write Eureka Server
2.1.1 New Springboot Project
Create a new Springboot (1.5.4.RELEASE) project and add the following dependencies:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency > </dependencies></dependencyManagement><dependencies> <dependency> <groupId> Org.springframework.cloud</groupid> <artifactid> spring-cloud-starter-config</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-eureka-server</artifactid> </ Dependency></dependencies>
2.1.2, Writing startup classes
Package Com.example.demo;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.cloud.netflix.eureka.server.enableeurekaserver;@ Springbootapplication@enableeurekaserverpublic class eurekaserverapplication {public static void main (string[] Args) { Springapplication.run (eurekaserverapplication.class, args);}}
2.1.3, configuration file Application.properties
server.port=9090eureka.client.register-with-eureka=falseeureka.client.fetch-registry= falseeureka.client.service-url.defaultzone=http://localhost:9090/eureka/,http://localhost:9090/eureka1/
Description
Eureka.client.register-with-eureka: indicates whether to register itself with Eureka server, which is true by Default. (since This project itself is Eureka server, all write False here)
Eureka.client.fetch-registry: indicates whether to get information from Eureka server, default to True (because this project is now a single node, all write false here)
Eureka.client.service-url.defaultzone: set the address that interacts with Eureka server, both the query service and the registration service need to rely on this address, and multiple comma-delimited
2.1.4, Testing
Visit: http://localhost:9090/
650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M01/99/D4/wKiom1lM2Q-hphqpAAFpSLZ5odg845.jpg "title=" 1498208594 (1). jpg "alt=" wkiom1lm2q-hphqpaafpslz5odg845.jpg "/>
2.2. Register the MicroServices on the Eureka server
2.2.1 New Springboot Project
Create a new Springboot (1.5.4.RELEASE) project and add the following dependencies:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency > </dependencies></dependencyManagement><dependencies> <dependency> <groupId> Org.springframework.cloud</groupid> <artifactid> spring-cloud-starter-config</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency></dependencies>
2.2.2, Writing startup classes
Package Com.example.demo;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient, @EnableEurekaClient @springbootapplicationpublic Class eurekaclientapplication {public static void main (string[] Args) {springapplication.run ( eurekaclientapplication.class, args);}}
2.2.3, configuration file Application.properties
server.port=9091spring.application.name=demo1eureka.client.service-url.defaultzone=http://localhost:9090/ Eureka/eureka.instance.prefer-ip-address=true
2.2.4, Testing
Visit: http://localhost:9090/
650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M02/99/D4/wKioL1lM3YWhSF8VAAGmwN3EGZQ068.jpg "title=" 1498209737 (1). jpg "alt=" wkiol1lm3ywhsf8vaagmwn3egzq068.jpg "/>
If you see this hint on the first page of Eureka server, the Eureka has entered protection mode:
emergency! EUREKA may incorrectly claiming INSTANCES is up when the They ' RE Not. Renewals is LESSER THAN THRESHOLD and HENCE the INSTANCES is not BEING EXPIRED JUST to be SAFE.
Resolution: Displays the specified IP
server.port=9091spring.application.name=demo1eureka.client.service-url.defaultzone=http://localhost:9090/ eureka/eureka.instance.prefer-ip-address=true# Display specified ipeureka.instance.instance-id: ${ spring.cloud.client.ipaddress}:${server.port}
650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M02/99/D4/wKiom1lM3zCT7_U3AAF1Ae2gecU507.jpg "title=" 1498210163 (1). jpg "alt=" wkiom1lm3zct7_u3aaf1ae2gecu507.jpg "/>
This article is from "i love big gold" blog, please be sure to keep this source http://1754966750.blog.51cto.com/7455444/1941405
Springcloud (3): micro-service Registration and Discovery (Eureka)