运行 API-GATEWAY 时出现以下错误,我尝试了很多方法但无法解决此问题。
描述:
在类路径中发现 Spring MVC,与 Spring Cloud Gateway 不兼容。
行动:
请设置 spring.main.web-application-type=reactive 或删除 spring-boot-starter-web 依赖。
主班
package com.sample.apigateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序.yml
spring:
application:
name: GATEWAY-SERVICE
cloud:
gateway:
routes:
- id: USER-SERVICE
uri: lb://USER-SERVICE
predicates:
- Path=/users/**
- id: DEPARTMENT-SERVICE
uri: lb://DEPARTMENT-SERVICE
predicates:
- Path=/departments/**
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
hostname: localhost
server:
port: …Run Code Online (Sandbox Code Playgroud) 请提出一些小的架构和设计问题。
\n问题:\nKubernetes Ingress 应该与 Spring Cloud Gateway 一起使用吗?如果不是,应该优先选择哪一个?
\n首先,通过 Spring Webflux / Spring Cloud Gateway 项目,我成功实现了基于路由的转发。这意味着,我的所有客户端只需要知道这个并且只有一个 Spring Cloud Gateway 端点,如果 URL 包含 serviceA,Spring Cloud Gateway 将转发到 serviceA,如果 URL 包含 serviceB,则 Spring Cloud Gateway 将转发到 serviceB,等等,简单明了。
\n我添加了更多 \xe2\x80\x9c 软件级别功能 \xe2\x80\x9d 例如动态配置(在运行时更改路由)、断路器、速率限制、舱壁和其他一些功能,非常酷,但实际上,我最终使用路由转发作为主要功能。
\n然后几周前,我花了一些时间研究 Kubernetes,更准确地说是 Kubernetes Ingress。\n我设法了解到 Kubernetes Ingress 是一个非常酷且强大的东西。我设法至少执行基于路由的转发。\n这意味着,客户端只需要知道这一个且唯一的 Ingress 端点,Ingress 将转发到 Kubernetes 集群内的底层服务。截至目前,它将所有内容转发到 Spring Cloud Gateway,Spring Cloud Gateway 将转发其他所有内容。我尝试过,它本来可以转发到真正的业务服务)。
\n这就是我产生怀疑的时刻。
\n我只是重复工作吗?(我的意思是就功能而言,我学习两者都很有趣)。
\n我是否应该考虑一种由 Spring Cloud Gateway(只有他)真正负责控制的架构?
\n我是否应该考虑一种入口和软件网关都具有完全重要性的架构,并在两者中配置功能?(接受重复的工作吗?)
\n我应该完全删除 Spring Cloud Gateway 吗?
\n …我正在使用Spring Cloud Gateway 2.0.0.M6测试一个简单的网关.我只想将一个URL转发到另一个带有**正则表达式的URL
示例1:/ integration/sbl/foo/bar => localhost:4178/a-integration/sbl/foo/bar
示例2:/ integration/sbl/baz/bad => localhost:4178/a-integration/sbl/baz/bad
到目前为止,我已经写了以下内容,但它只转发到http:// localhost:4178/a-integration /
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
String test = "http://localhost:4178/a-integration/";
return builder.routes()
.route("integration-test",
r -> r.path("/integration/sbl/**")
.uri(test)
)
.build();
}
Run Code Online (Sandbox Code Playgroud)
如何修复上述代码以启用此行为?
编辑
我根据下面的回答尝试了以下内容
String samtykke = "http://localhost:4178/";
return builder.routes()
.route("samtykke", r -> r
.path("/gb-integration/sbl/**")
.filters(f -> f.rewritePath("/gb-integration/sbl/(?<segment>.*)", "/gb-samtykke-integration/${segment}"))
.uri(samtykke))
.build();
Run Code Online (Sandbox Code Playgroud)
我尝试了一个GET http:// localhost:4177/gb-integration/sbl/api/sbl/income /并预期http:// localhost:4178/gb-samtykke-integration/api/sbl/income / back但它没用.
输出说:
2018-02-23 09:46:35.197 TRACE 6364 --- [ctor-http-nio-2] o.s.c.g.h.p.RoutePredicateFactory : Pattern "/gb-integration/sbl/**" matches against value …Run Code Online (Sandbox Code Playgroud) 我为我的应用程序创建了API网关,它将充当其他微服务的前端控制器。在生产设置中,我将Nginx用户用作网关的反向代理
API网关在端口8080上运行
Nginx的配置如下
gateway-api.conf:
server {
listen 80;
server_name api.example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://localhost:30010/;
keepalive_timeout 500s;
}
keepalive_timeout 500s;
access_log /var/log/nginx/api.log;
error_log /var/log/nginx/api_error.log;
}
Run Code Online (Sandbox Code Playgroud)
nginx.conf中的超时设置
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
send_timeout 300;
Run Code Online (Sandbox Code Playgroud)
Spring Cloud Gateway Gradle文件:
compile('org.springframework.cloud:spring-cloud-starter-gateway')
compile('org.springframework.cloud:spring-cloud-starter-openfeign')
compile("org.springframework.boot:spring-boot-starter-actuator")
compile('org.springframework.boot:spring-boot-starter-security')
springBootVersion=2.0.3.RELEASE
springDMPVersion=1.0.4.RELEASE
springPlatformBomVersion=Cairo-SR2
springCloudVersion=Finchley.RELEASE
Run Code Online (Sandbox Code Playgroud)
网关应用程序:
@SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
@EntityScan(basePackages = {"com.example"})
@EnableFeignClients(basePackages = "com.example")
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args); …Run Code Online (Sandbox Code Playgroud) java nginx spring-cloud nginx-reverse-proxy spring-cloud-gateway
我使用Spring云网关时遇到问题
如果任何依赖项直接或递归调用spring-boot-starter-tomcat
它将无法工作,因为它将启动嵌入式tomcat服务器而不是Spring云网关使用的netty服务器
我开始通过排除这种依赖性来解决这个问题
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
Run Code Online (Sandbox Code Playgroud)
春云网关成功运作
但有时我想使用spring-cloud-starter-oauth2来使用@ EnableOAuth2Sso
我开始使用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)
那时我面临着抛出异常的大问题
引起:java.lang.IllegalStateException:无法在类org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration上内省带注释的方法......
引起:java.lang.NoClassDefFoundError:javax/servlet/Filter
我在 springboot 应用程序中有 application.yaml 如下
spring:
cloud:
gateway:
routes:
- id: pgService
uri: http://localhost:2005/
predicates:
- Path=/employee/**
- id: inMateService
uri: http://localhost:2006/
predicates:
- Path=/consumer/**
Run Code Online (Sandbox Code Playgroud)
上述声明的变量是针对Spring Cloud Gateway的
我想在 application.properties 文件中声明这些相同的变量。我不想使用 yaml 文件。请帮助我实现这个目标谢谢
java properties-file microservices api-gateway spring-cloud-gateway
您能否指出spring-cloud-starter-gateway工作示例的任何书面或视频教程?使用spring-session-data-redis、Spring Boot 2和可能相关的库,例如 spring security?
基本上,我想在微服务环境中使用 Spring Clouting Gateway 服务器。我也想使用 Spring Boot 2.X。整个系统基于反应式原理。
我找到了很多如何使用 Redis 速率限制器设置spring-cloud-starter-gateway服务器的示例。我有一个网关服务器的工作版本,但它没有使用基于 Redis 的会话管理。当我将 Redis for session 放入图片中时,我会遇到各种异常。因此,非常感谢任何工作示例。
我是 Spring Cloud Gateway 的新手,我想要的是将传入请求记录到相应的路由 url,例如,如果我有以下路由配置:
- id: route1
uri: http://localhost:8585/
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo/(?<segment>.*), /$\{segment}
Run Code Online (Sandbox Code Playgroud)
然后对于“ http://localhost:8080/foo/route1 ”的传入请求,应打印以下日志消息。
“传入请求 url ' http://localhost:8080/foo/route1 ' 被路由到 ' http://localhost:8585/route1 '”
有什么简单的方法可以实现这一点,或者我可以通过设置日志级别来实现这一点。
你能帮忙吗
即使我能够使用给定的主机名直接访问应用程序,通过网关的 Api 调用也会抛出java.net.UnknownHostException
Spring Boot 版本:2.4.2
Spring-cloud.version:2020.0.1
Java 版本:11
注意: Samething 适用于 spring boot 2.3.8.RELEASE和云版本:Hoxton.SR9 with Java 8
日志:
2021-02-10 14:25:05.398 ERROR 12632 --- [ctor-http-nio-4] a.w.r.e.AbstractErrorWebExceptionHandler : [efcc81fd-3] 500 Server Error for HTTP GET "/sample-order-service/sample-order"
java.net.UnknownHostException: failed to resolve 'MY-COMPUTER-NAME' after 2 queries
at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1013) ~[netty-resolver-dns-4.1.58.Final.jar:4.1.58.Final]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ? org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
|_ checkpoint ? HTTP GET "/sample-order-service/sample-order" [ExceptionHandlingWebHandler]
Stack trace:
at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1013) ~[netty-resolver-dns-4.1.58.Final.jar:4.1.58.Final]
at io.netty.resolver.dns.DnsResolveContext.tryToFinishResolve(DnsResolveContext.java:966) …Run Code Online (Sandbox Code Playgroud)