Creating Config Server
➤ create a new spring boot application with name lab1-server.
select spring boot starter project dependencies
make packaging as jar
➤
it automatically add dependency management section to identify spring could parent pom .
pom.xml
package com.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Value("${lucky-word}") String luckyWord;
@GetMapping("/lucky-word")
public String showLuckyWord() {
return "The lucky word is: " + luckyWord;
}
}
➤ create a new spring boot application with name lab1-server.
select spring boot starter project dependencies
- under spring cloud select cloud bootstrap
- under spring cloud config select config server
make packaging as jar
➤
it automatically add dependency management section to identify spring could parent pom .
pom.xml
<?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>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.demo</groupId> <artifactId>lab1-server-2</artifactId> <version>0.0.1-SNAPSHOT</version> <name>lab1-server-2</name> <description>spring config server</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Team seems to have forgotten about this dependency, needed to run test: --> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> </dependency> <!-- Allow for automatic restarts when classpath contents change. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </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>
➤in Application class add @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class Lab1Server2Application {
public static void main(String[] args) {
SpringApplication.run(Lab1Server2Application.class, args);
}
}
➤Create a new repository on GitHub to hold your application configuration data. Call the repository "ConfigData" . Note the full URI of the repository, you will need this in a following step.
➤Add a new file to your GitHub repository called "lab-3-client.yml” (or lab-3-client.properties). Add a key called "lucky-word" and a value of "Irish", "Rabbit's Foot", "Serendipity", or any other value of your choosing.
➤Back in your project, create an application.yml (or application.properties) file in the root of your classpath (src/main/resources recommended). Add the key "spring.cloud.config.server.git.uri" and the value "https://github.com/"YOUR-GITHUB-ID"/ConfigData", substituting the value for Github ID and repository name as needed.
➤Also within application.yml (or application.properties), set the “server.port” to 8001.
application.yml
---
spring:
cloud:
config:
server:
git:
uri: https://github.com/cscdev
searchPaths: ConfigData
# "native" is used when the native profile is active, for local tests with a classpath repo:
native:
searchLocations: classpath:offline-repository/
server:
port: 8001
➤Run the application. Open the URL http://localhost:8001/lab-3-client/default/. You should see the JSON result that will actually be used by Spring.
{
"name": "lab-3-client",
"profiles": ["default"],
"label": null,
"version": "74baf0b6c7a8d55c0dc3635cd13772422489451b",
"state": null,
"propertySources": [{
"name": "https://github.com/cscdev/ConfigData/lab-3-client.yml",
"source": {
"lucky-word": "Irish"
}
}
]
}
Part 2 - Configuring config client
➤Create a new, separate Spring Boot application.
select spring boot starter project dependencies
- under spring cloud select cloud bootstrap
- under spring cloud config select config server
pom.xml
<?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>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>lab1-client-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lab1-client-2</name>
<description>spring config server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Allow for automatic restarts when classpath contents change. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</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>
<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>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>lab1-client-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lab1-client-2</name>
<description>spring config server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Allow for automatic restarts when classpath contents change. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</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>
➤Add a bootstrap.yml (or bootstrap.yml) file in the root of your classpath (src/main/resources recommended). Add the following key/values using the appropriate format:
---
spring:
application:
name: lab-3-client
cloud:
config:
uri: http://localhost:8001
server:
port: 8002
spring:
application:
name: lab-3-client
cloud:
config:
uri: http://localhost:8001
server:
port: 8002
this file must be "bootstrap" -- not "application" -- so that it is read early in the application startup process
➤Add a REST controller to obtain and display the key
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Value("${lucky-word}") String luckyWord;
@GetMapping("/lucky-word")
public String showLuckyWord() {
return "The lucky word is: " + luckyWord;
}
}
➤ go to browser click on below link
you will found
O/P
The lucky word is: Irish
No comments:
Post a Comment
Note: only a member of this blog may post a comment.