当前位置:网站首页>Eureka notes
Eureka notes
2022-07-23 16:57:00 【Lemon smash】
One , Service splitting and remote invocation
Any distributed architecture is inseparable from the splitting of services , The same goes for microservices .
1. Service splitting principle
Here I have summarized several principles for splitting microservices :
Different microservices , Don't develop the same business repeatedly
Microservice data independence , Do not access databases of other microservices
Microservices can expose their business as interfaces , For other microservices to call
2. Service splitting principle

cloud-demo: The parent project , Management dependence
order-service: Order micro service , Responsible for order related business
user-service: User microservices , Responsible for user related business
requirement :
Both order microservices and user microservices must have their own databases , Are independent of each other
Both order service and user service are exposed Restful The interface of
If the order service needs to query user information , Only user services can be called Restful Interface , Cannot query user database
3. Implementation of remote call cases
stay order-service In service , There is a basis id Interface for querying orders :

according to id Query order , The return value is Order object , Pictured :
Among them user by null
stay user-service There is a basis in id Query user interface :

The result of the query is shown in the figure :
3.1 Case needs :
modify order-service According to id Query order business , It is required to query the order at the same time , According to... Included in the order userId Find out the user information , Back together .

therefore , We need to be in order-service in towards user-service To launch a http Request , call http://localhost:8081/user/{userId} This interface .
The general procedure is like this :
Sign up for a RestTemplate Instance to Spring Containers
modify order-service In service OrderService Class queryOrderById Method , according to Order Object userId Inquire about User
Will search for User Fill in Order object , Back together
3.2. register RestTemplate
First , We are order-service In service OrderApplication Start the class , register RestTemplate example :
package cn.itcast.order;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}3.3. Implement remote call
modify order-service In service cn.itcast.order.service Under bag OrderService Class queryOrderById Method :

4. Providers and consumers
In the service invocation relationship , There will be two different roles :
Service providers : In a business , Services invoked by other microservices .( Provide interfaces to other microservices )
Serving consumers : In a business , Call the services of other microservices .( Call interfaces provided by other microservices )

however , The roles of service providers and service consumers are not absolute , But relative to the business .
If the service A Service called B, And the service B The service is called again C, service B What's your role ?
about A call B For your business :A It's about serving consumers ,B It's the service provider
about B call C For your business :B It's about serving consumers ,C It's the service provider
therefore , service B It can be either a service provider , It can also serve consumers .
5. The emergence of problems :
If our service provider user-service Multiple instances deployed , Pictured :

Let's think about a few questions :
order-service When initiating a remote call , How to know user-service Example of ip Address and port ?
There are many. user-service Address of the instance ,order-service How to choose when calling ?
order-service How to learn about a user-service Whether the instance is still healthy , Is it down ?
Two ,Eureka The structure and function of
These problems need to be used SpringCloud To solve the problem of , One of the most well-known registries is Eureka, Its structure is as follows :

Answer the previous questions .
problem 1:order-service How to know user-service Address of the instance ?
The process of obtaining address information is as follows :
user-service After the service instance starts , Register your information with eureka-server(Eureka Server side ). This is called service registration
eureka-server Save the mapping relationship between service name and service instance address list
order-service According to service name , Pull the instance address list . This is called service discovery or service pull
problem 2:order-service How to start from multiple user-service Select a specific instance from the instance ?
order-service Select an instance address from the instance list using the load balancing algorithm
Make a remote call to the instance address
problem 3:order-service How to learn about a user-service Whether the instance is still healthy , Is it down ?
user-service Every once in a while ( Default 30 second ) towards eureka-server Initiate request , Report your status , It's called heartbeat
When no heartbeat is sent over a certain period of time ,eureka-server It will be considered that the microservice instance has failed , Remove the instance from the service list
order-service When pulling Services , You can eliminate the fault instance
Be careful : A microservice , It can be either a service provider , It can also serve consumers , therefore eureka Register the service 、 Service discovery and other functions are uniformly encapsulated in eureka-client End
therefore , The next steps we implement include :

1. build eureka-server
First of all, let's register the service side of the center :eureka-server, This must be an independent microservice
1.1. establish eureka-server service
stay cloud-demo Under the parent project , Create a sub module :

1.2. introduce eureka rely on
introduce SpringCloud by eureka Provided starter rely on :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.3. Writing configuration files
Write a application.yml file , The contents are as follows :
server:
port: 10086
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
1.4. Start the service
Start microservices , Then visit the :http://127.0.0.1:10086
See the following results should be successful :

