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

Thanh Nguyen Van
  • 10,292
  • 6
  • 35
  • 53
CHETAN PATIL
  • 101
  • 1
  • 1
  • 4

5 Answers5

11

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/**
khwilo
  • 481
  • 4
  • 9
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

Capslock10
  • 796
  • 2
  • 16
  • 37
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/**
Superman
  • 221
  • 2
  • 11
1
## 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
Yuvi
  • 11
  • 1
  • 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](/help/how-to-answer). – Community Jun 06 '22 at 07:57
0

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

Jiao
  • 111
  • 1
  • 3