无法使用 API Gateway Spring Cloud 调用实际服务
Posted
技术标签:
【中文标题】无法使用 API Gateway Spring Cloud 调用实际服务【英文标题】:Unable to make a call to actual service using API Gateway Spring Cloud 【发布时间】:2021-07-26 04:07:05 【问题描述】:我正在使用 Spring Boot v2.4.2.RELEASE 开发 Spring Boot 微服务,但无法调用通过 API 网关提供实际服务。源代码参考取自这里 - https://www.youtube.com/watch?v=BnknNTN8icw&t=1s,仅使用最新的依赖项
API 网关代码 - MainApp.java
@SpringBootApplication
@EnableEurekaClient
public class CloudApiGatewayApplication
public static void main(String[] args)
SpringApplication.run(CloudApiGatewayApplication.class, args);
@Bean
public ServerCodecConfigurer serverCodecConfigurer()
return ServerCodecConfigurer.create();
bootstrap.yml
spring:
cloud:
config:
enabled: true
uri: http://localhost:9296
application.yml
server:
port: 9191
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka
instance:
hostname: localhost
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/users/**
- id: department-service
uri: lb://department-service
predicates:
- Path=/departments/**
discovery:
locator:
lower-case-service-id: true
enabled: true
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>cloud-api-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-api-gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
bootstrap.yml
spring:
cloud:
config:
enabled: true
uri: http://localhost:9296
application.properties
server.port=9001
spring.application.name=department-service
#spring.zipkin.base-url=http://127.0.0.1:9411/
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.instance.ip-address=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
eureka.instance.hostname=localhost
DepartmentController.java
@RestController
@RequestMapping("/departments")
public class DepartmentController
@Autowired
private DepartmentService departmentService;
@PostMapping("/")
public Department saveDepartment(@RequestBody Department department)
return departmentService.saveDepartment(department);
@GetMapping("/id")
public Department findDepartmentById(@PathVariable("id") Long departmentId)
return departmentService.findDepartmentById(departmentId);
【问题讨论】:
使用 Eureka UI 中的所有可用服务?共享网关和部门服务的堆栈跟踪 嘿,是的,所有服务都在 eureka 中可用,想知道为什么连日志都没有出现在 API 网关上,会尝试在某处发布整个代码.. 【参考方案1】:将以下属性添加到 API-GATEWAY 的 application.yml 以确保网关发现手动设置为 true 并且两个服务名称全部大写:
server:
port: 9191
spring:
application:
name: API-GATEWAY
cloud.gateway:
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: users
uri: lb://USER-SERVICE
predicates:
- Path=/users/**
- id: albums
uri: lb://DEPARTMENT-SERVICE
predicates:
- Path=/departments/**
【讨论】:
以上是关于无法使用 API Gateway Spring Cloud 调用实际服务的主要内容,如果未能解决你的问题,请参考以下文章
API-GATEWAY 不起作用。抛出 java.net.UnknownHostException:无法解决
使用 Spring Cloud Gateway 实现微服务 API 网关
实现Spring Cloud Gateway路由动态加载及持久化
有没有办法在路由之前应用过滤器或代码(Spring Cloud Gateway)
spring api gateway 不会将我重定向到 keycloak 提供的 spring-cloud-gateway-client url