spring cloud config client 使用

Spring cloud config client uses

client to get configuration parameters from config service, and config service gets configuration parameters from git



<?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
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>


	<groupId>com.midea</groupId>
	<artifactId>base</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>base</name>

	<!-- Set the warehouse, search according to the set order. -->
	<!--<repositories> <repository> <id>public</id> <name>Team Nexus Repository</name>
		<url>http://10.33.183.113:8081/nexus/content/groups/public</url> <snapshots>
		<enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots>
		</repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id>
		<name>Team Nexus Repository</name> <url>http://10.33.183.113:8081/nexus/content/groups/public</url>
		<snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots>
		</pluginRepository> </pluginRepositories> -->

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</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-actuator</artifactId>
		</dependency>
		<!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId>
			</dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId>
			</dependency> -->

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.4.0</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.4.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j2</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
	<build>

	</build>
</project>





server.port=8767
##logging.pattern.level=INFO
#
##Server path
#server.context-path=/eurekaServer

#Service Name
spring.application.name=configClient
spring.cloud.config.label=master
spring.cloud.config.profile=test
#Go to configServicej to get configuration parameters
spring.cloud.config.uri= http://localhost:8888/

#By default, authentication is required, and some pages cannot be accessed. After adding, all pages can be accessed without authentication.
management.security.enabled=false





package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;



@SpringBootApplication //spring boot starts the application
//@RefreshScope //Instruct the Config client to also refresh the injected property value when the server parameters are refreshed,
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}





package com.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

@Api(value = "/Test", description = "测试接口", tags = "TestController")
@RequestMapping("/Test")
@RestController
@RefreshScope //Instruct the Config client to also refresh the injected property value when the server parameters are refreshed,
public class TestController {
	final static Logger logger = LogManager.getLogger(TestController.class);

	//Get the configuration parameters and put them in the variable foo
	@Value("${foo}")
    String foo;
	
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String test() {
		logger.info("test");

		logger.info("foo == " + foo);
		return foo;
	}
}








Dynamic refresh
configuration Send a POST request http://localhost:8767/refresh to the Conf client , and return 200 OK.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326528269&siteId=291194637