Hi everyone! In this article we will create the simple Spring Cloud Project ready to start and work!
Day by day, often and often you can hear about microservices architecture. Everybody wants to create services that are independent from each other. And failure of the single service doesn’t lead to the whole application failure. Of course, microservices shouldn’t be used everywhere. To use them or not should be decided based on your application structure, needs and purpose. So here we will take a quick look onhow to create microservices in Java, and run them as separate servers.
Configuration Server
This first thing we will need to create is the Configuration Server. From it’s name you’ll see that this server is needed for configuration purposes. The configuration for our microservices will be located remotely, on the GitHub.
The Configuration Server is a simple Spring Boot application with Spring Cloud dependency.
- The parent file we use simple spring-boot-starter-parent
- For making server to Configuration one -> we just add one single Spring Cloud dependency — spring-cloud-config-server
- In the dependency managment section you have to specify the Spring Cloud version you are using in the project
After that in the main class of the application, in our case it is ConfigServerApplication, we actually need to tell the application that it is Configuration server. To make it real we just add annotation @EnableConfigServer to main class.
With this annotation our application will understand it’s purpose and will know what we expect from it.
And the last step for creating Configuration Server(yes! the last one!) is to add properties file with configuration of this application. You can use either .properties or .yml file.There is no difference at all. Just keep in mind, that Spring gives preference to .yml files. So if you have two configuration files called application.properties and application.yml -> Spring will choose the .yml configuration.
spring: cloud: config: server: git: uri: https://github.com/igorkosandyak/cloud-configuration server: port: 9999
Here is how the application.yml looks like. We say our Configuration server to go to https://github.com/igorkosandyak/cloud-configuration and pull configuration files for our future services. And we want the server to be running on the 9999 port. And that’s it! Now we are ready to start the Configuration Server!
So, we started the Configuration Server and see that it is succesfully running on the 9999 port and waiting for our future services to connect it for pulling their configurations.
Ok, awesome, let’s move forward!
Service Discovery
Next thing we will speak about is Service Discovery — that is easily made by Netflix Eureka. What do we need it for and what it does?
- We need our microservice to know about other services running and available for requesting;
- When the service starts up -> it registers itself with the Eureka server, so everybody else know about new service running;
- The Eureka server also provides that newly registered service with the list of all other services running;
- So, the server discovers other services and has itself been discovered by other services;
We need to create new Spring Boot application -> Let’s move to the pom.xml file and add all needed dependencies:
- Here we have have to specify new dependency called — spring-cloud-starter-eureka-server;
- In the dependency management section we also have to specify the version of the Spring Cloud we are using for this project;
Once the pom.xml is set up we have to add an entry point for our application, so let’s create new java class called EurekaServerApplication, annotate it with @ SpringBootApplication and @ EnableEurekaServer annotations:
After that’s done, let’s add some configurations for our Eureka Server:
Let’s add one bootstrap.yml file. It is important to call configuration files for microservices “bootstrap.yml”. Note this. In this file we will add our configurations for Eureka Server:
server: port: 2000 eureka: client: registerWithEureka: false fetchRegistry: false
So what this lines mean:
- We want Eureka to be running on the 2000 port;
- registerWithEureka and fetchRegistry set to false will tell Eureka server not to look for another Eureka servers and not to try to connect to them(recommended for development environment, not production. In the production mode is recommended to have couple Eureka servers — the optimal quantity is 3);
And that’s pretty much enough to run our Eureka Server!
So, now we can open browser, go to https://localhost:2000 and will see the UI of Eureka Server:
Ok! Great, Eureka Server is running and is waiting for new microservice instances. So let’s create them!
The next server we are going to create — is User Service. This would be the service that will have some logic and will be functional service. In our case it will just send us information — “Hi, this is message from user-service”.
So, we need to create another Spring Boot application. We have to set up pom.xml file, Main class and configuration file.
Here how the pom.xml looks like:
- The spring-cloud-starter-config dependency we need for our server to be able to commicate with Configuration Server;
- The spring-cloud-starter-eureka dependency is required for our server to be able to register itself with Eureka Server and provide or own information to Eureka;
- And the spring-boot-starter-web we need just to be able use Rest Controller and Request Mappings;
Next thing we are going to do is to set up Main class of the application. Let’s call it UserServerApplication:
@SpringBootApplication @EnableDiscoveryClient public class UserServerApplication { public static void main(String[] args) { SpringApplication.run(UserServerApplication.class, args); } }
As you see, we have one new annotation here— EnableDiscoveryClient. This annotation will teach our UserServerApplication how to communicate with Eureka Server, how to register itself as a client and how to provide information about own host, port and other metadata to Eureka;
Let’s also add bootstrap.yml file with configuration information for our client:
spring: application: name: user-server cloud: config: uri: https://localhost:9999 server: port: 7001
Yes! This is something new!
- spring.application.name=user-server — here is the place where we give the name(id) of our service;
- spring.cloud.config.uri=https://localhost:9999 — here we tell where to take the configurations for current service. So we tell the app to go to our Configuaration Server which is running on the https://localhost:9999, tell him our name(user-server) and ask to provide the configuration file for us. The configuration file for UserServerApplication we will put on the remote repository on GitHub;
- server.port=7001 — we just say that server has to be started on the 7001 port;
So, it’s almost done, except one thing. We have to provide our configuration file, and put it to the repo. Let’s do it now.
- First, let’s create an empty repo on the GitHub;
- Then clone it somewhere to your local machine;
- In this folder, create new file called user-server.yml with next content:
server: port: 8001 eureka: client: serviceUrl: defaultZone: https://localhost:2000/eureka
So, the server should be running on port 8001, and we also need to specify the location of our Eureka Server, so the UserServerApplication will know where to find it;
5. Open command line, move to the directory where you just cloned your repository and create the file and run next commands:
6. git add user-server.yml;
7. git commit -m “your commit message here”;
8. git push;
If everything is fine, the user-server.yml should appear in your remote repository. And than let’s just run the UserServerApplication.
- As you see, server is started on the 8001 port, not on the 7001 as specified in the local configuration file. That’s because in the user-server.yml, that is located in the remote repo we told the server to be running on the 8001 port. And it is running!
- When you go to the Eureka Server UI, you’ll notice that under “Instances currently registered with Eureka” our User-Server application appeared. And Eureka knows that this server is running on the https://localhost:8001.
Ok, let’s quickly add REST endpoint and see if it works.
@RestController @RequestMapping(value = "/user") public class UserController { @GetMapping("/message") public String sendMessage(){ return "Hi, this is message from user-service"; } }
So, let’s go to the browser, and open url https://localhost:8001/user/message. And here we’ve got:
Conclusion
Ok, now you have your first microservice written in Java with the help of Spring Cloud. It is working and ready to use. You can expand and scale it as much you want. For now it is just simple example of how easy to start the Spring Cloud Microservices, but in the real project you will definitely need microservices that would be able to communicate between each other, and you would definitely want to make some kind of gateway, endpoint to be like the entry point of your whole app. So you would use different microservices by calling single host and port, and this gateway will redirect requests to a particular microservice that is responsible for handling this or that kind of request. About this and more we will talk in the next topics! Don’t miss them!