我有一个简单的产品项目,我想在其中创建将注册到 Eureka 服务器的产品微服务和 API 网关的多个实例。
我有以下尤里卡服务器配置:
应用程序.properties:
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.instance.prefer-ip-address=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
API-Gateway 微服务有以下 application.properties:
spring.application.name=api-gateway
server.port=8082
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
我有产品微服务的以下 application.properties
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.application.name=product-service
server.port=0
eureka.instance.instance-id=${spring.application.name}:${instanceId:${random.value}}
我已经为 /products 映射创建了 RestController
@RestController
@RequestMapping("/products")
@Slf4j
public class ProductController {
private final CommandGateway commandGateway;
public ProductController(CommandGateway commandGateway) {
this.commandGateway = commandGateway;
}
@PostMapping
public String createProduct(@RequestBody CreateProductDTO createProductDTO) {
...
}
}
当我启动应用程序并检查 localhost:8761 时,我注意到网关微服务的主机是 host.docker.internal
当我从 Postman 应用程序发送请求时,我收到以下响应: