kubernetes - Kubernetes 服务发现中的 Spring Cloud Gateway 配置问题
问题描述
我正在尝试将 spring 云网关与 kubernetes 服务发现一起使用。以下是我正在使用的设置
构建.gradle
plugins {
id 'org.springframework.boot' version '2.2.0.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
ext {
set('springCloudVersion', "Hoxton.BUILD-SNAPSHOT")
set('springCloudKubernetesVersion', "1.0.3.RELEASE")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
implementation 'org.springframework.cloud:spring-cloud-starter-kubernetes'
implementation 'org.springframework.cloud:spring-cloud-starter-kubernetes-ribbon'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "org.springframework.cloud:spring-cloud-kubernetes-dependencies:${springCloudKubernetesVersion}"
}
}
test {
useJUnitPlatform()
}
应用程序.yml
spring:
application.name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
kubernetes:
reload:
enabled: true
server:
port: 8080
logging:
level:
org.springframework.cloud.gateway: TRACE
org.springframework.cloud.loadbalancer: TRACE
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
enabled: true
info:
enabled: true
DemoApplication.java
package com.example.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class DemoApplication {
@Autowired
private DiscoveryClient discoveryClient;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/services")
public List<String> services() {
return this.discoveryClient.getServices();
}
}
Spring Cloud Gateway 无法将请求重定向到其他服务。正在打印的日志是
2019-10-13 18:29:38.303 TRACE 1 --- [or-http-epoll-2] oscgfWeightCalculatorWebFilter:权重 attr:{} 2019-10-13 18:29:38.305 TRACE 1 --- [or-http -epoll-2] oscghRoutePredicateHandlerMapping : 找不到 [Exchange: GET http://gateway-url/service-name/hello] 的RouteDefinition
虽然当我打电话时http://<gateway-url>/services
,我可以看到所有服务的列表。因此,所有权限都在 pod 级别提供,并且服务发现工作正常。我很确定有一些配置,我错过了,但即使在多次查看文档后我也无法弄清楚。
解决方案
因此, Spring Cloud Hoxton.M3 release中似乎存在问题,因为它与Hoxton.M2 工作正常。
我已经为此打开了一个问题。
推荐阅读
- flutter - 什么主题元素控制 TextField 小部件的焦点颜色
- angular - Angular 8 - 如何拦截来自(rxjs)的http错误?
- ios - 如何从 AppDelegate 显示 iOS 13 SwiftUI 视图?
- laravel - Laravel:如何检查数组是否为空?
- python - 如何修复 for 循环中的“KeyError:2”?
- sql - 获取员工最近的薪水
- java - 使用 mockmvc 进行测试时无法模拟存储库
- c# - C# 对象序列化
- java - java - 如何使用java在黄瓜中传递布尔参数?
- c# - 当我按下跳跃键时,Unity2D我的角色跳跃不止一个