An application can consist with multiple microservices. These microservices can run independently. If we want to stop one of them, other microservices can keep working. So we can stop/update/delete one of them without stop whole project. Also microservices can use other microservices instance.
We will create a simple Spring Boot Microservices projec with using Eureka.You can reach the project repo via the link below:
Creating Eureka Server (eureka-server)
First we generate a Spring Boot project with using Spring Initializr. As dependency we only add Eureka Server. This project will our Eureka server and all other microservices will connect this server.
After download generate project, unzip and open it in IDE (I use IntelliJ)
Add @EnableEurekaServer annotation to Application Context class:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
And add these required properties below in application.properties:
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Creating Microservice (currency-service)
We generate another Spring Boot project with Spring Initializr and as dependencies we add Eureka Discovery Client, Spring Web and DB dependencies (JPA, H2) because we will get data from DB.
Dowload, unzip, IDE and @EnableDiscoveryClient annotation to Application Contect class.
@SpringBootApplication
@EnableDiscoveryClient
public class CurrencyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CurrencyServiceApplication.class, args);
}
}
Then these properties to application.properties:
spring.application.name=currency-service
server.port=8000
spring.jpa.show-sql=true
spring.h2.console.enabled=true
eureka.client.service-url.default-zone=http://localhost:8761/eureka
And we create some Spring Boot things (Entity, Repository, Service, Controller...). You can see the details in github repository (link above) .
Creating Microservice (convertor-service)
Just the same way with previous step, we also add openfeing while we generate the project. And we manually add netflix-ribbon dependency.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-ribbon</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
Add @EnableDiscoveryClient annotation with the same way. And application.properties:
spring.application.name=convertor-service
server.port=8100
eureka.client.service-url.default-zone=http://localhost:8761/eureka
And we add some stuff, if you check the codes in github repository, we use these codes in RestConsumer class:
List<ServiceInstance> siList = client.getInstances("currency-service");
ServiceInstance si = siList.get(0);
From conventor-service, we can get currency-service instances (of course while currency-service is running).
Run Eureka Server and Clients
First, run eureka-server, then currency-service, then convertor-service.
We will see the eureka-server's link in the log:
Delete /eureka from this URL and you will see the Eureka Server interface. Check the instances here, you can see the links of microservice clients.
To add data to DB, we call currency-service's /testdata endpoint. Click the currency-service's URL, delete /actuator/info endpoint and type /testdata endpoint (Just because we have this endpoint in CurrencyController):
@GetMapping("/testdata")
public void addData() {
currencyRateService.save(
new CurrencyRate(1001L,"usd","try", BigDecimal.valueOf(14.85)));
}
After we call /testdata endpoint, we click the conventor-service's URL, delete /actuator/info endpoint and type /data endpoint and call it. You are supposed to see test data which we added to currency-service's DB with calling /testdata endpoint.
Invalid character problem (for me, underscore)
The character [_] is never valid in a domain name.
java.lang.IllegalArgumentException: The character [_] is never valid in a domain name.
Eureka refers your computer name for hostname of microservice. When I click the microservice's URL in Eureka interface, these error occurs (because my computer name has underscore and it's invalid character):
We know we can use IP adress instead of the hostname. To see IP and other informations about microservice instances, we call /eureka/apps endpoint from Eureka interface.
You can also see IP with execute "ipconfig" and take IPv4 address of the network adapter which you connect the internet with.
After we get our IP address, delete hostname and type the IP and you will not get the error above.
http://atif_pc_074.atif.local:8000/testdata
to
http://192.168.1.19:8000/testdata
I also added a comment line in RestConsumer in convertor-service. If you have invalid character such as underscore like me, please check this comment line to work project properly.
Comments
Post a Comment