Micro Services Architecture: spring cloud of service registration and service discovery

The main module SpringCloud include: service discovery (Eureka), circuit breaker (Hystrix), there is a smart way (Zuul), the registry client load balancing (Ribbon), Archaius, Turbine, Eureka micro services.

1.Eureka Profile

Eureka Spring Cloud Netflix is ​​a sub-module, it is also one of the core modules. Drive for service discovery, a REST-based services, for location services, the intermediate layer in order to achieve the cloud service discovery and failover.

Service registration and discovery is very important for micro-service systems. With service discovery and registration, you do not need to change the day of the service call configuration file, you only need to use the service identifiers, you can access to the service. His function is similar to the registration center dubbo (register).

Service Discovery: Service discovery is one of the key principles of micro-services infrastructure. Trying to proceed to configure each client or some form of agreement can be said to be very difficult and very fragile. Eureka is the discovery of a service Netflix service and client. Such services can be configured for high availability and deployment, and among the registered service, the status of each service may be replicated to each other.

Service Registration: When a client registers to Eureka, which provides on its own metadata (such as host and port health indicators URL, home, etc.) Eureka receive a heartbeat message from a service by each instance. If it fails to receive a heartbeat longer than the configured, examples of which will normally be removed from the register

Below is a basic service registration and discovery

2.Eureka service discovery and registration (create registry)

(A): create a foundation of Spring Boot project, and the introduction of content-dependent needed in pom.xml

<?xml version="1.0" encoding="UTF-8"?>
	<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
		<modelVersion>4.0.0</modelVersion>

		<groupId>com.demo.springcloud</groupId>
		<artifactId>eureka_register_service</artifactId>
		<version>1.0.0</version>
		<packaging>jar</packaging>

		<name>eureka_register_service</name>
		<description>Spring Cloud project</description>

		<parent>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-parent</artifactId>
			<version>1.4.3.RELEASE</version>
			<relativePath /> 
		</parent>

		<properties>
			<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
			<java.version>1.8</java.version>
		</properties>

		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-test</artifactId>
				<scope>test</scope>
			</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-security</artifactId>
			</dependency>
		</dependencies>

		<dependencyManagement>
			<dependencies>
				<dependency>
					<groupId>org.springframework.cloud</groupId>
					<artifactId>spring-cloud-dependencies</artifactId>
					<version>Brixton.RELEASE</version>
					<type>pom</type>
					<scope>import</scope>
				</dependency>
			</dependencies>
		</dependencyManagement>

		<build>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
				</plugin>
			</plugins>
		</build>

	</project>	
复制代码

(B): to create a startup class Application

package com.demo.springcloud;

 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

 @EnableEurekaServer
 @SpringBootApplication
 public class Application {
 	public static void main(String[] args) {
 	    SpringApplication.run(Application.class, args);
 	}

 }
复制代码

By @EnableEurekaServer annotations begin a dialogue to provide service registry to other applications.

(C): Create a profile application.properties, be careful not to use spaces, whether to start the error

 server.port=8000
 eureka.client.register-with-eureka=false
 eureka.client.fetch-registry=false
 eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
复制代码

(D): performing bluid.sh constructed, and then execute the main method. Since I did not manually compile in eclipse, the start time has not read application.properties.

(e): See registry http://127.0.0.1:8000/ can see the following FIG.

Is not very simple ah, then people have to ask, why even the login ID and password are not, the external network can not directly access the registration center, so very safe ah. Well, then we join login ID and password

3.Eureka registry permissions added

(A): adding registry need to introduce jar, added in pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>
复制代码

(B): configuration file to create a new file bootstrap.yml

security:
    basic:
      enabled: true
    user:
      name: admin
      password: 123
复制代码

Remember recompile package and then execute the main method.

4. Register a Service

First of all to remove the registry permissions to join, otherwise the server will not link to the registration center to register.

Execution demo should be able to see the results above, below is serving BIZ-SERVICE-0 registry found, the figure is exposed service interface

Guess you like

Origin blog.csdn.net/weixin_34349320/article/details/91398064