问题springcloud getway配置文件加载失败
问题一
Description:
Failed to bind properties under 'spring.cloud.gateway' to org.springframework.cloud.gateway.config.GatewayProperties:
Reason: HV000083: The specified annotation javax.validation.constraints.NotNull defines no attribute 'groups'.
Action:
Update your application's configuration
问题二
Failed to bind properties under 'spring.cloud.gateway.routes[0].filters[0]' to org.springframework.cloud.gateway.filter.FilterDefinition:
Property: spring.cloud.gateway.routes[0].filters[0]
Value: StripPrefix=1
Origin: class path resource [application.yml]:19:15
Reason: HV000083: The specified annotation javax.validation.constraints.NotNull defines no attribute 'groups'.
Action:
Update your application's configuration
application.yml
server:
port: 9527
spring:
application:
name: getway-server
cloud:
gateway:
# discovery:
# locator:
# enabled: true
routes:
- id: author-server
uri: https://localhost:8888 #转发后的地址
predicates:
- Path=/author/**
filters:
- StripPrefix=1
#eureka:
# client:
# serviceUrl: #注册服务到eureka集群
# defaultZone: http://eureka8761.com:8761/eureka,http://eureka8762.com:8762/eureka,http://eureka8763.com:8763/eureka
# instance:
# instance-id: ${spring.application.name}:${server.port}
# prefer-ip-address: true #访问路径可以显示IP地址
原因
1、配置文件名称不对
应为application.yml
2、依赖导入有问题
getway自动配置源码
GatewayAutoConfiguration.java
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Health.class)
protected static class GatewayActuatorConfiguration {
@Bean
@ConditionalOnProperty(name = "spring.cloud.gateway.actuator.verbose.enabled",
matchIfMissing = true)//默认为true
@ConditionalOnAvailableEndpoint
public GatewayControllerEndpoint gatewayControllerEndpoint(
List<GlobalFilter> globalFilters,
List<GatewayFilterFactory> gatewayFilters,
List<RoutePredicateFactory> routePredicates,
RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator) {
return new GatewayControllerEndpoint(globalFilters, gatewayFilters,
routePredicates, routeDefinitionWriter, routeLocator);
}
@Bean
@Conditional(OnVerboseDisabledCondition.class)
@ConditionalOnAvailableEndpoint
public GatewayLegacyControllerEndpoint gatewayLegacyControllerEndpoint(
RouteDefinitionLocator routeDefinitionLocator,
List<GlobalFilter> globalFilters,
List<GatewayFilterFactory> gatewayFilters,
List<RoutePredicateFactory> routePredicates,
RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator) {
return new GatewayLegacyControllerEndpoint(routeDefinitionLocator,
globalFilters, gatewayFilters, routePredicates, routeDefinitionWriter,
routeLocator);
}
}
默认开启getway执行器配置。但没有导入相应的依赖。
pom.xml
<dependencies>
<!--gateway 网关依赖,内置webflux 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
需导入spring-boot-starter-actuator
pom.xml
<dependencies>
<!--gateway 网关依赖,内置webflux 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
执行成功
测试一下是否转发成功
正常访问
使用getway转发