尝试这个,我认为它可以帮助你解决问题...
这是我的网关bootstrap.yml文件。
spring:
application:
name: gateway-service
---
spring:
profiles: default
cloud:
consul:
config:
prefix: config/dev/
format: FILES
host: localhost
port: 8500
discovery:
prefer-ip-address: true
spring.profiles.active: dev
我将用这个依赖项来作为网关和所有应用程序的通用依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
我使用 Consul 作为我的配置服务器,并将其添加到这些配置中。配置路径为 /config/dev/gateway.yml。
zuul:
prefix: /api
ignoredServices: '*'
host:
connect-timeout-millis: 20000
socket-timeout-millis: 20000
routes:
customer-service:
path: /customer/**
serviceId: customer-service
stripPrefix: false
sensitiveHeaders: Cookie,Set-Cookie
网关服务的Spring Boot应用程序的注释如下所示:
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class GatewayServiceApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServiceApplication.class, args);
}
}
如果您按照这种方式构建应用程序,您可以使用您首选的路由方式。
示例Consul配置![enter image description here](https://istack.dev59.com/PeWys.webp)