我有Spring Cloud的基础知识,使用网关(使用Zuul Proxy)代理休息 endpoints (/ 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上运行,而前端应用程序( UI
和 Admin
)只知道 gateway
,并且不知道 ui
和 admin
后端结束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 !!!
}
admin app: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链接是不可取的 . 我怎样才能克服这个问题(如何在管理代码中保留相对引用)?
1 回答
对不起,我还是不遵循这个流程 . 看起来这个角度代码是一个客户端,"admin/write.html"应该通过zuul到http://localhost:8082/write.html?我不知道内部,但也许当你使用'admin'服务ID没有'path'然后zuul不是条带前缀?
试试吧: