我创建了两个Spring Boot应用程序,它们都将部署在Kubernetes集群中。其中一个应用程序将作为网关,因此使用Spring Cloud Gateway作为依赖项。我还希望使用Spring Cloud Kubernetes集成服务发现,让网关使用服务发现自动生成相应的路由。但是,当我公开运行在本地Minikube集群中的网关应用程序并调用第二个应用/服务时,会收到503错误,并显示以下消息:
使用以下Spring Cloud版本: spring-cloud-starter-kubernetes 1.0.1.RELEASE spring-cloud-starter-gateway 2.1.1.RELEASE
我的演示应用程序位于 https://github.com/nmaoez/spring-cloud-gateway-kubernetes,README.md提供了在本地Minikube集群中部署两个服务的步骤。还显示了所有可用的端点。但是有趣的部分是网关的application.yaml和application类。
之后我在调用
我期望如果我通过网关调用集群中的另一个服务,我会得到一个200响应和基于应用程序REST控制器的正确答案。
Unable to find instance for ...-service
目前我已安装以下内容:
- Minikube
- VirtualBox
- Docker Toolbox
- 通过DiscoveryClient提供集群中所有服务的列表。
- 在应用程序层面根据DiscoveryClient提供的URI调用其他服务。
URI/serviceId
调用其他服务时,我收到了503错误...。使用以下Spring Cloud版本: spring-cloud-starter-kubernetes 1.0.1.RELEASE spring-cloud-starter-gateway 2.1.1.RELEASE
我的演示应用程序位于 https://github.com/nmaoez/spring-cloud-gateway-kubernetes,README.md提供了在本地Minikube集群中部署两个服务的步骤。还显示了所有可用的端点。但是有趣的部分是网关的application.yaml和application类。
spring:
application:
name: gateway-service
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
management:
endpoints:
web:
exposure:
include: '*'
GatewayApplication.java
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class GatewayApplication {
@Autowired
RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/")
@ResponseBody
public String hello() {
return "GatewayApplication says hello!";
}
@GetMapping("/test")
@ResponseBody
public String invokeTestService() {
List<ServiceInstance> testServiceInstances = this.discoveryClient.getInstances("test-service");
return restTemplate.getForObject(testServiceInstances.get(0).getUri(), String.class);
}
@GetMapping("/services")
public List<String> services() {
return this.discoveryClient.getServices();
}
@GetMapping("/services/{serviceId}")
public List<ServiceInstance> servicesById(@PathVariable("serviceId") String serviceId) {
return this.discoveryClient.getInstances(serviceId);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
我设法使其运行,之后在网关服务/application.yaml中覆盖了url-expression字段
url-expression: "uri+'/'"
之后我在调用
gateway-uri/another-service/
后得到了正确的响应。但是我的愿望是不要显式地替换lb://serviceid
的默认值。我该怎么做呢?我期望如果我通过网关调用集群中的另一个服务,我会得到一个200响应和基于应用程序REST控制器的正确答案。