当前位置:网站首页>Introduction to microservices, euraka, ribbon, openfeign
Introduction to microservices, euraka, ribbon, openfeign
2022-04-22 00:25:00 【Old frog@】
Introduction to microservice
First understand a concept —— High availability , namely Ensure the high availability of the server , Reduce downtime ; The original single application has only one server , All services ( function ) All on this server , If something happens , The server is down , The whole system shut down . Microservices ensure that multiple services exist on multiple servers , Or multiple servers jointly maintain a service ( colony ), The failure of a service will not affect the whole , The whole system will not be shut down
Implementation of microservices
Each microservice corresponds to a server , A database ; But it is very troublesome and disorganized for different services to call each other directly , So register all modules in a common place , Every request finds a registry , Then it is sent to each server by the registry

Service registration
Implementation of registry (Netfilx)
- Create a new one springboot Project modules , Add dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- Annotate the entry class
@EnableEurekaServer, Represents the server side - The configuration file
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false # Whether to register in the Registration Center
fetch-registry: false # Whether to call other services
service-url:
defaultZone: http://${
eureka.instance.hostname}:${
server.port}/eureka/
After success, you can see the following page :

Implement service registration
- Create a service , And introduce dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- Add comments to the entry class
@EnableEurekaClient, Represents a client , Will find the server side and register - To configure
server:
port: 9001
spring:
application:
name: service-user # The name of the service in the registry
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
After successful registration, you can see the service in the Registration Center :

Shared services
For example, the response entity class, so that the same class in each service can be placed in a common service , Instead of writing it once in every service , After the shared service is created, it will be used in other services pom The dependency can be introduced into the file
Implement direct calls between services
User
@GetMapping("get/{id}")
public ResponseVO getById(@PathVariable Integer id){
return ResponseVO.FORBIDDEN;
}
Order
@Autowired
private RestTemplate restTemplate;
@GetMapping("getUser/{id}")
public ResponseVO getById(@PathVariable Integer id){
ResponseEntity<ResponseVO> entity = restTemplate.getForEntity("http://localhost:9001/user/get/1",ResponseVO.class);
ResponseVO responseVO = entity.getBody();
return responseVO;
}
adopt RestTemplate You can call when a service needs data from another service , But this direct call bypasses the registry , This way will ip It's dead , You have to know each other's ip Or domain name can be called , If the deployed address changes, it needs to be changed at the same time , It is not recommended to use
Ribbon
Use Ribbon Implement service calls
spring-cloud-starter-netflix-eureka-client 3.0 Version already built in ribbon, therefore ribbon Also lost version maintenance , Forced addition will result in jar Packet collision , To report a mistake ——No instances available for XXX; If you use the old version, you need to add spring-cloud-netflix-ribbon rely on
- stay RestTemplate Of Bean Add comment on
@LoadBalanced - In the use of restTemplate when ,url Parameters do not use the actual domain name and port , Instead, use the name registered in the registry
ResponseEntity<ResponseVO> entity = restTemplate.getForEntity("http://service-user/user/get/1",ResponseVO.class);
Load balancing
It refers to assigning tasks to multiple operation units for execution
Server side load balancing

utilize nginx Set up servers for clusters , Maintain a service through a cluster , At this time by nginx Load balance requests on the server side
Client load balancing

When a client needs to call other clients , The callee will run under multiple servers , Divide the task into different operation units
Ribbon It is a tool for client load balancing , Use ribbon The request can be assigned to different operation units through the registry
By setting the running environment, you can configure services to multiple ports :

-D Indicates passing parameters to the virtual machine , Followed by server.port You can run the service to 9003 port

OpenFeign( Declarative call )
ribbon Calling is not the best way , Recommended OpenFeign Make a declarative call
- Introduce dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- Use annotations at the caller's entry
@EnableFeignClients, Indicates that this client enables declarative calls - Create a new interface for calling a service , Using annotations @FeignClient(),value Is the name of the called service , The method inside uses springMVC Corresponding to the method path of the callee
@FeignClient("service-user")//value The parameter is the name of the callee
public interface UserService {
@GetMapping("user/get/{id}")// The path of the method being called
ResponseVO getById(@PathVariable Integer id);
}
- Direct injection when using this interface
@Autowired
private UserService userService;
@GetMapping("getUser2/{id}")
public ResponseVO get2ById(@PathVariable Integer id){
ResponseVO responseVO = userService.getById(id);
return responseVO;
}
版权声明
本文为[Old frog@]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220015316847.html
边栏推荐
- 多线程实现的交替打印的多种实现形式
- Functional coverage cov of coverage series learning
- C语言 结构体
- It is said that MES can improve the production efficiency of enterprises. What are the specific aspects?
- Why do requirements change during MES implementation? How to solve it?
- 分布式事务与Seata
- Tree and binary tree
- Busybox overview
- Codesys méthode de lecture des fichiers csv (non Excel)
- When the notebook expands the external display, the mouse cannot move to another screen outside the main display
猜你喜欢

零基础转行网工,我是如何拿到月薪20K的,网工系统学习路线分享

How to choose the current limiting resistor of Zener diode

Detailed explanation of thread pool

Introduction to container technology related concepts of container cloud series

华为Routing & Switching切换为Datacom,究竟变了些什么

内存管理、

Very powerful time and date plug-in --- jedate js

Thousands of people have collected and "finished" this 300 page 1000 interview questions together
![[reading notes] empirical accounting and financial research methods - principle, application and SAS implementation, Lu Guihua](/img/a0/ceb8212bae4d76860f5a70824cd2c8.gif)
[reading notes] empirical accounting and financial research methods - principle, application and SAS implementation, Lu Guihua

OpenSSL简单的使用
随机推荐
Structure of actual combat training module III
Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!
Why should relays be connected in parallel with diodes
C language structure
L1-020 帅到没朋友
OJ daily practice - factorial sequence and
刷题整理(一)
uniapp+php开发的影票返利系统,可完美运营
RT thread application - using RT thread on stm32l051 (I. new project of wireless temperature and humidity sensor)
How did I get a monthly salary of 20K when I changed from zero basis to network worker? Share the learning route of network worker system
华为Routing & Switching切换为Datacom,究竟变了些什么
Understand the production scheduling management function of MES system
2022年江西省安全员A证考试题及答案
非常强大的时间日期插件 --- JeDate.js
Oil monkey script synchronization
09. Raspberry pie ASP Net environment configuration
SEO keyword expansion - Automatic Keyword expansion software free download
二分查找法
Detailed explanation of TCP's three handshakes
制作最简单的根文件系统