Create another Spring Project: service-registry
Dependencies: spring-cloud-starter-netflix-eureka-server
Main class enable eureka service
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
Setup application.yml
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
Register EACH services with Eureka, for each services:
spring-cloud-starter-netflix-eureka-clientspring:
application:
name: user-service
# to let the service know Config Server
cloud:
config:
uri: <http://localhost:8888>
eureka:
client:
service-url:
defaultZone: <http://localhost:8761/eureka>
Now when the service start, will registers itself with Eureka. (can check in eureka port)
Create service config in service that want to call other services
@Bean
@LoadBalanced
public WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
When other service need another service, call the service name
@Service
public class OrderService {
private final WebClient webClient;
public Mono<UserDto> getUser(String userId) {
return webClient
.get()
**.uri("<http://user-service/users/{id}>", userId)**
.retrieve()
.bodyToMono(UserDto.class);
}
}