Spring Cloud Zuul典型配置实战

一 代码位置
二 测试
1 启动Eurek
2 启动client-a服务
3 启动client-b服务
4 修改启动配置,启动zuul-server
spring:
  profiles:
    active: example1

测试下面路径

zuul:
  routes:
    client-a: /client/**

浏览器输入: http://localhost:5555/client/mul?a=1&b=2

结果输出: client-a-2

它是一个从/client/**到client-a服务的一个映射规则

5 修改启动配置,启动zuul-server
spring:
  profiles:
    active: example2

测试下面配置

zuul:
  routes:
    client-a:

浏览器输入: http://localhost:5555/client-a/mul?a=1&b=3

结果输出: client-a-3

它是一个从/client-a/**到client-a服务的一个映射规则

6 修改启动配置,启动zuul-server
spring:
  profiles:
    active: example4

测试下面配置

zuul:
  routes:
    client-b:
      path: /client/**
      serviceId: client-b
    client-a:
      path: /client/**
      serviceId: client-a

浏览器输入: http://localhost:5555/client/mul?a=3&b=3

结果输出: client-a-9

如果同一个映射对应多个服务,按照加载顺序,最后加载的映射规则会把之前的映射规则覆盖掉。

7 修改启动配置,启动 启动zuul-server
spring:
  profiles:
    active: example5

测试下面配置

########################## 敏感头设置 ##########################
zuul:
  prefix: /pre
  routes:
    client-a:
      path: /client/**
#      sensitiveHeaders: Cookie,Set-Cookie,Authorization
      sensitiveHeaders: Set-Cookie,Authorization
      serviceId: client-a
#      stripPrefix: false

浏览器输入:http://localhost:5555/pre/client/mul?a=3&b=4

测试结果为:client-a-12

8 修改启动配置,启动 zuul-server

spring:
  profiles:
    active: example7

测试下面配置

########################## 使用url替代serviceId路由  ##########################
zuul:
  routes:
    client-a:
      path: /client/**
      url: http://localhost:7070 #client-a的地址

浏览器输入: http://localhost:5555/client/mul?a=3&b=4

输出结果为: client-a-12

修改启动配置,启动 zuul-server

spring:
  profiles:
    active: example8

测试下面配置

########################## 脱离eureka让zuul结合ribbon实现路由负载均衡  ##########################
zuul:
  routes:
    ribbon-route:
      path: /ribbon/**
      serviceId: ribbon-route

ribbon:
  eureka:
    enabled: false  #禁止Ribbon使用Eureka
ribbon-route:
  ribbon:
    NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule     #Ribbon LB Strategy
    listOfServers: localhost:7070,localhost:7071     #client services for Ribbon LB

浏览器多次输入: http://localhost:5555/ribbon/mul?a=3&b=4

结果分别为:
client-b-12
client-b-12
client-a-12
client-b-12
client-a-12
client-b-12
client-a-12

结果随机输出

10 修改启动配置,启动 zuul-server
spring:
  profiles:
    active: example9

测试下面配置

########################## foward跳转本地url  ##########################
zuul:
  routes:
    client-a:
      path: /client/**
      url: forward:/client

浏览器输入: http://localhost:5555/client?a=3&b=4

结果输出: 本地跳转:7

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值