qiuxuhui

导航

Zuul 网关转发的五种方式

 

 

上图为一个微服务框架的简单示例,当有一个HTTP请求发送到服务器的时候,其实是先经过了Nginx的,再经过了网关,这里的网关就担任了拦截过滤的作用,既然拦截和过滤了,肯定就涉及到了请求的转发

转发我大致例了以下五种转发方式:

方式一:path+serviceId 方式

在spring_clude的配置文件application.yml 文件中加入以下配置:

 

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

  跳转示例 :http://localhost:8888/order/order2/201810300001

http://localhost:8888/order就相当于指定了eureka中id为app-order的微服务,后面在接上他本身的参数,就可以正常使用了

 

方式二:指定服务id 方式
zuul:
	routes: #定义服务转发规则
	        app-order: /order/**

  跳转示例 :http://localhost:8888/order/order2/201810300001

和方法一一样,http://localhost:8888/order就相当于指定了eureka中id为app-order的微服务,后面在接上他本身的参数,就可以正常使用了

 

方式三:同时配置path和url 方式
zuul:
	routes: #定义服务转发规则
	   path: /order/**
	   url: http://127.0.0.1:8091 #真正的微服务地址,path匹配的请求都转发到这里

  跳转示例 :http://localhost:8888/order/order2/201810300001

上面的和方法一样,http://localhost:8888/order就相当于指定了eureka中id为app-order的微服务,后面在接上他本身的参数,就可以正常使用了

 

方式四:路由前缀 方式
zuul:
	prefix: /order2
	    strip-prefix: false
	    routes:
	       app-order: /order/**

  跳转示例 :http://localhost:8888/order2/app-order/201810300001

访问Zuul的/order2/app-order/201810300001路径,请求将会被转发到app-order 的order2/201810300001

 

方式五:路由前缀2 方式
zuul:
	routes:
	    app-order:
	       path: /order2/**
	       strip-prefix: false

  跳转示例 :http://localhost:8888/order2/201810300001

访问Zuul的/order2/201810300001路径,请求将会被转发到app-order 的order2/201810300001

 

posted on 2021-01-22 10:38  qiuxuhui  阅读(1058)  评论(0编辑  收藏  举报