[JAVA] Try running Spring Cloud Config for the time being

Purpose

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;
	}

}

Check the result

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

Try running Spring Cloud Config for the time being
Command to try using Docker for the time being
Spring Boot for the first time
Spring Cloud Config Embedding the Config Server
Spring AOP for the first time
Install Amazon Corretto (preview) for the time being
Use Java external library for the time being
Run Dataflow, Java, streaming for the time being
Try the Spring WebFlux tutorial
Hello World with Ruby extension library for the time being
Java14 came out, so I tried record for the time being
Try running Spring Boot on Kubernetes
I'm sorry to install JDK9 on my PC, but I'd like to try JShell for the time being.
Let's experience the authorization code grant flow with Spring Security OAuth-Part 2: Creating an app for the time being
Java12 came out, so I tried the switch expression for the time being
For the time being, run the war file delivered in Java with Docker
Glassfish tuning list that I want to keep for the time being
[First Java] Make something that works with Intellij for the time being
Introduction to java for the first time # 2
Support Protocol Buffers for Spring Cloud Stream
Learning for the first time java [Introduction]
Use the l method for time notation
The training for newcomers was "Make an app!", So I made an app for the time being.
I want you to use Scala as Better Java for the time being
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
Check the options set for the running Java process
Try calling the CORBA service from Spring (Java)
Walls hit by Rspec for the first time
Android Studio development for the first time (for beginners)
I tried touching Docker for the first time
Create Spring Cloud Config Server with security with Spring Boot 2.0
A memo to do for the time being when building CentOS 6 series with VirtualBox
[Rails] N + 1 is evil! If it does occur, resolve it for the time being! !! Is dangerous