Connection refused to eureka server

81 Views Asked by At

I have eureka-server:

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryServerApplication.class, args);
    }
}

application.properties:

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
server.port=8761

Now i have two microservices that are connected to eureka-server

enter image description here

Inventory-service:

@RestController
@RequestMapping("/api/inventory")
public class InventoryController {

    private final InventoryService inventoryService;

    @Autowired
    public InventoryController(InventoryService inventoryService) {
        this.inventoryService = inventoryService;
    }

    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<InventoryResponse> isInStock(@RequestParam(value = "skuCode") List<String> code) {
        return inventoryService.isInStock(code);
    }
}

application.properties:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/inventory-service
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update

eureka.client.service-url.defaultZone = http://localhost:8761/eureka

server.port=0
spring.application.name=inventory-service

order-service where i have web client:

@Configuration
public class WebClientConfig {

    @Bean
    @LoadBalanced
    public WebClient.Builder webClientBuilder(){
        return WebClient.builder();
    }
}

and service, where i try to call inventory-service:

    InventoryResponse[] inventoryResponses = webClientBuilder.build().get()
            .uri("http://inventory-service/api/inventory", uriBuilder -> uriBuilder.queryParam("skuCode", skuCodes).build())
            .retrieve()
            .bodyToMono(InventoryResponse[].class)
            .block();

application properties for order service:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/order-service
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update


server.port=8081
spring.application.name=order-service

However i am getting "Connection refused: no further information" error.

it seems that the WebClient cannot resolve "http://inventory-service/api/inventory" from eureka.

Do i need some specific explicit way how to get endpoints for inventory-service?

I have seen examples and tutorials that do not use any specific way and use service-name that is registered in eureka as endpoint, however i saw some older that use @EnableEurekaClient and then EurekaClient

What is the correct way?

0

There are 0 best solutions below