全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

spring cloud学习入门之config配置教程

前言

本文主要给大家分享了关于spring cloud的入门教程,主要介绍了config配置的相关内容,下面话不多说了,来一起看看看详细的介绍吧。

简介

Spring cloud config 分为两部分 server client

  • config-server 配置服务端,服务管理配置信息
  • config-client 客户端,客户端调用server端暴露接口获取配置信息

config-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 https://github.com/spring-cloud-samples/config-repo (本地下载)得到

通过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只需要配置应用名即可.

运行config-client

访问 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 here(本地下载)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。


# spring  # cloud  # config  # sping  # 配置  # springcloud config配置读取优先级过程详解  # 详解SpringCloud Config配置中心  # Spring Cloud Config Client超时及重试示例详解  # 利用Spring Cloud Config结合Bus实现分布式配置中心的步骤  # SpringCloud之分布式配置中心Spring Cloud Config高可用配置实例代码  # 详解spring cloud config整合gitlab搭建分布式的配置中心  # 详解spring cloud config实现datasource的热部署  # 简单了解Spring Cloud搭建Config过程实例  # 本地下载  # 默认值  # 客户端  # 相关内容  # 说了  # 不多  # 给大家  # 服务管理  # 只需要  # 这篇文章  # 谢谢大家  # 则可  # 在前面  # 三项  # 都为  # 很方便  # 两部分  # 值为  # 服务端  # 情况下 


相关文章: 小程序网站制作需要准备什么资料,如何制作小程序?  关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)  高防网站服务器:DDoS防御与BGP线路的AI智能防护方案  如何注册花生壳免费域名并搭建个人网站?  深圳网站制作案例,网页的相关名词有哪些?  外汇网站制作流程,如何在工商银行网站上做外汇买卖?  建站主机类型有哪些?如何正确选型  C++中的Pimpl idiom是什么,有什么好处?(隐藏实现)  大连网站制作公司哪家好一点,大连买房网站哪个好?  邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?  大连网站设计制作招聘信息,大连投诉网站有哪些?  如何在阿里云域名上完成建站全流程?  制作网页的网站有哪些,电脑上怎么做网页?  电脑免费海报制作网站推荐,招聘海报哪个网站多?  宝塔新建站点为何无法访问?如何排查?  南京网站制作费用,南京远驱官方网站?  如何在Tomcat中配置并部署网站项目?  太平洋网站制作公司,网络用语太平洋是什么意思?  如何设置并定期更换建站之星安全管理员密码?  制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?  如何通过多用户协作模板快速搭建高效企业网站?  如何用好域名打造高点击率的自主建站?  道歉网站制作流程,世纪佳缘致歉小吴事件,相亲网站身份信息伪造该如何稽查?  常州企业建站如何选择最佳模板?  单页制作网站有哪些,朋友给我发了一个单页网站,我应该怎么修改才能把他变成自己的呢,请求高手指点迷津?  新网站制作渠道有哪些,跪求一个无线渠道比较强的小说网站,我要发表小说?  电商网站制作价格怎么算,网上拍卖流程以及规则?  怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?  齐河建站公司:营销型网站建设与SEO优化双核驱动策略  无锡制作网站公司有哪些,无锡优八网络科技有限公司介绍?  c++23 std::expected怎么用 c++优雅处理函数错误返回【详解】  如何在Windows 2008云服务器安全搭建网站?  专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?  ,有什么在线背英语单词效率比较高的网站?  如何在新浪SAE免费搭建个人博客?  如何用PHP快速搭建高效网站?分步指南  如何使用Golang安装API文档生成工具_快速生成接口文档  网站制作话术技巧,网站推广做的好怎么话术?  如何解决VPS建站LNMP环境配置常见问题?  Python如何创建带属性的XML节点  网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?  建站主机如何选?高性价比方案全解析  制作销售网站教学视频,销售网站有哪些?  电商网站制作公司有哪些,1688网是什么意思?  如何配置支付宝与微信支付功能?  青岛网站建设如何选择本地服务器?  建站之星备案是否影响网站上线时间?  网站制作费用多少钱,一个网站的运营,需要哪些费用?  mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?  专业网站制作服务公司,有哪些网站可以免费发布招聘信息? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。