The above picture is a simple example of a microservice framework. When an HTTP request is sent to the server, it actually passes through Nginx first, and then passes through the gateway. The gateway here acts as an interception and filtering function. Since it intercepts and filters , It must involve the forwarding of the request
Forwarding I roughly illustrate the following five forwarding methods:
Method 1: path+serviceId method
Add the following configuration to the spring_cloud configuration file application.yml file:
server:
port: 8888 #服务端口
spring:
application:
name: app-zuul-gateway #指定服务名
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8100/eureka/ #注册到eureka中的地址
register-with-eureka: true
fetch-registry: true
instance:
prefer-ip-address: true #将自己的ip地址注册到Eureka服务中
ip-address: 127.0.0.1 #ip地址
zuul:
# 方式一:path+serviceId
routes: #定义服务转发规则
abcs: #abcs这个名字任意取的
path: /order/** #配置请求URL的请求规则
serviceid: app-order #eureka中服务的id
Jump example : http://localhost:8888/order/order2/201810300001
http://localhost:8888/order is equivalent to specifying the microservice whose id is app-order in eureka. After connecting with its own parameters, it can be used normally
Method 2: Specify the service id method
zuul:
routes: #定义服务转发规则
app-order: /order/**
Jump example : http://localhost:8888/order/order2/201810300001
Same as method one, http://localhost:8888/order is equivalent to specifying the microservice whose id is app-order in eureka. After connecting with its own parameters, it can be used normally.
Method 3: Configure path and url at the same time
zuul:
routes: #定义服务转发规则
abcs:
path: /order/**
url: http://127.0.0.1:8091 #真正的微服务地址,path匹配的请求都转发到这里
Jump example : http://localhost:8888/order/order2/201810300001
The above is the same as the method, http://localhost:8888/order is equivalent to specifying the microservice whose id is app-order in eureka. After connecting with its own parameters, it can be used normally.
Method 4: Routing prefix method
zuul:
prefix: /order2
strip-prefix: false
routes:
app-order: /order/**
Jump example : http://localhost:8888/order2/app-order/201810300001
Visit Zuul's /order2/app-order/201810300001 path, the request will be forwarded to app-order's order2/201810300001
Method 5: Route prefix 2 method
zuul:
routes:
app-order:
path: /order2/**
strip-prefix: false
Jump example : http://localhost:8888/order2/201810300001
Visit Zuul's /order2/201810300001 path, the request will be forwarded to order2/201810300001 of app-order
This is the end of today’s sharing. We welcome all students’ comments or suggestions to the editor.