spring cloud中代理服务器zuul的使用
主流网关:
zuul
kong 基于nginx的API Gateway
nginx+lua
1、新建项目,选择eureka discovery 和zuul
2、启动类中增加 @EnableZuulProxy
3、修改配置文件后缀名为yml,并在配置中增加端口号、应用名称和注册中心地址,如下:
server:
port: 9000
spring:
application:
name: api-gateway
eureka:
client:
service-url:
defaultZone: http://localhost:7880/eureka #注册中心地址
4、访问
http://192.168.136.128:8651/api/v1/orderfeignhystrix/save?userId=2&productId=2
修改为
http://192.168.136.128:9000/orderfeignhystrix-service/api/v1/orderfeignhystrix/save?userId=2&productId=2
http://192.168.136.128:8765/api/v1/product/list
修改为
http://192.168.136.128:9000/product-service/api/v1/product/list
5、自定义路由转发
zuul:
routes:
product-service: /apigateway/**
访问
http://192.168.136.128:9000/apigateway/api/v1/product/list
6、环境隔离:
需求 :不想让默认的服务对外暴露接口
product-service/api/v1/product/list
配置:
zuul:
ignored-patterns:
- /*-service/api/v1/product/list
可以访问
http://192.168.136.128:9000/apigateway/api/v1/product/list
不可以访问
http://192.168.136.128:9000/product-service/api/v1/product/list
原生地址仍旧可以访问
http://192.168.136.128:8765/api/v1/product/list