• Spring Cloud Gateway
• Eureka Discovery
• Open Feign Rest Client
• Hystrix DashBoard
Project Class Diagram
Create a new Spring project :
Selected dependencies
• Spring Web
• Spring Data JPA
• H2 Database
• Rest Repositories
• Lombok
• Spring Boot DevTools
• Eureka Discovery Client
• Spring Boot Actuator
Application Configuration
application.properties
spring.application.name=customer-service
spring.datasource.url=jdbc:h2:mem:customers-db
spring.cloud.discovery.enabled=true
#management.endpoints.web.exposure.include=*
server.port=8888
package ma.enset.customermicroservice.entites;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Entity
@Data @NoArgsConstructor @AllArgsConstructor @ToString
public class Customer {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
}
package ma.enset.customermicroservice.repository;
import ma.enset.customermicroservice.entites.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}
Database H2
Application Configuration
application.properties
spring.application.name=inventory-service
spring.datasource.url=jdbc:h2:mem:products-db
spring.cloud.discovery.enabled=true
server.port=8889
- Create
Product
entity - Create
ProductRepository
Spring Data based interface - Deploy Restful API of microservice with Spring Data Rest
- Microservice test
|
|
Selected dependencies
• Gateway
• Spring Boot Actuator
• Hystrix
• Eureka Discovery Client
application.properties
spring.application.name=gateway-service
spring.cloud.discovery.enabled=true
server.port=8890
application.yml
spring:
cloud:
gateway:
routes:
- id : r1
uri : http://localhost:8888/
predicates :
- Path= /customers/**
- id : r2
uri : http://localhost:8889/
predicates :
- Path= /products/**
|
|
@Bean
RouteLocator routeLocator(RouteLocatorBuilder builder){
return builder.routes()
.route("r1", (r) -> r.path("/customers/**").uri("http://localhost:8888/"))
.route("r2", (r) -> r.path("/products/**").uri("http://localhost:8889/"))
.build();
}
|
|
Selected dependencies
• Eureka Server
EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaDiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaDiscoveryApplication.class, args);
}
}
application.properties
server.port=8761
# dont register server itself as a client.
eureka.client.fetch-registry=false
# Does not register itself in the service registry.
eureka.client.register-with-eureka=false
Static routes configuration with Discovery Service
@Bean
RouteLocator routeLocator(RouteLocatorBuilder builder){
return builder.routes()
.route("r1", (r) -> r.path("/customers/**").uri("lb://CUSTOMER-SERVICE"))
.route("r2", (r) -> r.path("/products/**").uri("lb://INVENTORY-SERVICE"))
.build();
}
|
|
Dynamic routes configuration with Discovery Service
@Bean
DiscoveryClientRouteDefinitionLocator definitionLocator(ReactiveDiscoveryClient rdc,
DiscoveryLocatorProperties dlp){
return new DiscoveryClientRouteDefinitionLocator(rdc, dlp);
}
|
|
Selected dependencies
• OpenFeign
• Spring HATEOAS
Service Test
|
|
|
|
Link to Front End part.