1 Introduction to the functions of the microservice module
1.1 Service governance
Service governance is the most core and basic module in the microservice architecture. It is mainly used to realize the automatic registration and discovery of each microservice instance.
Spring Cloud Eureka is part of the Spring Cloud Netflix microservice suite, which is based on Netflix Eureka for secondary packaging. Mainly responsible for completing the service governance function in the microservice architecture.
1.2 Service registration
In the service governance framework, a registry is usually built, and each service unit registers its own services with the registry, including some additional information such as the service host and port number, service version number, and communication protocol. The registration center organizes the service list according to the service name. At the same time, it needs to monitor whether the services in the list are available by means of heartbeat detection. If it is not available, it needs to be removed from the service list to achieve the effect of troubleshooting services.
1.3 Service discovery
Under the service governance framework, calls between services are no longer realized by specifying specific instance addresses, but by initiating request calls by service names. The service caller obtains the list of service instances from the service list of the service registry through the service name, and takes out a service instance location to perform the service call through the specified load balancing strategy.
2 Demo build
2.1 Eureka Server builds a service registration center
The project structure is as follows
/src
/main
/java
/com/lerr/baseeureka
BaseEurekaApplication.java
/resources
application.properties
pom.xml
2.1.1 Take Maven as an example to configure project dependencies
- pom.xml (pom.xml introduces related dependencies)
<!-- 使用阿里云配置,下载速度可靠 -->
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<!-- 继承 spring-boot-starter-parent 一些已有的配置 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<!-- 引入 eureka-server表明当前工程是作为服务注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
2.1.2 The main startup class, add the annotation @EnableEurekaServer, and enable the server capability
- BaseEurekaApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer //启动一个服务注册中心,供其他应用进行对话
@SpringBootApplication
public class BaseEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(BaseEurekaApplication.class, args);
}
}
2.1.3 Configuration file configuration
Since it is a service registration center, you do not need to register yourself, eureka.client.register-with-eureka
because it is by default true
, so you need to pay special attention to the declaration asfalse
- application.properties
server.port=1111
eureka.instance.hostname=localhost
# 不向注册中心注册自己
eureka.client.register-with-eureka=false
# 注册中心的职责是维护实例,不需要去检索服务
eureka.client.fetch-registry=false
2.1.4 Start the project and access
Visit http://localhost:1111/
Eureka homepage
2.2 Microservice modules go to the registry to register
The project structure is as follows
/src
/main
/java
/com/lerr/basemsone
BaseMsOneApplication.java
/resources
application.properties
bootstrap.properties
pom.xml
2.2.1 Dependent configuration, pom.xml
- pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 如果是具体业务模块,引入eureka-client依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<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>
2.2.2 Startup configuration
@EnableDiscoveryClient //表明去注册中心注册
@SpringBootApplication
public class BaseMsOneApplication {
public static void main(String[] args) {
SpringApplication.run(BaseMsOneApplication.class, args);
}
}
2.2.3 Configuration file
- application.properties
server.port=8180
- bootstrap.properties
# 设定服务名称
spring.application.name=ms-one
# 服务注册中心的地址,告诉应用去这个地方注册
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
3 Start the service registry and microservices
3.1 Screenshot of the service registration center page
3.2 Log analysis
You can look at the log of the microservice module, which means that you go to the service center to register
2018-08-20 23:17:05.257 INFO 21816 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1534778225257 with initial instances count: 0
2018-08-20 23:17:05.261 INFO 21816 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application ms-one with eureka with status UP
2018-08-20 23:17:05.262 INFO 21816 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1534778225262, current=UP, previous=STARTING]
2018-08-20 23:17:05.264 INFO 21816 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_MS-ONE/F0R6S7J.SU.intra.cpic.com.cn:ms-one:8180: registering service...
At the same time, look at the log of the service registration center. When an application comes to register, the following log will be output
2018-08-20 23:17:05.395 INFO 24168 --- [nio-1111-exec-9] c.n.e.registry.AbstractInstanceRegistry : Registered instance MS-ONE/F0R6S7J.SU.intra.cpic.com.cn:ms-one:8180 with status UP (replication=false)
Author: liuyangcc
link: https: //www.jianshu.com/p/b3014de7c312
Source: Jane books
are copyrighted by the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.