While referring to Spring Cloud Config Manual Simply build ConfigServer and ConfigClient, Check if the configuration file placed in ConfigServer is reflected in Client.
Config Server
Added spring-cloud-config-server
build.gradle
dependencies {
implementation('org.springframework.cloud:spring-cloud-config-server')
}
Added @EnableConfigServer
to SpringBoot boot class
ConfigServerApplication.java
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
Set the git repository where the Config file is located to application.yml. Also, Springboot defaults to port 8080, so Set the port to 8880 so as not to collide with the client described later.
application.yml
server:
port: 8880
spring:
cloud:
config:
server:
git:
#The git repository URI where the configuration file resides
uri: https://github.com/spring-cloud-samples/config-repo.git
After setting up to this point, start up. Once started, it will be accessible on the following endpoints.
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
ConfigClient gets the configuration file of the result of replacing {} with the value of Client from ConfigServer.
{application}: Value of spring.application.name
(default is" application ")
{profile}: Value of spring.profiles.active
{label}: git branch, tag name, or commitID (default is "master")
If you try to make a request to http: // localhost: 8880 / foo / development /
The following response was returned
{
"name": "foo",
"profiles": [
"development"
],
"label": null,
"version": "a611374438e75aa1b9808908c57833480944e1a8",
"state": null,
"propertySources": [
{
"name": "https://github.com/spring-cloud-samples/config-repo.git/foo-development.properties",
"source": {
"bar": "spam",
"foo": "from foo development"
}
},
{
"name": "https://github.com/spring-cloud-samples/config-repo.git/foo.properties",
"source": {
"foo": "from foo props",
"democonfigclient.message": "hello spring io"
}
},
{
"name": "https://github.com/spring-cloud-samples/config-repo.git/application.yml (document #0)",
"source": {
"info.description": "Spring Cloud Samples",
"info.url": "https://github.com/spring-cloud-samples",
"eureka.client.serviceUrl.defaultZone": "http://localhost:8761/eureka/",
"foo": "baz"
}
}
]
}
If there is authentication in the git repository, it seems that you should set application.yml referring to this area Authentication
Config Client
Added spring-cloud-starter-config
build.gradle
dependencies {
implementation('org.springframework.cloud:spring-cloud-starter-config')
}
Create a new bootstrap.yml
SpringCloudConfigClient loads bootstrap.yml instead of application.yml and
Access to that spring.cloud.config.uri
(http: // localhost: 8888 if not specified)
bootstrap.yml
spring:
application:
name: foo
profiles:
active: development
cloud:
config:
#ConfigServer URI
uri: http://localhost:8880
#Set to true if you want to raise an Exception when you cannot connect to ConfigServer(default is false)
fail-fast: true
Added an endpoint that can check the setting value to the startup class
ConfigClientApplication.java
@SpringBootApplication
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@GetMapping("/bar")
public String bar(@Value("${bar: notDefined}") String bar) {
return bar;
}
@GetMapping("/foo")
public String foo(@Value("${foo: notDefined}") String foo) {
return foo;
}
}
Since the application name of Client is foo and profile is development, the following configuration file is the expected value. https://github.com/spring-cloud-samples/config-repo/blob/master/foo-development.properties
bar: spam
foo: from foo development
When you start Client and access localhost: 8080 / bar, "spam" When I accessed localhost: 8080 / foo, "from foo development" was displayed.
Recommended Posts