Microservices Architecture: Spring-Cloud
What are microservices?
Microservices are a way to split up all the modules of a bloated project and make them unrelated to each other, even without using the same database. Such as: There are User module and Power module in the project, but the User module and Power module are not directly related, just some data need to be exchanged, so we can separate these two modules. When the User needs to call Power, Power is a service party. But when power needs to call the user, the user is the server, so they don’t care who is the server and who is the caller, they are two independent services, at this point, the concept of microservices came into being.
Classic question: The difference between microservices and distribution
Talk about the difference, we talk simple first what is distributed, the so-called distributed, is the large system is divided into multiple modules (like this and services) to deploy on different machines (because a machine could not afford so much pressure or the cost of a very good server could be several ordinary), Each module interacts with each other through interfaces. In fact, distribution is also a kind of micro service. Because both modules are broken up into separate units that provide interfaces to call, what is the essential difference between them? The main difference is in “goals,” which is what you’re trying to do with your architecture. What is the goal of distribution? As we have just seen, even if one machine cannot afford it, or because of the cost, multiple machines have to be used to complete the deployment of the service. The goal of microservices is to separate the modules without affecting each other, such as module upgrades or bugs.
Speaking so much, can use a sentence to understand: distributed is also a kind of micro-service, and micro-service it can be on a machine.
Relationship between Microservices and Spring-Cloud (difference)
Microservices are just a way of architecture for a project, or a concept, like our MVC architecture, and Spring-Cloud is an implementation of this technology.
Do microservices have to use Spring-Cloud?
We have just said, the service is just a way of architecture of the project, if you know what is micro service concept enough you will know, in fact the micro service even if without any technology can also achieve, just have a lot of problems we need to solve, for example: load balancing, service registration and discovery of service invocation, the road by… And so on and so forth, and so forth, and so forth, and spring-Cloud comes out, and spring-Cloud packs all the technology to deal with these problems, and it’s kind of an open bag kind of thing.
Setup of spring-Cloud project
Since spring-Cloud is based on the Spring-boot project, our project has to be a spring-boot project. As for the spring-boot project, we won’t discuss it in this section. One point to note here is that the spring-Cloud version corresponds to the spring-Boot version as shown below:
In my case my version looks like this
Spring – the boot:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2. RELEASE</version>
</parent>Copy the code
spring-cloud:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Copy the code
When you have these dependencies in your project, your Spring Cloud project is set up (it may take a while to download Spring-Cloud for the first time)
Spring – Cloud component:
Eureka:
What is eureka?
Eureka is one of the submodules of Netflix and is also a core module. There are two components in Eureka, one is EurekaServer(an independent project) and this is used to locate services to implement load balancing and failover of middle-tier servers The other is EurekaClient (our microservice), which is used to interact with the Server and makes the interaction very simple: you just need the service identifier to get the service.
Relationship to Spring-Cloud:
Spring Cloud encapsulates the Eureka module developed by Netflix to implement service registration and discovery (which can be compared to Zookeeper).
Eureka adopts the C-S design architecture. Eureka Server serves as the Server for the service registry function. It is the service registry.
Other microservices in the system use Eureka’s clients to connect to Eureka Server and maintain heartbeat connections. In this way, the maintenance personnel of the system can monitor the normal operation of each micro-service in the system through Eureka Server. Some of SpringCloud’s other modules, such as Zuul, can use Eureka Server to discover other microservices in the system and perform related logic.
Role diagram:
How to use it?
Add dependencies to spring-Cloud projects:
Eureka client:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<dependency>Copy the code
Eureka server:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<dependency>Copy the code
Add the following configuration to the Eureka server project:
server:
port: 3000
eureka:
server:
enable-self-preservation: false # Turn off self-protection mechanisms
eviction-interval-timer-in-ms: 4000 Set the cleanup interval (default: 60*1000)
instance:
hostname: localhost
client:
registerWithEureka: false # Do not register yourself as a client
fetchRegistry: false # do not need to obtain the registration information from the server.
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eurekaCopy the code
Of course, it’s not all necessary, but I’m just copying my configuration here
Then annotate the spring-Boot startup project with @enableeurekaserver to start the project
@EnableEurekaServer
@SpringBootApplication
public class AppEureka {
public static void main(String[] args) {
SpringApplication.run(AppEureka.class); }}Copy the code
If you see this picture, you’ve built it:
The warning just means you turned off his self-protection mechanism
Eureka client configuration:
server:
port: 6000
eureka:Copy the code
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka/ #eureka server provides the registration address to refer to the server configuration of this path
instance:
instance-id: power-1 The unique instance ID of this instance registered with the Eureka server
prefer-ip-address: true Whether to display the IP address
leaseRenewalIntervalInSeconds: 10 # How long does it take for a eureka customer to send a heartbeat to the Eureka server to indicate that it is still alive? Default is 30 seconds (in the same units as configured below)
easeExpirationDurationInSeconds: 30 How long should the Eureka server wait after receiving the last heartbeat of the instance before deleting the instance? Default is 90 seconds
spring:
application:
name: server-power This instance is registered to the eureka server name
Copy the code
Add @enableeurekaclient to the spring-boot project on the client to start the project.
Add @enableeurekaclient to the spring-boot project on the client to start the project.
Here we can see that a service named server-power (capitalized in the figure) with id POWER-1 is registered with our Eureka. At this point, a simple Eureka is set up.
This article will teach you about micro-service architecture: Spring-Cloud
- Feel good friends can forward this article to pay attention to small;
- Thank you for your support!