当前位置:网站首页>Creation of gateway routing service
Creation of gateway routing service
2022-07-22 02:15:00 【Hu Shang】
List of articles
Quick start
Import dependence ,gateway yes SpringCloud Officially developed , So we need to ensure that the father maven Added to the project SpringCloud Version manager for
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
To configure yml file
server: port: 8080 spring: application: name: api-gateway-demo cloud: # Start gateway Configuration of gateway: # Configure routing rules , This is an array routes: - id: order_route # Unique identification of the route , At present, which service does this gateway route to , This id Take the corresponding name , But there is no mandatory requirement to customize uri: http://127.0.0.1:8083 # Address to forward , Here is the order service url # Configure assertion rules , Used for routing rule matching Never forward any requests to the above uri in predicates: - Path=/order_server/** # PredicateDefinition Object's name Properties are all String, So here we specify that any attribute is a value ,Path # So we need to use =, It will internally assign values for us through reflection # We usually specify such a prefix to distinguish routes , That is to say, my browser requests http://localhost:8080/order_server/order/add # It will forward as http://127.0.0.1:8083/order_server/order/add # But our order service is not order_server This prefix is , Just directly http://127.0.0.1:8083/order/add # So we need to use a filter to remove order_server The prefix # Configure filters filters: - StripPrefix=1 # StripPrefix Remove the first layer path before forwarding #- id: stock_route You can write multiple routes , The format refers to the above
Start the project for testing , Here, when we enter
http://localhost:8080/order_server/order/add
It will perform route resolution and becomehttp:/127.0.0.1:8083/order/add
Integrate Nacos
In the quick start section above , The address of the forwarding path is written in the configuration file , There will be many problems that are difficult to maintain , For example, the server is migrated and replaced ip、 The service cluster also needs the middle tier to balance the load … So we need to get the service address through the Registration Center
add to nacos rely on
<dependencies> <!-- gateway rely on --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- nacos The registry relies on --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies>
To write yml To configure
server: port: 8080 spring: application: name: api-gateway cloud: # Start gateway Configuration of gateway: routes: - id: order_route # lb Means from nacos Get microservices by name in , And follow the load balancing strategy ,lb,LoadBalancer, and order-service Is the service name uri: lb://order-service predicates: - Path=/order_server/** filters: - StripPrefix=1 # nacos Configuration of nacos: discovery: server-addr: 127.0.0.1:8848 username: nacos password: nacos
Restart the service for testing
nacos List of services for
There is also a way to abbreviate : Remove the routing configuration , add spring.cloud.gateway.discovery.locator.enabled=true
In the request url Use in http://loclhost:8080/ service name /order/add
, It will automatically find the remote address of the service in the registry according to the service name , And it will be automatically removed uri At the level of service name
server:
port: 8080
spring:
application:
name: api-gateway
cloud:
# Start gateway Configuration of
gateway:
discovery:
locator:
enabled: true
# nacos Configuration of
nacos:
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
test
Of course , In daily development , This method is not very useful , More often, we use the method of configuring routes , The code is more readable , And more flexible
边栏推荐
- JS object: several ways to implement inheritance and their advantages and disadvantages
- [自然语言处理] 预训练词向量百度云下载 [Word2Vec, Glove, FastText]
- (闲暇)LeetCode_7. 整数反转
- 6.< tag-动态规划和打家劫舍合集(树形DP)>lt.198.打家劫舍 + lt.213. 打家劫舍 II + lt.337. 打家劫舍 III dbc
- 电脑是怎样上网的 (三) 报文头封装和接入网与网络运营商
- PageHelper paging plug-in learning
- (闲暇)LeetCode13. Roman to Integer
- Codeforces Round #808 (Div. 2) - A,B,C
- 【开发教程6】AI语音人脸识别(会议记录仪/人脸打卡机)-串口
- 08.归并排序和快速排序
猜你喜欢
随机推荐
【御芯微WIoTa自组网协议开发套件试用体验】rt-thread bsp软件包制作
马斯克把大脑上传云端?周鸿祎:还得要用人脑安全卫士杀个毒
excel if 判读单元格如果为空就不参与计算
HCIP第八天笔记
DHCP protocol
[learning notes] on Gaussian integer
电脑是怎样上网的 (一) 消息产生 DNS
次小生成树
CTF解题思路
Rigid body of unity physical component
Learn STM32, starting with the framework design of Hal library
工作笔记——使用Jest时遇到的一些问题
Anti sandbox method
313131313123
DHCP协议
产品代码更新代码
The fathers of programming languages are too bored to retire and choose to return to the workplace
Matlab simulation analysis of spectrum switching mechanism of cognitive radio based on relay assistance
08.归并排序和快速排序
JS object: check whether the attribute exists