zuul聚合微服务 (一)
- 实例代码
- 运行项目(需启动两个用户微服务和一个订单微服务,eureka-server,zuul的项目
1.08-ms-consumer-order-ribbon
2.08-ms-eureka-server
3.08-ms-gateway-zuul-aggregation
4.08-ms-provider-user
- 聚合请求
zuul的路由配置 (二)
- ① 源码
- ② 路由忽略微服务
zuul:
ignored-services: microservice-provider-user
- ③ 其他路由配置可查看项目示例的配置文件
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
microservice-provider-user: /user/**
management:
security:
enabled: false
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
ignored-services: microservice-provider-user
management:
security:
enabled: false
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
ignored-services: '*' # 使用'*'可忽略所有微服务
routes:
microservice-provider-user: /user/**
management:
security:
enabled: false
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
user-route: # 该配置方式中,user-route只是给路由一个名称,可以任意起名。
service-id: microservice-provider-user
path: /user/** # service-id对应的路径
management:
security:
enabled: false
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
user-route: # 该配置方式中,user-route只是给路由一个名称,可以任意起名。
url: http://localhost:8000/ # 指定的url
path: /user/** # url对应的路径。 这样就可以将/user/**映射到http://localhost:8000/**,这种方式访问不会作为HystrixCommand执行,也不能使用ribbon来负载多个URL,例6可以解决该问题
management:
security:
enabled: false
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
user-route:
path: /user/**
service-id: microservice-provider-user
ribbon:
eureka:
enabled: false # 禁用掉ribbon的eureka使用
microservice-provider-user:
ribbon:
listOfServers: localhost:8000,localhost:8001
management:
security:
enabled: false
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
prefix: /api
strip-prefix: false
routes:
microservice-provider-user: /user/**
logging:
level:
com.netflix: DEBUG
management:
security:
enabled: false
# 访问Zuul的/api/microservice-provider-user/1路径,请求将会被转发到microservice-provider-user的/api/1,可查看日志打印,有助于理解。
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
microservice-provider-user:
path: /user/**
strip-prefix: false
logging:
level:
com.netflix: DEBUG
management:
security:
enabled: false
# 这样访问Zuul的/user/1路径,请求将会被转发到microservice-provider-user的/user/1,可查看日志打印,有助于理解。
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
ignoredPatterns: /**/admin/** # 忽略所有包括/admin/的路径
routes:
microservice-provider-user: /user/**
management:
security:
enabled: false
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
route-name:
path: /path-a/**
url: forward:/path-b
management:
security:
enabled: false
server:
port: 8040
spring:
application:
name: microservice-gateway-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
route-name:
path: /path-a/**
url: forward:/path-b
management:
security:
enabled: false
PS:这次说了zuul的路由和在zuul网关做聚合项目。下次继续说zuul的微网关设置。