我目前正在使用spring cloud gateway项目构建简单的api网关,计划是将路由持久化到mongodb中,然后刷新,以便新路由可用。我做了这样简单的事情来从 mongo 获取我的路线。
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder){
List<CreateAPIRequest> apiRequestList = repository.findAll();
RouteLocatorBuilder.Builder routeLocator = builder.routes();
for (CreateAPIRequest request: apiRequestList) {
routeLocator
.route(r-> {
r.path("/"+request.getProxy().getListenPath())
.filters(f->f.stripPrefix(1))
.uri(request.getProxy().getTargetUrl())
});
}
return routeLocator.build();
}
Run Code Online (Sandbox Code Playgroud)
我能够在数据库中创建新路线,但无法即时刷新。
我需要了解如何动态刷新路线。
谢谢
我想要部署 Spring Cloud Gateway,但它是基于 Spring Framework 5、Project Reactor 构建的。
我可以创建一个 WAR 文件并将其部署在传统应用服务器(例如 Jboss 或 Tomcat)上吗?
本页中的 Spring 文档说这是可能的。
Tomcat、Jetty、Servlet 3.1+ 容器支持 Spring WebFlux
注意:这是 Spring Cloud Gateway 项目链接 https://spring.io/projects/spring-cloud-gateway
最基本的 Spring Cloud Gateway 应用程序无法正常工作。
应用程序.yml
server:
port: 9000
Run Code Online (Sandbox Code Playgroud)
网关应用程序.java
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("test", r -> r.path("/test")
.uri("http://httpbin.org:80"))
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
邮递员:http://localhost:9000/test
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL
manually please check your spelling and try again.</p>
Run Code Online (Sandbox Code Playgroud)
邮递员: http://localhost:9000/test2
{
"timestamp": …Run Code Online (Sandbox Code Playgroud) 我尝试使用 Keycloak Spring Boot Adapter 来保护我的 Spring Cloud Gateway。添加依赖项后,它立即向我显示此错误keycloak-spring-boot-starter:
**********************************************************
Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.
**********************************************************
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gatewayConfigurationService' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]: Unsatisfied dependency expressed through method 'gatewayConfigurationService' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.convert.ConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=webFluxConversionService)}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798) …Run Code Online (Sandbox Code Playgroud) spring spring-boot spring-cloud keycloak spring-cloud-gateway
感谢您对解决以下问题的任何帮助。我正在使用 Spring Cloud Hoxton.SR4 并使用 spring-cloud-starter-gateway 创建了一个网关服务。应用程序在启动时抛出以下异常。
Run Code Online (Sandbox Code Playgroud)org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'discoveryClientRouteDefinitionLocator' defined in class path resource [org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfiguration$ReactiveDiscoveryClientRouteDefinitionLocatorConfiguration.class]: Unsatisfied dependency expressed through method 'discoveryClientRouteDefinitionLocator' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'discoveryLocatorProperties' defined in class path resource [org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties]: Factory method 'discoveryLocatorProperties' threw exception; nested exception is java.lang.NoClassDefFoundError: javax/validation/ValidationException at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:539) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] …
我正在进入 Spring Cloud 2020.0.0 世界。
\n我有一个配置服务器、网关服务器、尤里卡服务器和第一个微服务设置,并部署在开发环境中,它们都可以在其中相互通信。它们都是配置优先的,并且都成功从配置服务器拉取属性源,然后向尤里卡服务器注册。因为至少现在我没有为我的服务应用程序设置 DNS,所以我有preferIpAddress: true.
将日志级别跟踪 spring-cloud-gateway,我看到发现客户端选择了这些 eureka 注册的应用程序:
\n 2021-01-28 14:49:28.438 DEBUG 14432 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition ReactiveCompositeDiscoveryClient_CLOUD-SERVICES-CONFIG applying {pattern=/CLOUD-SERVICES-CONFIG/**} to Path\n \n 2021-01-28 14:49:28.476 DEBUG 14432 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition ReactiveCompositeDiscoveryClient_CLOUD-SERVICES-CONFIG applying filter {regexp=/CLOUD-SERVICES-CONFIG/(?<remaining>.*), replacement=/${remaining}} to RewritePath\n \n 2021-01-28 14:49:28.509 DEBUG 14432 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : RouteDefinition matched: ReactiveCompositeDiscoveryClient_CLOUD-SERVICES-CONFIG\nand so on...\nRun Code Online (Sandbox Code Playgroud)\n在本地运行 API 网关(我也有一个部署的实例),Postman GET @http://localhost:8080/DATA-SERVICES-PERSON-1_0结果Route matched: ReactiveCompositeDiscoveryClient_DATA-SERVICES-PERSON-1_0以及
我正在寻找自定义我的 oauth2 登录页面。我正在使用 Spring cloud Gateway,所以这是 Spring Security Webflux。这应该相当简单,但我无法找到一种利用 Webflux 安全性来做到这一点的方法。我尝试过的东西:
@Bean
public SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
return http
.csrf().disable().authorizeExchange()
.pathMatchers(HttpMethod.GET,"/oauth2login",
"*/*.js", "/*.json", "/*.ico", "/login").permitAll()
.pathMatchers("/api/","/api/**").authenticated()
.and().oauth2ResourceServer(ServerHttpSecurity.OAuth2ResourceServerSpec::jwt)
.authorizeExchange().anyExchange().authenticated()
.and().securityContextRepository(new WebSessionServerSecurityContextRepository())
.oauth2Login()
.and().build();
}
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
spring-security spring-security-oauth2 spring-webflux spring-cloud-gateway
我的架构中有几个微服务。我想实现一个 API 网关来将请求路由到服务。为了实现这一目标,我实施了spring-cloud-gateway这就是我的application.yml
server:\n port: 9090\nspring:\n application:\n name: "API-GATEWAY"\n cloud:\n gateway:\n routes:\n - id: task-service\n uri: 'http://localhost:8083'\n predicates:\n - Path=/task/**\nRun Code Online (Sandbox Code Playgroud)\n到目前为止,一切都按预期进行。一个请求localhost:9090/task/123是localhost:8083/task/123. 下面就到了第二部分。
我希望某些用户只能访问某些端点。在我的 JWT 令牌中,我有角色字段。
\n {\n "accountName": "erdem.ontas",\n "surname": "\xc3\x96nta\xc5\x9f",\n "roles": [\n "ADMIN",\n "USER"\n ],\n}\nRun Code Online (Sandbox Code Playgroud)\n我不想在每个服务中单独指定授权,有没有办法在 spring-cloud-gateway 中指定基于角色的访问?例如,我希望 USER 角色能够访问GET http://localhost:9090/task/但不能访问GET http://localhost:9090/dashboard/
当我使用这样的路由统计时,我正在使用 Spring cloud gateway(它工作正常):
@Bean
RouteLocator routeLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route((r) -> r.path("/customers/**").uri("http://localhost:8081/"))
.route((r) -> r.path("/products/**").uri("http://localhost:8082/"))
.build();
}
Run Code Online (Sandbox Code Playgroud)
但是当我想使用带有 DiscoveryClientRouteDefinitionLocator 的动态路由时,我的功能不起作用:
@Bean
DiscoveryClientRouteDefinitionLocator definitionLocator(
ReactiveDiscoveryClient rdc,
DiscoveryLocatorProperties properties) {
return new DiscoveryClientRouteDefinitionLocator(rdc, properties);
}
Run Code Online (Sandbox Code Playgroud)
这是我的 application.properties :
server.port=8888
spring.application.name=gateway-service
spring.cloud.discovery.enabled=true
Run Code Online (Sandbox Code Playgroud)
当我调用这个 uri http://localhost:8888/PRODUCT-SERVICE/products 我得到:
Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
Tue Dec 22 00:42:19 WEST 2020
[97127699-3] There was an unexpected error (type=Internal Server Error, status=500).
Run Code Online (Sandbox Code Playgroud)
控制台中生成的异常: …