spring boot cloud Zuul Proxy - hardcoded proxy route references in code

1.7k views Asked by At

I have the basics of Spring Cloud working with the gateway (with Zuul Proxy) proxying rest end points (/ui, /admin as separate microservices). I am following spring boot blog by Dave Syer

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

The idea is that the gateway(Zuul) will be running on 8080 and the front end apps (UI and Admin) would be aware of only the gateway and have no knowledge of ui and admin backend end URLs. The admin app would simply contact `http://localhost:8080/admin'

The setup works fine except that the front end apps (for ex: admin) would have to hardcode the /admin route as a prefix in all the JS & HTML code. 2 examples below:

Following doesn't work (in 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";       
    }

However, following does work:

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 !!!

    }

Similar issues exist in admin app: index.html

Works (but not desired):

<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>

Doesn't work:

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

Evidently this hardcoding of "/admin" route into all href links in all HTML and JS code in the admin is not desirable. How can I overcome this (how can I keep relative references in my admin code)?

1

There are 1 answers

0
hi_my_name_is On

sorry, I still dont follow this flow. It looks like this angular code is a client and "admin/write.html" should go through zuul to http://localhost:8082/write.html? I dont know internals, but maybe when you are using 'admin' service id without 'path' then zuul is not striping prefix?

Try with it:

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