zuul的路由手动配置和默认配置(挺详细的),以及踩到的坑日常记录

服务yml配置:

server:
  port: 8081

spring:
  application:
    name: item-service

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:7000/eureka
  #instance:
   # ip-address: ${spring.application.name}:${server.port}
    #prefer-ip-address: true #真实ip
    #hostname: 127.0.0.1


第一种配置方式:
备注:手动配置,如果开启多个同一个服务,无法解决负载均衡
写死了发送请求后指定的服务端口
 

zuul:
    routes:#key value键值对
        keys:  #可以随意写key
            path: /user-service/**   #匹配路径  例如发送请求路径/user-service/user/list  匹配路径+请求路径
            url: http://127.0.0.1:8081  #只要路径匹配,就转到这个服务

第二种配置方式:(zuul内置了ribbon)
备注:手动配置,如果开启多个同一个服务,通过负载均衡自动选择服务或者按照均衡策略
(遵循负载均衡,发送请求后,找这个服务下的服务对应策略端口)

zuul:
  routes:
    keys:
      path: /query/**
      serviceId: item-service

遇到的坑:如果服务的yml的instance配置没有注销,ribbon报错,说某某service找不到,注销服务的yml的instance配置就好了
 

response:This application has no explicit mapping for /error, so you are seeing this as a fallback.

原来配置

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:7000/eureka
  instance:
    ip-address: ${spring.application.name}:${server.port}
    prefer-ip-address: true #真实ip
    hostname: 127.0.0.1

正确配置

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:7000/eureka
  instance:
   # ip-address: ${spring.application.name}:${server.port}
    prefer-ip-address: true #真实ip
    hostname: 127.0.0.1
    instance-id: ${spring.application.name}:${server.port} #服务对应的服务id

原因:

注册服务保存的格式是:map<key,value[{map1<key,vaule>,map2<key,vaule>,map3<key,vaule>}]>

ip-address和instance-id不是一回事!!!

instance-id是服务对应的服务id。

ip-address不知道,之前误以为配置服务对应的服务id是这个。。。

第三种配置方式:(zuul内置了ribbon)
备注:手动配置,如果开启多个同一个服务,通过负载均衡自动选择服务或者按照均衡策略
(遵循负载均衡,发送请求后,找这个服务下的服务对应策略端口)

简化了第二种,因为key的名字并不重要,路由映射的重要信息为两点:1、服务名  2、映射路径
所以引出了把key写成服务名,后面追加映射路径

zuul:
  routes:
    item-service: /query/**  #服务名: 映射路径

引出zuul的路由默认自动配置
问题来了,
如果有很多服务,一个个配,很麻烦
于是zuul在拉取服务的时候,其实已经帮我们自动配置了。
多个服务,zuul路由默认配置如下:
 

zuul:
  routes:
    item-service: /item-service/**  #服务名: /服务名/**   #用服务名作为映射路径
    aa-service: /aa-service/**
    bb-service: /bb-service/**
    cc-service: /cc-service/**
    dd-service: /dd-service/**

之前第二种第三种都是自定义配置映射路径,叫手动配置

zuul:
  routes:
	item-service: /query/**  #服务名: /手动配置的映射路径/**
	aa-service: /aa/**
	bb-service: /bb/**
	cc-service: /cc/**
	dd-service: /dd/**

手动配置zuul某个服务的路由规则后,默认的路由规则也在

即配置一个规则相当于两个规则:

zuul:
  routes:
	item-service: /query/**  #服务名: /手动配置的映射路径/**


等同于

zuul:
  routes:
	item-service: /query/**  #服务名: /手动配置的映射路径/**
        item-service: /item-service/**  #服务名: /默认配置的映射路径/**

   
手动配置后,请求的映射路径可以是自定义的,也可以是服务名

例子1:
http://localhost:7001/query/item/list/dasffffffff
http://localhost:7001/item-service/item/list/dasffffffff

例子2:
http://localhost:7001/aa/item/list/fgsgfd
http://localhost:7001/aa-service/item/list/fsgfdg


禁用某个服务的zuul默认路由规则:
说明: 不想让某个服务在zuul拉取服务的时候自动生成路由规则
 

zuul:
  routes:
    item-service: /query/**
  ignored-services:  #服务名集合配置
    - item-service
    - aa-service
    - bb-service
    - cc-service
    - dd-service

zuul路由拉取服务后,默认规则的服务名暴露:
路由配置完成,zuul代理了服务,访问需要加映射的路径,所有的zuul路由默认规则的服务名都会暴露在请求访问的url路径上
如:zuul路由默认配置的服务名暴露
 

URL1:      http://localhost:7001/query/item/list/gfsdf  #没有暴露
URL2:      http://localhost:7001/aa-service/item/list/gfsdf #暴露
URL2:      http://localhost:7001/bb-service/item/list/gfsdf #暴露
URL2:      http://localhost:7001/cc-service/item/list/gfsdf #暴露
URL2:      http://localhost:7001/dd-service/item/list/gfsdf #暴露


解决:去掉映射路径服务的yml配置:
 

server:
  port: 8081

spring:
  application:
    name: item-service


手动配置路由规则去前缀:
原来访问某个服务两种方式(没去掉前缀的访问配置):

controller层:


/**
 * @Date 2019/11/9 04:09
 * by mocar
 */
