前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Spring Cloud Config

Spring Cloud Config

作者头像
netkiller old
发布2018-03-05 18:32:11
发布2018-03-05 18:32:11
1.2K00
代码可运行
举报
文章被收录于专栏:NetkillerNetkiller
运行总次数:0
代码可运行

摘要: 本文节选自《Netkiller Java 手札》 Spring Cloud Config

本文节选自《Netkiller Java 手札》

https://www.netkiller.cn/java/index.html

12.1. Spring Cloud Config

12.1.1. Git 仓库

克隆仓库

代码语言:javascript
代码运行次数:0
运行
复制
git clone https://github.com/netkiller/config.git

创建配置文件 server-development.properties

代码语言:javascript
代码运行次数:0
运行
复制
vim server-development.properties

test.a=KKOOKK
message=Hello world

提交配置文件

代码语言:javascript
代码运行次数:0
运行
复制
git commit -agit push

12.1.2. Server

12.1.2.1. Maven pom.xml
代码语言:javascript
代码运行次数:0
运行
复制
<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>netkiller.cn</groupId>
	<artifactId>cloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Neo</name>
	<description>http://www.netkiller.cn</description>
	<packaging>jar</packaging>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath />
	</parent>

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

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

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

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR5</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>
12.1.2.2. Client

Application

代码语言:javascript
代码运行次数:0
运行
复制
package cn.netkiller.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;@Configuration@EnableAutoConfiguration@EnableDiscoveryClient@EnableConfigServer@SpringBootApplicationpublic class Application {	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}
12.1.2.3. application.properties
代码语言:javascript
代码运行次数:0
运行
复制
server.port=8888spring.cloud.config.server.git.uri=https://github.com/netkiller/config.git
12.1.2.4. 测试服务器
代码语言:javascript
代码运行次数:0
运行
复制
neo@netkiller $ curl http://localhost:8888/server-development.json
{"message":"Hello world","test":{"a":"KKOOKK"}}

12.1.3. Client

12.1.3.1. Maven pom.xml
代码语言:javascript
代码运行次数:0
运行
复制
<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>netkiller.cn</groupId>
	<artifactId>cloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>

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

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

	<dependencies>
		<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.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR5</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>
12.1.3.2. Application
代码语言:javascript
代码运行次数:0
运行
复制
package cn.netkiller.cloud.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@SpringBootApplicationpublic class Application {	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}@RefreshScope@RestControllerclass MessageRestController {	@Value("${message:Hello default}")
	private String message;	@RequestMapping("/message")
	String getMessage() {
		return this.message;
	}
}
12.1.3.3. bootstrap.properties
代码语言:javascript
代码运行次数:0
运行
复制
spring.application.name=server-development
spring.cloud.config.uri=http://localhost:8888management.security.enabled=false
12.1.3.4. 测试 client
代码语言:javascript
代码运行次数:0
运行
复制
neo@netkiller $ curl http://localhost:8080/message.json             
Hello world

Donations (打赏)

We accept PayPal through:

https://www.paypal.me/netkiller

Wechat (微信) / Alipay (支付宝) 打赏:

http://www.netkiller.cn/home/donations.html

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-06-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Netkiller 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本文节选自《Netkiller Java 手札》
  • 12.1. Spring Cloud Config
    • 12.1.1. Git 仓库
    • 12.1.2. Server
      • 12.1.2.1. Maven pom.xml
      • 12.1.2.2. Client
      • 12.1.2.3. application.properties
      • 12.1.2.4. 测试服务器
    • 12.1.3. Client
      • 12.1.3.1. Maven pom.xml
      • 12.1.3.2. Application
      • 12.1.3.3. bootstrap.properties
      • 12.1.3.4. 测试 client
  • Donations (打赏)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档