Spring Cloud Config

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

本文节选自《Netkiller Java 手札》

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

12.1. Spring Cloud Config

12.1.1. Git 仓库

克隆仓库

git clone https://github.com/netkiller/config.git

创建配置文件 server-development.properties

vim server-development.properties

test.a=KKOOKK
message=Hello world

提交配置文件

git commit -agit push

12.1.2. Server

12.1.2.1. Maven pom.xml

<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

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

server.port=8888spring.cloud.config.server.git.uri=https://github.com/netkiller/config.git

12.1.2.4. 测试服务器

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

<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

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

spring.application.name=server-development
spring.cloud.config.uri=http://localhost:8888management.security.enabled=false

12.1.3.4. 测试 client

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

原文发布于微信公众号 - Netkiller(netkiller-ebook)

原文发表时间:2017-06-16

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Netkiller

Phalcon VS Spring 用法对照手册

Phalcon VS Spring 摘要 Phalcon VS Spring 用法对照表 ---- 目录 1. Install 1.1. Phalcon 1.2...

3356
来自专栏用户画像

springmvc 最简化配置

一、新建maven webapp项目 http://blog.csdn.net/jxq0816/article/details/76063973

432
来自专栏吴伟祥

Spring Boot 之Servlet、Listener、Filter

693
来自专栏积累沉淀

Hive2.0.0操作HBase 1.2.1报错解决

首先看错  org.apache.hive.service.cli.HiveSQLException: Failed to open new session: ...

1859
来自专栏吴伟祥

Spring4 @Configuration源码

442
来自专栏海说

Java应用中常见的JDBC连接字符串(SQLite、MySQL、Oracle、Sybase、SQLServer、DB2)

Java应用中常见的JDBC连接字符串 Java应用中连接数据库是不可或缺的,于是便整理一些可能用到的JDBC的jar包及其相匹配的URL,以备日后查阅。 1)...

2280
来自专栏闻道于事

商城项目回顾整理(二)easyUi数据表格使用

后台主页: 商品的数据表格展示 引入用户表数据表格展示 引入日志表数据表格展示 引入订单表数据表格展示 后台主页代码: 1 <%@ page languag...

3305
来自专栏闻道于事

JavaWeb(三)servlet

Servlet * 什么是Servlet 是运行在web服务器端的Java应用程序,它使用JAVA语言编写,具有Java语言的优点。与Java程序的区别:Ser...

3539
来自专栏一个会写诗的程序员的博客

《Springboot极简教程》使用@SpringBootApplication annotation

许多Spring Boot开发人员总是使用@Configuration,@EnableAutoConfiguration和@ComponentScan来标注它们...

693
来自专栏java初学

j2ee—框架(2):Servlet+JSP实现基本的登录功能(v2.0)

33613

扫描关注云+社区