Spring Boot 2.5.6 API网关+Eureka服务器=404 [英] Spring Boot 2.5.6 API Gateway + Eureka Server = 404
本文介绍了Spring Boot 2.5.6 API网关+Eureka服务器=404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Spring API网关、Eureka服务器和WebFlux创建项目。
通过春季文档,我已经启动了3项服务:
- 本地主机:3761-尤里卡
- 本地主机:4001-服务
- 本地主机:8080-api网关
网关应用程序.yml-不工作
server:
port: 8080
eureka:
instance:
hostname: localhost
non-secure-port: 8761
prefer-ip-address: true
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.non-secure-port}/eureka/
spring:
application:
name: GATEWAY-SERVER
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: my-service-route
uri: lb://MY-SERVICE
predicates:
- Path=/api/my/service/**
网关应用程序.yml-正在工作
server:
port: 8080
eureka:
instance:
hostname: localhost
non-secure-port: 8761
prefer-ip-address: true
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${eureka.instance.non-secure-port}/eureka/
spring:
application:
name: GATEWAY-SERVER
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: my-service-route
uri: http://localhost:4001
predicates:
- Path=/api/my/service/**
请注意,唯一的区别是URI不查找服务名称。
推荐答案
做一些网关服务测试。我注意到了一些奇怪的事情:
我已经列出了网关上的服务:
GatewayServerApplication
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServerApplication implements CommandLineRunner {
@Autowired
private DiscoveryClient discoveryClient;
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("LIST INSTANCES");
List<String> list = discoveryClient.getServices();
list.forEach(item -> {
System.out.println("Service: " + item);
List<ServiceInstance> instances = this.discoveryClient.getInstances(item);
instances.forEach(instance -> {
System.out.println("URI: " + instance.getUri());
System.out.println("HOST: " + instance.getHost());
System.out.println("PORT: " + instance.getPort());
System.out.println("INSTANCE ID: " + instance.getInstanceId());
System.out.println("SERVICE ID: " + instance.getServiceId());
});
});
}
}
2021-11-13 09:24:35.633 INFO 1547 --- [ main] b.c.f.gateway.GatewayServerApplication : Started GatewayServerApplication in 3.576 seconds (JVM running for 4.419)
LIST INSTANCES
Service: my-service
URI: http://192.168.0.103:8761
HOST: 192.168.0.103
PORT: 8761
INSTANCE ID: mbp-of-rafael:MY-SERVICE:4001
SERVICE ID: MY-SERVICE
URI和端口定义为8761(eureka服务器端口)。但我想应该是4001(我的服务港)。我说错了吗?!
这篇关于Spring Boot 2.5.6 API网关+Eureka服务器=404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文