Eureka (service registry), including the registration of the service function call, fuse, demotion, load and so on.
With the relationship between the service center project what changes it, with a few pictures to explain (N, find a good follow-up on the drawing software):
So it seems to have not see what a simple place, but when the project up to a dozen or dozens of projects when the relationship between the project is very complicated, and when there is a service IP address changes when associated with several services have to rely address corresponding changes, which cause great inconvenience to the person in the actual development operation and maintenance. When all the services are performed by the registry call, we do not care about the specific services of IP addresses, only need to apply for the service center can be, and how to call the service center is its own thing, and it can be achieved with the service center many of the advanced features, such as load balancing.
Creating SpringCloud project with maven in the development, implementation, infrastructure development SpringCloud by new Springboot module. Reliance should be introduced include at least SpringBoot, SpringCloud, I use the version 1.5.3 and below are specific dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>Dalston.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
There are two components in the Eureka: Eureka Eureka server and the client, the server used to register the server, java client is a client, to simplify the interaction with the server, polls the load balancer, and provides service failure switching support. Use a diagram to explain the relationship between the process of calling Eureka (N) achieved:
Three roles:
Eureka-Service: Registration Service Center
Service-Provider: service provider, the service has registered the call center for consumers
Service-Consumer: consumer services, access to other services from the service center
One. Implementation of the registration service center
1. Add dependence
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2. Profiles
spring.application.name=spring-cloud-eureka server.port=8000 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
In the default configuration, the service center will also be registered as their own client, it needs to be disabled,
eureka.client.register-with-eureka = false Indicates whether to register themselves to the service center, default true
eureka.client.fetch-registry = false Indicates whether to obtain a list of services from a service center, default true
3. Add notes
In the startup class, add annotations @EnableEurekaServer
@SpringBootApplication @EnableEurekaServer public class SpringCloudEurekaApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudEurekaApplication.class, args); } }
4. Cluster
As the core components of micro-services, all services are dependent on this assembly, single component configuration is obviously very risky, and therefore it is necessary to configure a cluster in the actual production environment for at least two more service centers to be configured to form clusters when one of these services dawdle away, another service can continue to provide services
The following code backup register (host file in System32) in the host file
127.0.0.1 peer1 127.0.0.1 peer2 127.0.0.1 peer3
Profiles
--- spring: application: name: spring-cloud-eureka profiles: peer1 server: port: 8000 eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/ --- spring: application: name: spring-cloud-eureka profiles: peer2 server: port: 8001 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1:8000/eureka/,http://peer3:8002/eureka/ --- spring: application: name: spring-cloud-eureka profiles: peer3 server: port: 8002 eureka: instance: hostname: peer3 client: serviceUrl: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/
Respectively peer1, peer2, peer3 start service center
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2 java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3
Log browser: http: // localhost: 8080 /, you can see the other two service centers have been registered, bringing the cluster is complete.