Let's create config server to keep all the configurations and get the configurations from it.
Up to now, we have kept all the configurations in the same service. Here we are going to create a separate service to keep only the configurations.
Below are the steps
1. create spring boot application adding "config server" dependency
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency
2. add @EnableConfigServer annotation in the main class
3. create a "config" directory inside resources folder. After that create files with each service name
eg: student.yaml
copy all the configurations from service, here student service
4. Go to respective service (student service) and add the link to configuration server.
you can keep the service name in the respective property files inside each service.
As you shown client service to import details from config server , now student service can take properties from config server
spring:
application:
name: student
config:
import: optional:configserver:http://localhost:8088
cloud:
config:
server:
bootstrap: true
5. Not enough, you need to add the config server client dependency to the student service.
<!--config server client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
6.
Below is the total code samples for Config server
main class
package com.demo.micro.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
Student .yaml file inside "config" directory
server:
port: 8203
spring:
datasource:
url: jdbc:mysql://localhost:3306/student_mgt
username: root
password: root
# for Spring Boot 2
# spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
# for Spring Boot 3
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
show-sql: false
generate-ddl: true
# Hibernate ddl auto (create, create-drop, validate, update)
hibernate:
ddl-auto: update
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#Enable actuator and all
management:
endpoints:
web:
exposure:
include: "*"
application.yaml
server:
port: 8088
spring:
profiles:
active: native
POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo.micro</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2022.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Student Service - application.yaml
spring:
application:
name: student
config:
import: optional:configserver:http://localhost:8088
cloud:
config:
server:
bootstrap: true
No comments:
Post a Comment