当我尝试使用 eureka 服务器 Web 客户端构建器访问微服务时,要求提供微服务名称以及非标准端口号。如果我不使用端口号,则会生成错误。
我想使用尤里卡服务器进行服务发现。我的想法是,当您发现微服务时,您无需提及非标准端口地址和微服务名称。但就我而言,我必须提及端口地址这是我的所有代码详细信息:
尤里卡服务器:
应用程序属性:
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
代码春季启动:
@SpringBootApplication
@EnableEurekaServer
public class EurakaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurakaServerApplication.class, args);
}
}
消费者服务:
@Service
public class ConsumerService {
@LoadBalanced
private final WebClient webClient;
public ConsumerService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://microservice1:8080").build();
}
public Mono<String> getProcessedData() {
return webClient
.get()
.uri("/producer/data")
.retrieve()
.bodyToMono(String.class)
.map(data -> data + " - Extra Information Added");
}
}
消费者控制器:
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
private final ConsumerService consumerService;
public ConsumerController(ConsumerService consumerService) {
this.consumerService = consumerService;
}
//localhost:8081/consumer/processed-data
@GetMapping("/processed-data")
public Mono<String> getProcessedData() {
return consumerService.getProcessedData();
}
}
应用程序属性:
server.port=8081
spring.application.name=microservice2
生产者应用程序属性:
server.port=8080
spring.application.name=microservice1
生产者申请:
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}}
错误消息(如果我删除端口号 8080):
Servlet.service() for servlet \[dispatcherServlet\] in context with path \[\] threw exception \[Request processing failed: org.springframework.web.reactive.function.client.WebClientRequestException: finishConnect(..) failed: Connection refused: microservice1/127.0.0.1:80\] with root cause
java.net.ConnectException: finishConnect(..) failed: Connection refused
我不想使用 microservice1:8080(对我来说它有效)。我只想使用“microservice1”。如果我不提及端口 8080,它会自动寻找端口 80。它显示连接拒绝错误。我想使用 http://microservice1 。
谢谢 。