Spring Cloud Learning: Creation of config client project

I. Introduction

We learned how to create a configuration center service project and use url to request the return result. Now we learn how to build a config client project and get the content information of the specified file on github.

Create config-client project

1. Dependency Introduction

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-client</artifactId>
</dependency>

Complete dependence

<properties>
		<java.version>1.8</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
	</parent>
	<!-- Management dependence -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot integration Web assembly -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>
	</dependencies>
	<!-- Note: This must be added, or there are problems with all kinds of dependencies. -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

2. Edit configuration file application.properties

#If the eureka registry is configured, this is the alias of the registry.
spring.application.name=config-client
#Specify the branch of remote warehouse
spring.cloud.config.label=master
#dev development environment test test environment pro formal environment 
spring.cloud.config.profile=dev
#Configure Service Center Address
spring.cloud.config.uri= http://localhost:8888/
server.port=8881

3. Writing Request Mapping

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Application {

	public static void main(String[] args) {
		try {
			SpringApplication.run(Application.class, args);
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	@Value("${foo}")
	private String foo;
	
	@Value("${democonfigclient.message}")
	private String msg;
	
	//Create a message that does not exist in the file
	@Value("${ss}")
	private String ss;
	
	@RequestMapping(value = "/hi")
	public String hi(){
		return foo+" "+msg;
	}
	
	@RequestMapping("/ss")
	public String ss(){
		return ss;
	}
}

Three, test

1. Start the configuration directly, observe the startup log and find out

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'ss' in value "${ss}"

Describes that if the specified file key information does not exist in the specified config-service project, the client startup will report an error

2. Remove ss configuration and restart

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Application {

	public static void main(String[] args) {
		try {
			SpringApplication.run(Application.class, args);
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	@Value("${foo}")
	private String foo;
	
	@Value("${democonfigclient.message}")
	private String msg;
	
	//Create a message that does not exist in the file
//	@Value("${ss}")
//	private String ss;
	
	@RequestMapping(value = "/hi")
	public String hi(){
		return foo+" "+msg;
	}
	
//	@RequestMapping("/ss")
//	public String ss(){
//		return ss;
//	}
}

Visit http://localhost:8881/hi

 

Tags: Spring Java github SpringBoot

Posted on Sun, 06 Oct 2019 09:24:59 -0700 by awared