Want to create a distributed system in Java. You have come to the right place. We can create that through Spring cloud. Spring Cloud is a framework for building robust cloud applications.
Overview
In this blog we will make the application that will make the following
discovery-service
config server
config client
We are going to make the system in which we have the functionality of service discovery each service will register itself. Second, Config server which will be the central repository of configuration for all the services. The third is the config client which picks the configuration from the config client.
Steps to create service discovery.
Do make the service discovery we have to do the following things.
we need add the following dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server
</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Add the following property in application yaml of discovery server.
spring:
application:
name: discovery-server
server:
port: 8761 # default port of discovery server
eureka:
instance:
hostname: localhost
client:
register-with-eurka: false
fetch: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Meaning and usage of some property/annotation
eureka.client.server-url.defaultZone represents the URL for the discovery server. Config server will use this URL to register itself so other services or end-user can access it.
We need to add the @EnableEurekaServer in main class of application. Your discovery server is ready.
Steps to create Config server.
Now we will make the config server. This service will provide the configuration for the other application. We will register the this config server in service register so that other service can access it.
We have to provide following dependency for config server.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
We can provide the following properties in application yaml
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/ThakurPriyanka/config-repository.gits
pring.application.name=config-server
eureka.client.server-url.defaultZone=http://localhost:8761/eureka
Meaning and usage of some property/annotation
The spring.cloud.config.server.git.uri represents the git Url for the repo that contains the configuration for the services. You have to fork the repo and provide the URL of the forked repo to this property.
In the config service adds the two annotations in the main class.
@EnableConfigServer
@EnableDiscoveryClient
The first annotation, EnableConfigServer will make the Application to act as Config Server. By the second annotation, EnableDiscoveryClient we are enabling our application to be a client so that it can register itself to service register.
Steps to create the Config Client
Now we will make the config client that will pick the configuration from config server and display it to end user.
we will add the following dependency.
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
we will create the file bootstrap.property in config client application with the following property.
spring.application.name=config-client-app
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
server.port=8085
eureka.client.server-url.defaultZone=http://localhost:8761/eureka
Meaning and usage of some property/annotation
The spring.cloud.config.discovery.enabled property will enable the client to have the configuration from the config server. The @ConfigurationProperties(prefix = <propertyName>) we will add this annotation to the class where we have to pick the configuration from the config server. The propertyName represents the property name from the application YAML file whose value we need to pick. In that class, we can define the getter and setter for the configuration field.
@EnableConfigurationProperties(ConfigClientApplication.class) is been added to the main class. ConfigClientApplication is the class that is picking the configuration from the server. This annotation will tell the application which class is responsable for configurations.
Source: Medium
The Tech Platform
Comments