【问题标题】:spring boot cloud Zuul Proxy - hardcoded proxy route references in codespring boot cloud Zuul Proxy - 代码中的硬编码代理路由引用
【发布时间】:2015-06-27 05:43:50
【问题描述】:

我有 Spring Cloud 使用网关(使用 Zuul 代理)代理休息端点(/ui、/admin 作为单独的微服务)的基础知识。我关注spring boot blog by Dave Syer

zuul:
  routes:
    ui:
      url: http://localhost:8081
    admin:
      url: http://localhost:8082
    resource:
      url: http://localhost:9000

这个想法是 gateway(Zuul) 将在 8080 上运行,前端应用程序(UIAdmin)将只知道 gateway 而不知道 uiadmin 后端 URL。管理应用程序只需联系 `http://localhost:8080/admin'

除了前端应用程序(例如:admin)必须将 /admin 路由硬编码为所有 JS 和 HTML 代码中的前缀之外,设置工作正常。以下2个例子:

以下不起作用(在 admin.js 中)。

angular.module('admin', []).controller('home',
function($scope, $http) {

    var computeDefaultTemplate = function(user) {
        $scope.template = user && user.roles &&
        user.roles.indexOf("ROLE_WRITER")>0 ? "write.html" : "read.html";       
    }

但是,以下确实有效:

angular.module('admin', []).controller('home',

function($scope, $http) {

    var computeDefaultTemplate = function(user) {
        $scope.template = user && user.roles && 
        user.roles.indexOf("ROLE_WRITER")>0 ? "admin/write.html" : "admin/read.html";       
    // ..... !! NOTE the hardcoding of 'admin/' prefix route !!!

    }

管理应用中存在类似问题:index.html

有效(但不需要):

<body ng-app="admin" ng-cloak class="ng-cloak" ng-controller="home">
    ...
    ..... !! NOTE the hardcoding of 'admin/' prefix route !!!
    <script src="admin/js/admin.js" type="text/javascript"></script>
    ....
</body>
</html>

不起作用:

<body ng-app="admin" ng-cloak class="ng-cloak" ng-controller="home">
    ...
    <script src="js/admin.js" type="text/javascript"></script>
    ....
</body>
</html>

显然,这种将“/admin”路由硬编码到管理员中所有 HTML 和 JS 代码中的所有 href 链接的做法是不可取的。我该如何克服这个问题(如何在我的管理代码中保留相关引用)?

【问题讨论】:

  • 那么'admin'服务中localhost:8082上的这个角码吗?
  • 是的。我将使用该注释编辑问题
  • 如果你是通过zuul路由器,你必须知道上下文。我看不到在你的 js 中有“/admin”的任何方法。为什么这是“不可取的”?
  • 您有没有找到合适的解决方案?我有一个类似的问题,我希望 zuul 去除前缀(例如 /myapp/api/v1/**),所以我不必在我的 ui 服务中硬编码此路径(即不为所有 css/ 加上前缀js 和提交/操作)

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


【解决方案1】:

对不起,我还是不遵循这个流程。看起来这个角代码是一个客户端,“admin/write.html”应该通过zuul到http://localhost:8082/write.html?我不知道内部情况,但也许当您使用没有“路径”的“管理员”服务 ID 时,zuul 不是条带前缀?

试试看:

zuul:
  routes:
    admin:
      path: /admin/**
      stripPrefix: true
      url: http://localhost:8082

【讨论】:

    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 2020-10-25
    • 2017-12-02
    • 2015-05-23
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    相关资源
    最近更新 更多