Have you figured out the relationship between microservices and spring cloud?

Microservice architecture: Spring-Cloud

What is a microservice?

Microservice is to separate all the modules of a bloated project and make them disconnected from each other, even without using the same database. ratio

For example, there are User module and Power module in the project, but User module and Power module are not directly related, only some data needs to be submitted.

Mutual, then it’s okay. These two modules are separated separately. When the user needs to call power, power is a server, but power needs

When the user is called, the user is the server again. Therefore, they don’t care who is the server and who is the caller. They are two independent services.

At that time, the concept of microservices came out.

Classic problem: the difference between microservices and distributed

Speaking of differences, let’s briefly talk about what distributed is. The so-called distributed is to divide a huge system into multiple modules (this is very similar to microservices).

Like) deployed on different machines (because one machine may not be able to withstand such a large pressure or the cost of a very good server may be several

Normally), each module exchanges data through interfaces, in fact, distributed is also a kind of microservice. Because it's all, the modules are split and become independent

The unit provides an interface to call, so what is the essential difference between them? The difference between them is mainly reflected in the "target". What is a target is yours

Things to do in such an architecture project. What is the goal of distributed? We just saw it, it’s just too much for a machine, or it’s the cost.

The problem is that multiple machines have to be used to complete the deployment of the service, and the goal of microservices is to separate the modules without being affected by each other, such as

Module upgrades or BUG etc...

Having said so much, it can be understood in one sentence: Distributed is also a type of microservice, and microservice can be on one machine.

The relationship between microservices and Spring-Cloud (the difference)

Microservices are just a way of project architecture, or a concept, just like our MVC architecture, then Spring-Cloud is for this

The realization of this technology.

Do microservices have to use Spring-Cloud?

We just said that microservices are just a way of project architecture. If you know enough about the concept of microservices, you will know. In fact, microservices are

It can be achieved without the help of any technology, but there are many problems that we need to solve, such as: load balancing, service registration and discovery, service invocation, routing

by. . . . Wait for a series of questions, so Spring-Cloud came out, and Spring-Cloud will package all the technologies to deal with these problems

Well, it's similar to the feeling of opening the bag and eating. .

Construction of Spring-Cloud project

Because spring-cloud is based on the spring-boot project, our project must be a spring-boot project. As for the spring-boot project,

We will not discuss this section first. One point to note here is that the version of spring-cloud and spring-boot should correspond to the following figure:

In my case, my version is spring-boot like this:

 

<parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>2.0.2.RELEASE</version>

  </parent>



spring-cloud:
 

<dependencyManagement>

    <dependencies>

      <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-dependencies</artifactId>

        <version>Finchley.SR2</version>

        <type>pom</type>

        <scope>import</scope>

      </dependency>

    </dependencies>

  </dependencyManagement>

After you have these dependencies in your project, your spring cloud project has been built (it may take a while to download spring-cloud for the first time)

Spring-Cloud components:

eureka:

What is eureka?

Eureka is one of the sub-modules of Netflix, and it is also a core module. There are two components in eureka, one is EurekaServer (an independent item

(Objective) This is used to locate services to achieve load balancing and failover of middle-tier servers, and the other is EurekaClient (our microservice)

It is used to interact with Server, which can make the interaction very simple: only need to pass the service identifier to get the service.

Relationship with spring-cloud:

Spring Cloud encapsulates the Eureka module developed by Netflix to implement service registration and discovery (compare with Zookeeper).

Eureka adopts the design architecture of CS. Eureka Server serves as the server for the service registration function, and it is the service registration center.

Other microservices in the system use Eureka's client to connect to the Eureka Server and maintain a heartbeat connection. So that system maintenance personnel can

Eureka Server is used to monitor whether each microservice in the system is running normally. Some other modules of SpringCloud (such as Zuul) can be

Use Eureka Server to discover other microservices in the system and execute related logic.

Role relationship diagram:

how to use?

Add dependency in spring-cloud project: eureka client:

<dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

    </dependency>

eureka server:

<dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

    </dependency>

Add the following configuration to the eureka server project:

server:

port: 3000

eureka:

server:

 enable-self-preservation: false  #关闭自我保护机制

 eviction-interval-timer-in-ms: 4000 #设置清理间隔(单位:毫秒 默认是60*1000)

instance:

 hostname: localhost

client:

 registerWithEureka: false #不把自己作为一个客户端注册到自己身上
fetchRegistry: false  #不需要从服务端获取注册信息(因为在这里自己就是服务端,而且已经禁用自己注

册了)

 serviceUrl:

  defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

Of course, not all of them are necessary, here is just a copy of my configuration here,
and then add a comment on the spring-boot startup project: @EnableEurekaServer can start the project

@EnableEurekaServer

@SpringBootApplication

public class AppEureka {

  public static void main(String[] args) {

    SpringApplication.run(AppEureka.class);

 }

}

If you see this picture, then you have built it:

This warning just means you turned off his self-protection mechanism

Eureka client configuration:

server:

port: 6000

eureka:

然后在客户端的spring-boot启动项目上 加入注解:@EnableEurekaClient 就可以启动项目了 这里就不截图了我们

直接来看效果图:

这里我们能看见 名字叫server-power的(图中将其大写了) id为 power-1的服务 注册到我们的Eureka上面来了

至此,一个简单的eureka已经搭建好了。

当然 这篇咱们先讲应用, 源码文章以后再更新, 或者大家腾讯课堂搜鲁班学院 我会在里面免费的公开课上讲到

Spring Cloud 以及Eureka的源码

client:

 serviceUrl:

   defaultZone: http://localhost:3000/eureka/  #eureka服务端提供的注册地址 参考服务端配

置的这个路径

instance:

 instance-id: power-1 #此实例注册到eureka服务端的唯一的实例ID

 prefer-ip-address: true #是否显示IP地址

 leaseRenewalIntervalInSeconds: 10 #eureka客户需要多长时间发送心跳给eureka服务器,表明它仍

然活着,默认为30 秒 (与下面配置的单位都是秒)

 leaseExpirationDurationInSeconds: 30 #Eureka服务器在接收到实例的最后一次发出的心跳后,需要

等待多久才可以将此实例删除,默认为90秒

spring:

application:

 name: server-power #此实例注册到eureka服务端的name



Then add a comment on the client's spring-boot startup project: @EnableEurekaClient can start the project, here is not a screenshot, let's look at the rendering directly:

Here we can see that the service whose name is server-power (capitalized in the figure) is registered on our Eureka.

At this point, a simple eureka has been built.

Of course, let’s talk about the application first. The source code article will be updated later. If you need microservice architecture and more information, you can add my Java advanced communication group: 901439810 , welcome your exchanges

Guess you like

Origin blog.csdn.net/Lubanjava/article/details/98501470