springcloud api gateway properties file declaring variables
Asked Answered
I

5

10

I have application.yaml in springboot app as below

spring:
  cloud:
    gateway:
      routes:
      - id: pgService
        uri: http://localhost:2005/
        predicates:
        - Path=/employee/**
      - id: inMateService
        uri: http://localhost:2006/
        predicates:
        - Path=/consumer/**

The above declared variables are with respect to spring cloud gateway

I want to declare these same variables in application.properties file. i don't want to use yaml file. please help me to achieve this Thank you

Isocrates answered 4/11, 2020 at 7:8 Comment(0)
O
13

You can define the variables as follows in your application.properties file:

spring.cloud.gateway.routes[0].id=pgService
spring.cloud.gateway.routes[0].uri=http://localhost:2005/
spring.cloud.gateway.routes[0].predicates[0]=Path=/employee/**
spring.cloud.gateway.routes[1].id=inMateService
spring.cloud.gateway.routes[1].uri=http://localhost:2006/
spring.cloud.gateway.routes[1].predicates[0]=Path=/consumer/**
Onslaught answered 11/9, 2021 at 7:19 Comment(0)
L
3

Just updated the ans.

It should be similer to this format:

spring.cloud.gateway.discovery.locator.predicates[0].name: Path
spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/'+serviceId+'/**'"
spring.cloud.gateway.discovery.locator.predicates[1].name: Host
spring.cloud.gateway.discovery.locator.predicates[1].args[pattern]: "'**.foo.com'"
spring.cloud.gateway.discovery.locator.filters[0].name: Hystrix
spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId
spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath
spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/(?<remaining>.*)'"
spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"

Ref. https://cloud.spring.io/spring-cloud-gateway/multi/multi__configuration.html


If you are using intelliJ, the below plugin is so helpful for converting the format between .yaml and .properties: https://plugins.jetbrains.com/plugin/13804-convert-yaml-and-properties-file

Labelle answered 4/11, 2020 at 7:23 Comment(0)
C
2

It worked for me


spring.cloud.gateway.routes[0].id=USER-SERVICE
spring.cloud.gateway.routes[0].uri=lb://USER-SERVICE
spring.cloud.gateway.routes[0].predicates[0].name=Path
spring.cloud.gateway.routes[0].predicates[0].args[pattern]=/user/**

spring.cloud.gateway.routes[1].id=USER-SERVICE
spring.cloud.gateway.routes[1].uri=lb://USER-SERVICE
spring.cloud.gateway.routes[1].predicates[0].name=Path
spring.cloud.gateway.routes[1].predicates[0].args[pattern]=/department/**
Cammiecammy answered 18/9, 2021 at 14:58 Comment(1)
U can change the id and uri in second declaration.Diphyodont
M
1

works for me

spring.cloud.gateway.routes[0].id=USER-SERVICE
spring.cloud.gateway.routes[0].uri=lb://USER-SERVICE
spring.cloud.gateway.routes[0].predicates[0]=Path=/v1/users/**
spring.cloud.gateway.routes[1].id=DEPARTMENT-SERVICE
spring.cloud.gateway.routes[1].uri=lb://DEPARTMENT-SERVICE
spring.cloud.gateway.routes[1].predicates[0]=Path=/v1/departments/**

In my cotroller class:

@RestController
@RequestMapping("/v1/users")
public class UserController {


@RestController
@RequestMapping("/v1/departments")

You need to change them

Michaelmichaela answered 8/11, 2022 at 22:12 Comment(0)
S
0
## microservices mapping ##
spring.cloud.gateway.routes[0].id=product-service
spring.cloud.gateway.routes[0].uri=lb://product-service
#spring.cloud.gateway.routes[0].uri=http://localhost:8002/
spring.cloud.gateway.routes[0].predicates[0]=Path=/product/api/**
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=2

## microservices mapping ##
spring.cloud.gateway.routes[1].id=product-service
#spring.cloud.gateway.routes[1].uri=lb://product-serviceed
spring.cloud.gateway.routes[1].uri=http://localhost:8003
spring.cloud.gateway.routes[1].predicates[0]=Path=/user/api/**
spring.cloud.gateway.routes[1].filters[0]=StripPrefix=2

Please refer code here : https://github.com/sunilkul/spring-cloud- 
microservice/tree/spring-cloud-demo
Saponaceous answered 6/6, 2022 at 7:5 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Permit

© 2022 - 2024 — McMap. All rights reserved.