When I am try to access microservice using eureka server webclient builder asking for microservicename along with no-standard port number. If I am not use port number it is generating error.
I want to use eureka server for service discovery. My idea is when you discovering a microservice you need not to mention non-standerd port address along with microservice name . But in my case I have to mention port address Here is my all code details :
eureka server :
application.properties:
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
code spring boot :
@SpringBootApplication
@EnableEurekaServer
public class EurakaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurakaServerApplication.class, args);
}
}
consumerservice:
@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");
}
}
Consumer Controller:
@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();
}
}
application.properties:
server.port=8081
spring.application.name=microservice2
Producer application.properties:
server.port=8080
spring.application.name=microservice1
ProducerApplication :
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}}
The error message(if I remove port number 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
I don't want to use microservice1:8080(for me it is working). I want to use only "microservice1".If I am not mentioning port 8080 the it automatically looking for port 80. It showing connection refuse error. I want to use http://microservice1 .
Thanks .
create your own configuration class and create a builder for your service by giving the baseUrl
without the port (only server name) like this.
@Configuration
@AllArgsConstructor
public class UtilConfigWebClient {
private final EurekaClient eurekaClient;
/**
* this is only for microservice1
*
* @return {@link WebClient.Builder} with base url as http://microservice1
*/
@Bean("microservice1WebClientBuilder")
@LoadBalanced
public WebClient.Builder paymentWebClientBuilder() {
return WebClient.builder()
.baseUrl("http://microservice1");
}
}
Inject that bean (It should be the WebClient.Builder. Not a WebClient) in your ConsumerService service by using @Qualifier
Like this.
@Service
public class ConsumerService {
private final WebClient.Builder microservice1WebClient;
public ConsumerService(@Qualifier("microservice1WebClientBuilder") WebClient.Builder microservice1WebClient) {
this.microservice1WebClient = microservice1WebClient;
}
public Mono<String> getProcessedData() {
return microservice1WebClient.build()
.get()
.uri("/producer/data")
.retrieve()
.bodyToMono(String.class)
.map(data -> data + " - Extra Information Added");
}
}