Как настроить динамическую маршрутизацию Spring Cloud Gateway?

проект содержит три микросервиса (мс), указанные в файле settings.gradle:

rootProject.name = 'twoSourcesGr'
includeBuild('regulator')
includeBuild('dsClient1')
includeBuild('portal')

здесь:

regulator = eureka-server, его файл build.gradle:

plugins {
    id 'org.springframework.boot' version '2.7.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'mag.microservices'
version = '1.0'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2021.0.3")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
}

dsClient1 = eureka-client, его файл build.gradle:

plugins {
    id 'org.springframework.boot' version '2.7.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'mag.microservices'
version = '1.0'
sourceCompatibility = '17'

repositories {  mavenCentral()}
ext {   set('springCloudVersion', "2021.0.3")}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}

portal = gateway, его файл build.gradle:

plugins {
    id 'org.springframework.boot' version '2.7.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'mag.microservices'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {  mavenCentral()}
ext {   set('springCloudVersion', "2021.0.3")}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}

соответственно, файлы свойств

regulator:: application.properties

server.port=8081

spring.application.name=twoSources

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://localhost:8081/eureka

dsClient1:: application.properties

server.port=0

spring.application.name=dsClient1

eureka.client.service-url.defaultZone=http://localhost:8081/eureka

portal:: application.properties

    server.port=8082
    
    spring.application.name=portal
    
    eureka.client.service-url.defaultZone=http://localhost:8081/eureka
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

модули проектов стартуют вручную в порядке: regulator, dsClient1, portal. браузер по адресу http://localhost:8081/ показывает, что оба мс работают: eureka

работает также мс dsClient1 на random порту http://localhost:45939/ds1.

/ds1 - это путь в контроллере модуля dsClient1.

но по адресу http://localhost:8082/dsClient1/ds1, где ожидается такой же результат, формируется ошибка 404. порт 8082 назначен севису gateway, но маршрут не работает.

пробовал некоторые настройки файлов application.properties, но безрезультатно.

Как правильно настроить маршрутизацию Spring Cloud Gateway ?


Ответы (0 шт):