spring cloud学习入门之config配置教程
前言
本文主要给大家分享了关于spring cloud的入门教程,主要介绍了config配置的相关内容,下面话不多说了,来一起看看看详细的介绍吧。
简介
spring cloud config 分为两部分 server client
- config-server 配置服务端,服务管理配置信息
- config-client 客户端,客户端调用server端暴露接口获取配置信息
config-server
创建config-server
首先创建工程.
文件结构:
├── config-server.iml ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── lkl │ │ └── springcloud │ │ └── config │ │ └── server │ │ └── application.java │ └── resources │ ├── application.properties │ └── bootstrap.properties └── test └── java
pom.xml内容:
<?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> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.2.3.release</version> <relativepath /> <!-- lookup parent from repository --> </parent> <groupid>com.lkl.springcloud</groupid> <artifactid>spring-cloud-config-server</artifactid> <version>1.0-snapshot</version> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-config</artifactid> <version>1.0.4.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-config</artifactid> </dependency> <!--表示为web工程--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</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> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
创建启动类
application.java
package com.lkl.springcloud.config.server; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.cloud.config.server.enableconfigserver; import org.springframework.context.annotation.configuration; import org.springframework.web.bind.annotation.restcontroller; /** * created by liaokailin on 16/4/28. */ @configuration @enableautoconfiguration @restcontroller @enableconfigserver public class application { public static void main(string[] args) { springapplication.run(application.class, args); } }
其中 @enableconfigserver为关键注解
在resources文件下创建application.properties
server.port=8888
配置工程监听端口8888,默认情况下client通过读取http://localhost:8888获取配置信息
创建bootstrap.properties
spring.cloud.config.server.git.uri: https://github.com/liaokailin/config-repo
该配置信息通过fork ()得到
通过spring.cloud.config.server.git.uri
指定配置信息存储的git地址
运行config-server
spring boot工程很方便启动,运行application.java即可
获取git上的资源信息遵循如下规则:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
application:表示应用名称,在client中通过spring.config.name
配置
profile:表示获取指定环境下配置,例如开发环境、测试环境、生产环境 默认值default,实际开发中可以是 dev、test、demo、
production等
label: git标签,默认值master
如果application名称为foo,则可以采用如下方式访问:
http://localhost:8888/foo/default
http://localhost:8888/foo/development
只要是按照上面的规则配置即可访问.
config-client
创建config-client
目录结构如下:
├── pom.xml ├── spring-cloud-config-client.iml └── src ├── main │ ├── java │ │ └── com │ │ └── lkl │ │ └── springcloud │ │ └── config │ │ └── client │ │ └── application.java │ └── resources │ └── bootstrap.yml └── test └── java
pom.xml
<?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.lkl.springcloud</groupid> <artifactid>spring-cloud-config-client</artifactid> <version>1.0-snapshot</version> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.2.3.release</version> <relativepath /> <!-- lookup parent from repository --> </parent> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-parent</artifactid> <version>1.0.1.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> <dependencies> <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> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>http://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginrepositories> <pluginrepository> <id>spring-milestones</id> <name>spring milestones</name> <url>http://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginrepository> </pluginrepositories> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
创建启动类application.java
package com.lkl.springcloud.config.client; 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; /** * created by liaokailin on 16/4/28. */ @springbootapplication @restcontroller public class application { @value("${name:world!}") string bar; @requestmapping("/") string hello() { return "hello " + bar + "!"; } public static void main(string[] args) { springapplication.run(application.class, args); } }
创建bootstrap.properties文件,其内容如下:
spring.application.name: foo spring.cloud.config.env:default spring.cloud.config.label:master spring.cloud.config.uri:http://localhost:8888
其中 spring.application.name
为应用名称,spring.cloud.config.uri
配置config-server暴露的获取配置接口,其默认值为http://localhost:8888
第二三项配置在前面已经提到过,配置的都为默认值,因此bootstrap.properties
只需要配置应用名即可.
运行
访问 http://localhost 得到 `hello liaokailin` 获取到git上的配置信息
访问 http://localhost/env 得到所有的配置信息,可以发现获取配置信息成功.
{ profiles: [ ], configservice:https://github.com/liaokailin/config-repo/foo.properties: { name: "liaokailin", foo: "devoxxfr" }, configservice:https://github.com/liaokailin/config-repo/application.yml: { info.description: "spring cloud samples--lkl", info.url: "https://github.com/spring-cloud-samples", eureka.client.serviceurl.defaultzone: "http://localhost:8761/eureka/" }, commandlineargs: { spring.output.ansi.enabled: "always" }, servletcontextinitparams: { }, ...}
ok ~ it's work ! more about is ()
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
上一篇: 字符集Charset 编码和解码 博客分类: java基础 javacharsetByteBufferCharBuffer
下一篇: Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换 博客分类: java JSP.netBlog
推荐阅读
-
spring cloud学习教程之config修改配置详解
-
spring cloud学习入门之config配置教程
-
Spring学习笔记之RedisTemplate的配置与使用教程
-
Spring学习笔记之RedisTemplate的配置与使用教程
-
spring-cloud入门之spring-cloud-config(配置中心)
-
spring-cloud入门之spring-cloud-config(配置中心)
-
spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config
-
SpringCloud学习笔记(7):使用Spring Cloud Config配置中心
-
Spring Boot(二)之搭建spring cloud config配置中心
-
SpringCloud教程 | 第11篇:分布式配置中心(Spring Cloud Config) 客户端实战