2. Service registration
below , We will user-service Sign up to eureka-server In the middle .
1) Introduce dependencies
stay user-service Of pom In file , Introduce the following eureka-client rely on :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2) The configuration file
stay user-service in , modify application.yml file , Add service name 、eureka Address :
spring:
application:
name: userservice
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
3) Start multiple user-service example ( It's just a demonstration )
To demonstrate a scenario where a service has multiple instances , Let's add a SpringBoot Start configuration of , Start one more user-service.
First , Copy the original user-service A launch configuration :
then , In the pop-up window , Fill in information :
Now? ,SpringBoot Two windows appear user-service A launch configuration :
however , The first is 8081 port , The second is 8082 port .
Start two user-service example :
see eureka-server Manage Pages :

3. Service discovery
below , We will order-service Logic modification of : towards eureka-server Pull user-service Information about , Implementing service discovery .
1) Introduce dependencies
I said before , Service discovery 、 Service registration is uniformly encapsulated in eureka-client rely on , Therefore, this step is consistent with the service registration .
stay order-service Of pom In file , Introduce the following eureka-client rely on :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2) The configuration file
Service discovery also needs to know eureka Address , Therefore, the second step is consistent with the service registration , It's all configuration eureka Information :
stay order-service in , modify application.yml file , Add service name 、eureka Address :
spring:
application:
name: orderservice
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
3) Service pull and load balancing
Last , We're going eureka-server Middle pull user-service List of instances of the service , And load balancing .
But we don't have to do these moves , Just add some comments .
stay order-service Of OrderApplication in , to RestTemplate This Bean Add one @LoadBalanced annotation :
modify order-service In service cn.itcast.order.service Under bag OrderService Class queryOrderById Method . Modify access url route , Use the service name instead of ip、 port :

spring Will automatically help us from eureka-server End , according to userservice This service name , Get instance list , Then load balancing is completed .
边栏推荐
- Cuibaoqiu, vice president of Xiaomi group: open source is the best platform and model for human technological progress
- NodeJs实现token登录注册(KOA2)
- Less than 10 days before the PMP Exam on July 30, what should be done?
- RISC-V基金会董事谭章熹:RISC-V,从边缘逐渐向中央扩展
- 【Error】TypeError: expected str, bytes or os.PathLike object, not int
- anchor free yolov1
- Priyanka Sharma, general manager of CNCF Foundation: read CNCF operation mechanism
- 小米集团副总裁崔宝秋:开源是人类技术进步的最佳平台和模式
- 系统内存介绍和内存管理
- mysql如何查询不在数据库里的数据?
猜你喜欢

Deep learning convolutional neural network paper study alexnet

NodeJs实现token登录注册(KOA2)

Royal O'Brien, executive director of o3df: open source has no boundaries, and all shared sounds will become the actual direction

Bag of Tricks for Image Classification with Convolutional Neural Networks(卷积神经网络在图像分类中的技巧)

Tips and tricks for Neural Networks 深度学习训练神经网络的技巧总结(不定期更新)

General paging function

Bag of tricks for image classification "with convolutional neural networks"

卷积神经网络模型之——GoogLeNet网络结构与代码实现

Direct exchange

Case analysis of building campus information management system with low code
随机推荐
Circuit structure and output mode of GPIO port of 32-bit single chip microcomputer
O3DF执行董事Royal O’Brien:开源没有边界,所有共享的声音都会变成实际方向
【TensorFlow】检测TensorFlow GPU是否可用
uni-app进阶之认证【day12】
tensorflow2.X实战系列softmax函数
How to buy financial products with a return of more than 6%?
距离IoU损失:包围盒回归更快更好的学习(Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression)
IE盒模型和标准盒模型
【30. n-皇后问题】
VMware虚拟机的三种网络模式
CNCF基金会总经理Priyanka Sharma:一文读懂CNCF运作机制
Eureka笔记
Ie box model and standard box model
灰色预测(MATLAB)
48:第五章:开发admin管理服务:1:创建子工程【imooc-news-dev-service-admin】,管理服务模块;
Surface family purchase reference
pinia(菠萝)
securecrt 乱码
SQL156 各个视频的平均完播率
YOLOv4: Optimal Speed and Accuracy of Object Detection