我们已经使用 Spring Cloud 堆栈构建了一组微服务,我在开发时使用 Eureka 服务作为服务发现,但在将其移动到 Kubenretes 时,我们有两个选择
- 继续使用 Eureka 作为发现服务
- 开始使用原生 Kubernetes 服务
因此,我们选择了#2,因为使用 Kubernetes 原生服务比使用另一层重定向更有意义。
我们使用 Spring Cloud Zuul 作为 API Gatway 启用@DiscoveryClient
以获取部署在命名空间中的服务,以便 Zuul 可以代理对底层服务的调用,设置工作良好,直到我们向命名空间添加新服务,API 网关无法找出新的添加的服务因此无法代理调用它,但是,如果重新启动 API 网关,那么它会发现添加的服务,所以它似乎无法即时解析新添加的服务,是否有配置或 jar我的设置中缺少
这是我的 API 网关 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.org.apigw</groupId>
<artifactId>api-gateway</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.org.starters</groupId>
<artifactId>org-starters</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>com.org.starters</groupId>
<artifactId>org-observability-starter</artifactId>
</dependency>
<!-- enable ribbon auto retries -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我的 API Gateway 配置如下所示:-
spring:
application:
name: api-gateway
cloud.kubernetes.discovery.enabled: false
server:
port: 9191
countryCode: ""
opa:
host: http://localhost:8181
jwt:
secret: "this is test secret key for development"
expiration: 86400000
tokenValidationEnabled: true
# fix gateway timeouts
hystrix.command.default.execution.timeout.enabled: false
ribbon:
ReadTimeout: 30000
ConnectTimeout: 30000
MaxAutoRetries: 1
zuul:
ribbon:
eager-load:
enabled: true
host:
connect-timeout-millis: 30000
socket-timeout-millis: 30000
# maxTotalConnections: 1700
# maxPerRouteConnections: 100
sensitiveHeaders:
ignoredServices: ""
#ignoredPatterns:
# - /as/*
routes:
service1:
path: /svc1/**
serviceId: svc1
stripPrefix: true
customSensitiveHeaders: false
service2:
path: /svc1/**
serviceId: svc2
stripPrefix: true
customSensitiveHeaders: false
include-debug-header: true
debug.request: true
debugFilters.disabled: false
logging.level:
com.netflix.zuul: debug