简体   繁体   中英

Express Gateway. Using multiple services with custom url path

I have 2 services and I want to create an API gateway for these services.

Service 1

  • name: Leon
  • host: api.leon.com

endpoints:

  /
  /jungle

Service 2

  • name: Tiger
  • host: api.tiger.com

endpoints:

 /
 /jungle

Expected Behavior:

API Gateway handle a request as:

apigateway.com/leon          =======>> Leon service '/'
apigateway.com/leon/jungle   =======>> Leon service '/jungle'
apigateway.com/tiger         =======>> Tiger service '/'
apigateway.com/tiger/jungle  =======>> Tiger service '/jungle'

So I want to match the first path of the incoming request URL with services.

And my gateway. config.yaml file like that:

http:
  port: 8080
admin:
  port: 9876
  host: localhost
apiEndpoints:
  leon:
    host: localhost
    methods : [ 'POST', 'GET' ]
    paths: '/*'
  tiger:
    host: localhost
    methods : ['POST', 'GET']
    paths: '/*'
serviceEndpoints:
  leonsrv:
    url: 'http://leon-service.us-west-2.compute.amazonaws.com/'
  tigersrv:
    url: 'http://tiger-service.us-west-2.compute.amazonaws.com/'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  - name: leon
    apiEndpoints:
      - leon
    policies:
      - proxy:
          - action:
              serviceEndpoint: leonsrv
              changeOrigin: true
  - name: tiger
    apiEndpoints:
      - tiger
    policies:
      - proxy:
          - action:
              serviceEndpoint: tigersrv
              changeOrigin: true

But Its not working. I cannot send a request to tiger service.

How Can solve this problem please help!

The two endpoints defined in your apiEndpoints section are the same: http://localhost/*. You need to include more of your target URLs to get allow express-gateway to distinguish between them:

apiEndpoints:
  leon:
    host: localhost
    methods : [ 'POST', 'GET' ]
    paths: [ '/leon/', '/leon/*' ]
  tiger:
    host: localhost
    methods : ['POST', 'GET']
    paths: [ '/tiger/', '/tiger/*' ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM