related
-
Spring Cloud Series 1 – Service Registration and Discovery Eureka
-
Spring Cloud – Client calls Rest + Ribbon
-
Spring Cloud Combat Series iii – Declarative client Feign
-
Spring Cloud Series iv – Fuse Hystrix
-
Spring Cloud Combat Series 5 – Service Gateway Zuul
-
Spring Cloud Deployment Series (6) – Distributed Configuration center Spring Cloud Config
-
Spring Cloud Combat Series (7) – Service Link Tracking Spring Cloud Sleuth
-
Spring Cloud Combat Series (8) – Micro service monitoring Spring Boot Admin
-
Spring Cloud OAuth 2.0
-
Single sign-on JWT and Spring Security OAuth
preface
Spring Cloud encapsulates the Eureka module developed by Netflix for service registration and discovery. Eureka adopts the C-S design architecture. Eureka Server serves as the service registry, and other micro-services in the system use Eureka clients to connect to Eureka Server and detect the service survival status through heartbeat connections.
The body of the
-
Eureka Server: Acts as a service registry for service registration and discovery.
-
Eureka Client: All services registered with the service center.
-
Service Provider: Registers its own services with Eureka Server so that Service consumers can find them.
-
Service Consumer: Gets a list of Service registrations from Eureka Server to be able to consume services.
-
1. Create a service registry
Create two project modules, one Module (namely Spring Boot) project as the service registry, namely Eureka Server, and the other as Eureka Client.
After Eureka Server is created, the project pom. XML file is as follows:
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3. RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<groupId>io.ostenant.github.springcloud</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
</properties>
<dependencies>
<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>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
2. Start the service registry
Eureka is a highly available component that has no back-end cache. After each instance is registered, a heartbeat needs to be periodically sent to the registry (so it can be done in memory). By default, Eureka Server is also a Eureka Client and a Server must be specified. Before starting Eureka Server, first configure the application.yml file.
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Copy the code
- eureka.client.register-with-eureka:
Set whether to register with the Eureka Server as a Eureka Client. The default value is true.
- eureka.client.fetch-registry
Sets whether to obtain registration information from Eureka Server. The default value is true. Because this example is a single Eureka Server and does not need to synchronize data from other Eureka Server nodes, set it to false.
- eureka.client.service-url.defaultZone
Set the address for interaction with the Eureka Server. The query and registration services depend on this address. If there are multiple addresses, separate them with commas.
Add @enableeurekaserver to the Application class of Spring Boot project:
@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {
public static void main(String[] args) { SpringApplication.run(EurekaserverApplication.class, args); }}Copy the code
Eureka Server has an interface. After starting the project, open a browser and visit http://localhost:8761 to view it.
3. Create a service provider
When a Eureka Client initiates registration with the Eureka Server, it provides metadata such as host and port. Eureka Server receives heartbeat messages from each Eureka Client instance. If the heartbeat times out, the instance is usually removed from Eureka Server.
Create a service-hi Module with the following 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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3. RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<groupId>io.ostenant.github.springcloud</groupId>
<artifactId>service-hi</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<name>service-hi</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-cloud-starter-web</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
Indicate that you are a EurekaClient by annotating @enableEurekaclient.
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {
@Value("${server.port}")
private String port;
public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args);
}
@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "Hi " + name + ", I am from port: "+ port; }}Copy the code
@enableeurekaclient is not enough. You also need to specify the address of the service registry in the configuration file.
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8763
spring:
application:
name: service-hi
Copy the code
The Eureka client needs to specify Spring.application. name, a unique identifier for the service, based on which services will call each other.
Start the Eureka Server and access the Eureka Server address http://localhost:8761. The SERVICE name service-hi and port 7862 are registered in the Eureka Server list.
3. Eureka Server is highly available
In a distributed system, the service registry is the most important part of the foundation and must be in a state to provide services. To maintain its availability, using clusters is a good solution.
Eureka implements high availability deployment through peer node registration. Therefore, you only need to configure the serviceUrl of other available Eureke Servers for each Eureke Server to achieve high availability deployment.
spring:
profiles:
active: peer1 #peer2
---
spring:
profiles: peer1
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/
---
spring:
profiles: peer2
server:
port: 8762
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
Copy the code
Change the configuration file of the Eureka Server and start the Eureka Server twice using Spring.profiles. Active =peer1 and spring.profiles. Active = Peer2 as parameters respectively.
-
Go to http://localhost:8761/. You can see that the Eureka Client has registered with the Eureka Server on port 8761.
-
The service provider’s profile is not registered with Eureka Server on port number 8762. Go to http://localhost:8762/. It can be found that the service provider’s information has been initiated to register 8762, that is, the registration information of 8761 has been synchronized to 8762 node.
reference
- An In-depth Understanding of Spring Cloud and Microservice Construction by Zhipeng Fang
Related articles
-
Spring Cloud Series 1 – Service Registration and Discovery Eureka
-
Spring Cloud – Client calls Rest + Ribbon
-
Spring Cloud – Declarative client calls to Feign
Welcome to pay attention to the technical public number: Zero one Technology Stack
This account will continue to share learning materials and articles on back-end technologies, including virtual machine basics, multithreaded programming, high-performance frameworks, asynchronous, caching and messaging middleware, distributed and microservices, architecture learning and progression.