@RestController
@RequestMapping("/vslm/item")
public class CategoryController {
    @GetMapping("/list/{pid}")
    public String queryByPid(@PathVariable(name = "pid") String pid){
        return pid.toString();
    }
}


路由规则方式1:
请求路径格式: http://localhost:7001/vslm/vslm/item/list/gfsdf
 

zuul:
  routes:
    key1:
      path: /vslm/**
      serviceId: item-service
      

路由规则方式2(简化方式即上面说的路由配置第三种 服务名: /映射路径/**):
请求路径格式: http://localhost:7001/vslm/vslm/item/list/gfsdf

zuul:
  routes:
    item-service: /vslm/** #服务名: /映射路径/**


去前缀的路由yml配置(单个服务去前缀):
路由规则方式1的去前缀:

zuul:
  routes:
    key1:
      path: /vslm/**
      serviceId: item-service
      strip-prefix: false #单配某个服务不用带前缀访问  也可以映射  strip英文翻译:带
  #ignored-services: #服务名集合配置
   # - item-service


访问路径变成了:
请求路径格式:         http://localhost:7001/vslm/item/list/gfsdf  #成功
原来的请求路径格式: http://localhost:7001/vslm/vslm/item/list/gfsdf  #多了个vslm匹配   现在访问不成功了

路由规则方式2的去前缀:
 

zuul:
  routes:
    item-service: /vslm/**
  strip-prefix: false #全局去前缀

访问路径变成了:
请求路径格式:         http://localhost:7001/vslm/item/list/gfsdf   #失败
报错:This application has no explicit mapping for /error, so you are seeing this as a fallback.

还是得这个路径格式访问才能成功: http://localhost:7001/vslm/vslm/item/list/gfsdf  #多了个vslm匹配
结论:去前缀,不能简写路由规则,strip-prefix: false   不生效


那么自动路由规则能去前缀吗?
      
自动路由规则说明:因为没有写路由规则,所以必须设置全局的去前缀,而不是单个服务去前缀
自动路由规则:
zuul自动配置的yml路由规则:

zuul:
  routes:
    服务名: /服务名/**
    


也属于简写配置路由的方式,会报错,所以不能去前缀
访问格式只能:

 http://localhost:7001/服务名(aa-service)/xxcontroller/item/list/gfsdf

prefix配置:即映射路径加前缀

zuul的yml配置:

zuul:
  routes:
    key1:
      path: /vslm/**
      serviceId: item-service
      strip-prefix: false
  prefix: /api
  #strip-prefix: true  #这个是指  prefix: /api  是否生效  默认为true,带前缀;false,不带前缀

访问路径: http://localhost:7001/api/vslm/item/list/456dad

上面说的全局strip-prefix,原来是来管理prefix的,那更不会生效了

结论:所以去前缀只能针对单个服务设置,简洁写法和路由自动配置规则无法去前缀

附正确的配置:

服务的yml:

server:
  port: 8081

spring:
  application:
    name: item-service

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:7000/eureka
  instance:
   # ip-address: ${spring.application.name}:${server.port}
    prefer-ip-address: true #真实ip
    hostname: 127.0.0.1
    instance-id: ${spring.application.name}:${server.port} #服务对应的服务id

zuul的yml:

server:
  port: 7001

spring:
  application:
    name: zuul-service

eureka:
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:7000/eureka
  instance:
    #ip-address: ${spring.application.name}:${server.port}
    prefer-ip-address: true #访问路径可以显示IP地址
    hostname: 127.0.0.1
    instance-id: ${spring.application.name}:${server.port}
ribbon:
  ConnectTimeout: 60000
  ReadTimeout: 60000

zuul:
  routes:
    key1:
      path: /vslm/**
      serviceId: item-service
      strip-prefix: false
  prefix: /api
  #strip-prefix: true  #这个是指  prefix: /api  是否生效
  ignored-services: #服务名集合配置
    - item-service

controller层测试代码:

/**
 * @Date 2019/11/9 04:09
 * by mocar
 */
@RestController
@RequestMapping("/vslm/item")
public class CategoryController {
    @GetMapping("/list/{pid}")
    public String queryByPid(@PathVariable(name = "pid") String pid){
        return pid.toString();
    }
}

正确访问路径:

http://localhost:7001/api/vslm/item/list/hello

zuul知识点介绍参考:https://blog.csdn.net/yangliuhbhd/article/details/80490237

发布了109 篇原创文章 · 获赞 2 · 访问量 5729

猜你喜欢

转载自blog.csdn.net/Seven71111/article/details/102993517