当前位置:网站首页>Nacos / sentinel gateway current limiting and grouping (code)
Nacos / sentinel gateway current limiting and grouping (code)
2022-04-23 07:41:00 【cqwoniu】
Accumulation of knowledge points in project-based learning
1. gateway
1. Gateway is also called inter network connector 、 Protocol converter , Realize network interconnection above the network layer , It is only applicable to the interconnection of two networks with different high-level protocols ;
2. stay OCI In terms of the agreement : Gateways fall into two categories , One is a connection oriented gateway , One is a connectionless gateway ;
3. stay TCP In terms of the agreement , A gateway to another network is essentially an address IP Address ; The hosts of different networks are in different networks , To achieve communication , You have to go through the gateway . It's the only one with a gateway set up IP Address ,TCP/IP In order to realize the mutual communication between different networks .
2.API gateway
API A gateway is a server , It's the only access to the system ,API The gateway encapsulates the internal architecture of the system , It provides a customized interface for the client API.Spring Cloud Gateway It is a simple and effective unified method for microservice architecture API Routing management

3. The functions that the gateway should have
1. performance :API High availability 、 Load balancing , Fault tolerance mechanism
2. Security : Authority authentication 、 desensitization 、 Flow cleaning 、 Back end signature ( Ensure that the whole link is trusted to call ), The blacklist ( Restrictions on illegal calls )
3. journal : logging , When it comes to distributed , Full link tracking is essential
4. cache : Data caching
5. monitor : Record request response data ,api Time consuming analysis , Performance monitoring
6. Current limiting : flow control , Staggered flow control , It can be defined as a variety of current limiting rules
- Gateway dimension : For a micro service as a whole
Coarse grained : Global configuration - API Grouped dimensions : For a range of URL Address , It can not be in a micro service
Fine granularity : Local configuration
Add dependency :
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
Modify the configuration file application.yml:
sentinel:
filter:
enabled: true
datasource:
ds1.file:
file: classpath:router-flow.json
ruleType: gw-flow
ds2.file:
file: classpath:api-flow.json #api-flow.json Grouping of interfaces
ruleType: gw_api_group
Add current limiting rule gw-flow.json
[
{
"resource": " The name of the resource : Gateway time routes.id,api grouping :api Name of the group ",//
"resourceMode": 0 , //0 It's the gateway ,1 yes
"count": 2,
"intervalSec": 60 // timer
},
// Here is an example
{
"resource": "admin-service-api",
"resourceMode":"1",
"count": 1,
"intervalSec": 60
}
]
add to api-group.json
[
{
"apiName": "admin-service-api",
"predicateItems": [
{
"pattern": "/admin/login"
}
]
}
]
add to Controller Get current limiting rules
@RestController
public class FlowRulesController {
@GetMapping("/gw/flow/rules")
public Set<GatewayFlowRule> getGatewayFlowRules(){
return GatewayRuleManager.getRules() ;
}
@GetMapping("/gw/api/groups")
public Set<ApiDefinition> getApiGroupRules(){
return GatewayApiDefinitionManager.getApiDefinitions();
}
}
- The data source of dynamic rules used ( Use nacos To configure )
sentinel:
transport:
dashboard: sentinel-server:8858 # sentinel-dashboard Put it in ecs Inside
datasource:
# ds1.file:
# file: classpath:gw-flow.json # gateway + API Current limiting of packets
# ruleType: gw_flow
ds1.nacos: #com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource Use Nacos Perpetuate my sentinel Data time , Need to add nacos-datasource Dependence
serverAddr: nacos-server:8848
dataId: gw-flow
ruleType: gw_flow
#
# ds2.file:
# file: classpath:api-group.json
# ruleType: gw_api_group
ds2.nacos:
serverAddr: nacos-server:8848
dataId: api-group
ruleType: gw_api_group
Use nacos Persistence rules , Need to add dependencies
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
7. Grayscale : Online grayscale deployment , Can reduce the risk
8. route : Dynamic routing rules
4. nginx And gatewau The difference between :
1.nginx, As the gateway of the whole global , It's external , Out of the outermost ;gateway, More like a business gateway , It is mainly used to provide services for different clients , For aggregating business . Each micro service is deployed independently , Single responsibility , When providing external services, you need to have something to aggregate the business
2.nginx It's written in different languages , Should not be associated with expansion , and gateway It's different , It's like java Written , Easy to extend and maintain
5. Use Spring Cloud Gateway
1. stay gateway-server Add dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2. stay gateway-server Add a startup class
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class ,args) ;
}
}
3. stay gateway-server Add configuration files to it
server:
port: 80
spring:
application:
name: gateway-server
cloud:
nacos:
discovery:
server-addr: nacos-server:8848 # Modify the host file
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true # admin-service ADMIN-SERVICE /admin-service/** -> Microservices (ADMIN-SERVICE) routes: - id: admin-service_router uri: lb://admin-service # Forward to that destination predicates: - Path=/admin/** filters: - StripPrefix=1 # Current end access /admin/login - >login take admin Automatically remove - id: member-service_router uri: lb://member-service # Forward to that destination predicates: - Path=/user/** filters: - StripPrefix=1 # Current end access /user/login - >login take user Automatically remove - id: finance-service_router uri: lb://finance-service # Forward to that destination predicates: - Path=/finance/** filters: - StripPrefix=1 # Current end access /finance/xx, Get rid of finance - id: exchange-service_router # Routing of trading system uri: lb://exchange-service # Forward to that destination predicates: - Path=/exchange/** filters: - StripPrefix=1 # Current end access /exchange/xx,exchange - id: test_router uri: http://www.aliyun.com predicates: - Path=/product # /test->http://www.baidu.com The gateway itself will build a request to access the address we want to access GET:http://www.baidu.com/test # Through the gateway to complete 2 Current limiting of three dimensions : Gateway dimension + API grouping sentinel: transport: dashboard: sentinel-server:8858 # sentinel-dashboard Put it in ecs Inside datasource: # ds1.file: # file: classpath:gw-flow.json # gateway + API Current limiting of packets # ruleType: gw_flow ds1.nacos: #com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource Use Nacos Perpetuate my sentinel Data time , Need to add nacos-datasource Dependence serverAddr: nacos-server:8848 dataId: gw-flow ruleType: gw_flow # # ds2.file: # file: classpath:api-group.json # ruleType: gw_api_group ds2.nacos: serverAddr: nacos-server:8848 dataId: api-group ruleType: gw_api_group # We demonstrated sentinel-dashboard The definition of rules , And when the rules are defined , Our gateway can immediately sense ( take effect )(nacos Can't perceive ), But next time we restart , Will lose the rules .--》nacos redis: host: redis-server port: 6380 password: Ltd3411??
版权声明
本文为[cqwoniu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230622032415.html
边栏推荐
猜你喜欢

FSM有限状态机

如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列

可视化之路(九)Arrow类详解

Take you to travel in space, and American photography technology provides comprehensive technical support for aerospace creative applet

keytool: command not found

超级宝典&编程指南(红蓝宝书)-读书笔记

Source Insight 4.0常见问题

菜菜的并发编程笔记 |(五)线程安全问题以及Lock解决方案

直观理解熵

Reflection on the systematic design of Android audio and video caching mechanism
随机推荐
Reflection on the systematic design of Android audio and video caching mechanism
数据库查询优化的方式
On BFC (block formatting context)
数据分析入门 | kaggle泰坦尼克任务(三)—>探索数据分析
anaconda3安装
Discussion on frame construction and technology selection of short video platform
OpenGL超级宝典初步配置(freeglut、glew、gltools、glut)
VScode
什么是闭包?
10.更新操作
‘npm‘不是内部或外部命令,也不是可运行的程序 或批处理文件
每日一题 | 曾被反转链表支配的恐惧
后台管理系统框架,总有你想要的
Machine vision series (02) -- tensorflow2 3 + win10 + GPU installation
P1446 [HNOI2008]Cards(Burnside定理+dp计数)
[Ted series] how to get along with inner critics?
经典套路:一类字符串计数的DP问题
状态同步与帧同步
[牛客练习赛68]牛牛的粉丝(矩阵快速幂之循环矩阵优化)
Date对象(js内置对象)