我在从 api 网关调用一些 url 到一个微服务时遇到问题。 下面显示的两个 url 不起作用,并抛出 404 Not Found 错误。
http://localhost:8765/currency-conversion/currency-conversion/from/USD/to/INR/quantity/10
http://localhost:8765/currency-conversion/currency-conversion-feign/from/USD/to/INR/quantity/10
这里是api网关的应用程序属性文件
spring.application.name=api-gateway
server.port=8765
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.instance.hostname=localhost
# http://localhost:8765/CURRENCY-CONVERSION/currency-conversion/from/USD/to/INR/quantity/10 (Working)
# http://localhost:8765/CURRENCY-CONVERSION/currency-conversion-feign/from/USD/to/INR/quantity/10 (Working)
spring.cloud.gateway.discovery.locator.enabled=true
# http://localhost:8765/currency-conversion/currency-conversion/from/USD/to/INR/quantity/10 (Not Working)
# http://localhost:8765/currency-conversion/currency-conversion-feign/from/USD/to/INR/quantity/10 (Not Working)
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
这是货币转换服务的应用程序属性
文件spring.config.import=optional:configserver:http://localhost:8888
spring.application.name=currency-conversion
server.port=8100
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.instance.hostname=localhost
这是currency-conversion-service的controller
类@RestController
public class CurrencyConversionController {
@GetMapping("/currency-conversion/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversion calculateCurrencyConversion(
@PathVariable String from,
@PathVariable String to,
@PathVariable BigDecimal quantity
) {
}
@GetMapping("/currency-conversion-feign/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversion calculateCurrencyConversionFeign(
@PathVariable String from,
@PathVariable String to,
@PathVariable BigDecimal quantity
) {
}
}
我该如何修复它?
我相信你应该拥有以下房产:
spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true
所以没有破折号,而是驼峰式大小写。
我更改了微服务的名称,如下所示,它起作用了
来自
spring.application.name=货币交换
至
spring.application.name=货币交换-ms
来自
spring.application.name=货币转换
至
spring.application.name=货币转换-ms
如果您使用 Maven 来管理依赖项,则可能需要更新 pom.xml 以使用正确的 Spring Cloud API 网关依赖项。这似乎来自 Udemy 的 in28 分钟关于掌握 Spring 微服务的课程。有课程更新改变原来的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway-mvc</artifactId>
</dependency>
新的依赖项
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
只需去掉artifactId末尾的mvc即可。希望这有帮助。