Search code examples
spring-bootspring-cloudmicroservicesnetflix-eurekanetflix-zuul

Zuul url mapping with spring boot,Eureka


I am building Rest api using microservice architecture. I have multiple apis for user that we have made into multiple projects. I have everything else ready except I am not able to map the user facing url to the application url in zuul.

The user facing url is : user/v1/accountholders/{id}/cards and the actual url for my application is /user-cards/v1/accountholders/{id}/cards.

Here id is the path variable. Below are other similar api url so if there is a way to configure them generically in zuul. Also the context root of the application url is also the project name in 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}

Need some help to set this up in the properties or yml file for zuul. I havent been able to make any progress with the mapping stuff yet. Any inputs will be helpful.

SOLVED:- After getting the input from @Daniel (which is the accepted answer)This is what i used in 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

Solution

  • It is possible to achieve what you are trying to do by giving the correct Zuul config. Lets assume you have the user-cardholder service running on port 8081 and the user-account service on 8082 so that you can successfully answer requests going against:

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

    If this is working then you can achive what you are trying for these two services by using the following zuul config:

    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/
    

    Unfortunately you will also have to add more configs – even when they go against the same backend service – due to the fact that internal and external urls are differing. Otherwise you could just add the option stripPrefix: false and use the same internally as externally.