TP : Application distribuée basée sur deux micro-services (Customer et Billing) en utilisant Spring framework, OpenFeign, Eureka discovery, Gateway.
- Main
@SpringBootApplication
public class CustomerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerServiceApplication.class, args);
}
@Bean
CommandLineRunner start(CustomerService customerService) {
return args -> {
customerService.saveCustomer(new CustomerRequestDTO(UUID.randomUUID().toString(), "Amine", "amine@devhcm.com"));
customerService.saveCustomer(new CustomerRequestDTO(UUID.randomUUID().toString(), "Hamza", "hamza@test.com"));
customerService.saveCustomer(new CustomerRequestDTO(UUID.randomUUID().toString(), "Houssine", "houssine@test.com"));
};
}
}
- OpenFeign
@FeignClient(name = "CUSTOMER-SERVICE")
public interface CustomerRestClient {
@GetMapping(path = "/api/customers/{id}")
Customer getCustomer(@PathVariable String id);
@GetMapping(path = "/api/customers")
List<Customer> allCustomers();
}
- Main
@SpringBootApplication
@EnableFeignClients
public class BillingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BillingServiceApplication.class, args);
}
@Bean
CommandLineRunner start(InvoiceService invoiceService) {
return args -> {
invoiceService.saveInvoice(new InvoiceRequestDTO(BigDecimal.valueOf(Math.random() * 1000), "79de0b79-d6b7-45e7-b355-6015268eb90e"));
invoiceService.saveInvoice(new InvoiceRequestDTO(BigDecimal.valueOf(Math.random() * 1000), "79de0b79-d6b7-45e7-b355-6015268eb90e"));
invoiceService.saveInvoice(new InvoiceRequestDTO(BigDecimal.valueOf(Math.random() * 1000), "1c491bfa-8c29-4cfb-8731-681b107b3654"));
invoiceService.saveInvoice(new InvoiceRequestDTO(BigDecimal.valueOf(Math.random() * 1000), "1c491bfa-8c29-4cfb-8731-681b107b3654"));
};
}
}
@Configuration
public class GatewayConfig {
@Bean
DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(
ReactiveDiscoveryClient rdc,
DiscoveryLocatorProperties dlp
) {
return new DiscoveryClientRouteDefinitionLocator(rdc, dlp);
}
}
The gateway now allows the 2 micro-services to communicate with each other through port : 9999