Microservice -Cloud Configurations -Spring
Let's explore the Microservice deployments ,registration using Service Registry and controlling the communication using APIGateways in next blog.
Will create a microservice Library using H2 databass fro the persistance.
The entire codebase will be shared in the git will share at the end of the blog.
Let's create the Books microservices like below
Controller
Services
Repository
Entity
application.yaml
server:
port: 9000
Postman test output
Create the Books microservices the same way. Refer to the code from the end. Mail goal is to access using the gateway service
Big Step Going to add the microservices to the Service Registry
to do that create one more microservices with the below dependency
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
and create application.yml with the values
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
where 8761 is the default port number usied for the serive resgistry can change if we want.
Currentmicro service is not registering to the service resigrty and not fetching from it so
keeping this properties to false.
Annotate the ServiceRegistryApplication class with @EnableEurekaServer
@SpringBootApplication@EnableEurekaServerpublic class ServiceRegistryApplication {public static void main(String[] args) {SpringApplication.run(ServiceRegistryApplication.class, args);}
}
Yes, by doing this much our service Register is ready 💪.
Once the Service Registry is up will register the microservices to the registry.
Client configurations
Edit the pom to enable Eureka client
Configure application.yml like below
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka
instance:
hostname: localhost
spring:
application:
name: LIBRARY-SERVICE
Annotate the main class with @EnableEurekaClient
After the successful configuration, we are ready for deployment, deploy applications in the order.
I created one more microservices like LIBRARY-SERVICES with the name BOOK-SERVICES.
First, run the ServiceRegistryApplication then the client microservice (Book and Library) where we configured the fetch-registry to true
The benefit of registering the application to the service registry is that we can use the application name ("LIBRARY-SERVICE") to make internal calls. No need to remember about the IP configurations 😉.
See the below example
Before Registering to the Service Registry used to call the application using the IP & port number.
After Registering to the Service Registry can use the Application name like below
Create API -gateway, Client will communicate through API gateway where we used to write all the routing, it's kind of abstraction.
API-Gateway also get registered into the service registry
To create one more service, like Book and Library only changes are in the routing config files
server:
port: 9003
spring:
application:
name: API-GATEWAY
cloud:
gateway:
discovery:
locator:
lower-case-service-id: true
enabled: true
routes:
- id: LIBRARY-SERVICE
uri: http://localhost:9000
predicates:
- Path=/library/**
filters:
- name: CircuitBreaker
args:
name: LIBRARY-SERVICE
fallbackuri: forward:/libraryfallback
- id: BOOK-SERVICE
uri: http://localhost:9001
predicates:
- Path=/books/**
Now all the API can be accessed through port 9003
Code for reference: spring-cloud reference.
Happy coding 😊
Comments
Post a Comment