【发布时间】: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 网关上,会尝试在某处发布整个代码..
标签: spring spring-boot api-gateway