【问题标题】:Zuul url mapping with spring boot,EurekaZuul url映射与spring boot,Eureka
【发布时间】:2016-03-21 23:23:59
【问题描述】:

我正在使用微服务架构构建 Rest api。我有多个我们已经制作成多个项目的用户 api。除了我无法将面向用户的 url 映射到 zuul 中的应用程序 url 之外,我已经准备好了其他所有东西。

面向用户的 url 是:user/v1/accountholders/{id}/cards,而我的应用程序的实际 url 是 /user-cards/v1/accountholders/{id}/cards。

这里的 id 是路径变量。下面是其他类似的 api url,所以如果有一种方法可以在 zuul 中通用地配置它们。应用程序url的上下文根也是Eureka中的项目名称。

Other similar urls are:

client side:- /user/v1/accountholders/{id}/cards/{cardid}
application:- /user-cards/v1/accountholders/{id}/cards/{cardid}

client side:- /user/v1/accountholders
application:- /user-cardholder/v1/accountholder

client side:- /user/v1/accountholders
application:- /user-cardholder/v1/accountholder

client side:- /user/v1/accountholders/{id}
application:- /user-cardholder/v1/accountholders/{id}

client side:- /user/v1/accountholders/{id}/accounts
application:- /user-accounts/v1/accountholders/{id}/accounts

client side:- /user/v1/accountholders/{id}/accounts/{accid}
application:- /user-accounts/v1/accountholders/{id}/accounts/{accid}

需要一些帮助才能在 zuul 的属性或 yml 文件中进行设置。我还没有在映射方面取得任何进展。任何输入都会有所帮助。

已解决:- 从@Daniel 获得输入后(这是公认的答案)这是我在 zuul config 中使用的:-

zuul:
 routes:
   User-Cards: 
        path: /user/v1/accountholders/*/cards/**
        url: http://desktop-uvkv1ed:9999/user-cards/v1/accountholders
   User-Transactions1: 
        path: /user/v1/accountholders/*/transactions
        url: http://desktop-uvkv1ed:5555/user-transactions/v1/accountholders
        service-id: User-Transactions
   User-Transactions2:  
        path: /user/v1/accountholders/*/accounts/*/transactions
        url: http://desktop-uvkv1ed:5555/user-transactions/v1/accountholders
        service-id: User-Transactions
   User-Accounts: 
        path: /user/v1/accountholders/*/accounts/**
        url: http://desktop-uvkv1ed:7777/user-accounts/v1/accountholders
   User-Cardholders: 
        path: /user/v1/accountholders/**
        url: http://desktop-uvkv1ed:8888/user-cardholders/v1/accountholders

【问题讨论】:

  • 乍一看,我不认为你想要的目前是可能的,但支持正则表达式映射的拉取请求应该很快就会出现。 github.com/spring-cloud/spring-cloud-netflix/pull/699
  • 你能帮助我使用我可以使用的方法吗?我可以使用应用程序 url,但不能使用面向用户的 url,以便我可以轻松地执行映射。
  • @spencergibb 实际上这可能是因为本质上都可以追溯到AbstractUrlMappingHandler 的子类,它确实支持蚂蚁风格的模式匹配。

标签: spring-boot spring-cloud microservices netflix-eureka netflix-zuul


【解决方案1】:

通过提供正确的 Zuul 配置,可以实现您想要做的事情。假设您在端口 8081 上运行用户持卡人服务,在 8082 端口上运行用户帐户服务,这样您就可以成功回答反对的请求:

http://localhost:8081/user-cardholder/accountholders/{id} 
http://localhost:8082/user-account/accountholders/{id}/accounts/{accid}

如果这有效,那么您可以通过使用以下 zuul 配置来实现您为这两个服务尝试的内容:

zuul:
  routes:
    cardholder:
      path: /user/accountholders/*
      url: http://localhost:8081/user-cardholder/accountholders/
    account:
      path: /user/accountholders/*/accounts/**
      url: http://localhost:8082/user-accounts/accountholders/

不幸的是,由于内部和外部 url 不同,即使它们针对相同的后端服务,您也必须添加更多配置。否则,您只需添加选项 stripPrefix: false 并在内部和外部使用相同的选项。

【讨论】:

  • 谢谢丹尼尔.. 这帮助很大
猜你喜欢
  • 2017-11-13
  • 2018-10-25
  • 2019-03-04
  • 2017-08-08
  • 2016-09-26
  • 1970-01-01
  • 2020-04-15
  • 2016-08-31
  • 2019-03-20
相关资源
最近更新 更多