Table of Contents of Series Articles
Chapter 1 Application of Java Thread Pool Technology
Chapter 2 Application of CountDownLatch and Semaphone
Chapter 3 Introduction to Spring Cloud
Chapter 4 Spring Cloud Netflix-Eureka
Article directory
- Table of Contents of Series Articles
-
- Preface
- 1. Two major components of Eureka
- 2. Eureka service registration and discovery
- 3. Case
-
- 3.1. Create the main project
-
- 3.2. Create sub-public module common-api
-
- 3.3. Create the Eureka registration center module eureka-server
-
- 3.4. Create the user service module user-service! [Insert picture description here](https://img-blog.csdnimg.cn/e52d0b166629469baa1cd934714c9831.png)
-
- 3.5. View the compiled package
- summary
Article directory
- Table of Contents of Series Articles
- Preface
- 1. Two major components of Eureka
- 2. Eureka service registration and discovery
- 3. Case
-
- 3.1. Create the main project
- 3.2. Create sub-public module common-api
- 3.3. Create the Eureka registration center module eureka-server
- 3.4. Create the user service module user-service! [Insert picture description here](https://img-blog.csdnimg.cn/e52d0b166629469baa1cd934714c9831.png)
- 3.5. View the compiled package
- summary
Preface
Today we explain the first generation implementation of Spring Cloud microservices: Spring Cloud Netflix
Eureka is an open source service registration and discovery component developed by Netflix.
Spring Cloud uses Spring Boot ideas to add automated configuration to Eureka. Developers only need to introduce relevant dependencies and annotations to easily integrate the microservices built by Spring Boot with Eureka.
Attached is a simple Netflix microservice architecture diagram (Zuul is too troublesome to be directly replaced by Gateway):
Alibaba microservice architecture diagram:
If you want to switch between Netflix and Alibaba, you can look at the picture below and replace the relevant components. The core business code is basically not too complicated. Big change.
1. Two major components of Eureka
Eureka adopts CS (Client/Server, client/server) architecture, which includes the following two components: Eureka Server, Eureka Client
components | introduce |
---|---|
Eureka Server | Eureka service registration center, mainly used to provide service registration functions |
Eureka Client | Eureka client usually refers to each microservice in the microservice system |
2. Eureka service registration and discovery
Function | introduce |
---|---|
Register Service | It is an Eureka Server that provides service registration and discovery functions. |
Provider Service | It is an Eureka Client used to provide services. It registers the services it provides to the service registration center for discovery by service consumers. |
Consumer Service | It is an Eureka Client, used to consume services. It can obtain the service list from the service registration center and call the required services. |
3. Case
3.1. Create the main project
Name: SpringCloud
3.1.1. Main project pom.xml configuration
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hqyj</groupId>
<artifactId>drp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>drp-parent</name>
<description>Demo project for Spring Boot</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.2. Create sub-public module common-api
3.2.1. Add module
3.2.2, pom.xml configuration
<?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>
<parent>
<groupId>com.hqyj</groupId>
<artifactId>SpringCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>common-api</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
3.2.3. For maven non-springboot projects, add main entrance
Add Main.java and specify the main entrance to prevent Maven package/install from failing.
public class Main {
public static void main(String[] args) {
System.out.println("common-api");
}
}
3.3. Create the Eureka registration center module eureka-server
3.3.1. Configure 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>
<parent>
<groupId>com.hqyj</groupId>
<artifactId>SpringCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>eureka-server</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skip-tests>true</skip-tests>
</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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.enesusta</groupId>
<artifactId>spring-devtools</artifactId>
<version>1.0.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
3.3.2. Configure application.yml
In the resource directory, create a new application.yml file
Edit the application.yml file and add eureka configuration
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称,
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${
eureka.instance.hostname}:${
server.port}/eureka/ #单机版服务注册中心
3.3.3. Start eureka-server
Create the EurekaServerApplication.java startup file
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
3.3.3.1. Compile eureka-server
3.3.3.2. Run the EurekaServerApplication.java file
Start: http://localhost:7001/
3.4. Create user service module user-service
3.4.1. Configure 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>
<parent>
<groupId>com.hqyj</groupId>
<artifactId>SpringCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>user-service</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--devtools 开发工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--Spring Boot 测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--junit 测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.hqyj</groupId>
<artifactId>common-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
3.4.2. Configure application.yml
server:
port: 8001
spring:
application:
name: user-service #微服务名称
eureka:
client: #将客户端注册到 eureka 服务列表内
service-url:
defaultZone: http://localhost:7001/eureka #这个地址是 7001注册中心在 application.yml 中暴露出来额注册地址 (单机版)
3.4.3. Start user-service
Create UserApplication.java startup file
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
3.4.3.1. Compile user-service
3.4.3.2. Run the UserApplication.java file
3.4.3.3. Testing
http://localhost:8001/user/userInfoList
3.5. View the compiled package
summary
Eureka, as an open source service registration and discovery component developed by Netflix, is still very classic. It is also very good in terms of service registration and discovery. Interested students can download the source code of Eureka and study it to see how Eureka implements service registration and how the caller discovers and calls services. Finally, let’s take a look at the same functions in Eureka. How to implement load balancing calls by serving multiple machine instances.