这篇文章将为大家详细讲解有关spring-cloud中如何创建配置项目,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创建配置项目
在github中创建一个项目,专门用来保存我们所有项目的配置文件,项目是我的项目结构
配置项目地址:https://github.com/bigbeef/cppba-config
eureka-server.properties
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
spring.application.name=eureka-server
server.port=18761
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer1:18761/eureka/
创建spring-cloud-config-server项目
项目结构如图:
pom.xml核心代码
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
SpringCloudConfigServerApplication.java
package com.cppba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
application.properties
这个根据自己实际的git项目修改配置
server.port=8888
spring.application.name=config-server
spring.cloud.config.server.git.uri=https://github.com/bigbeef/cppba-config
spring.cloud.config.label=master
# spring.cloud.config.server.git.username=
# spring.cloud.config.server.git.password=
spring.cloud.config.server.git.searchPaths=\
cppba-spring-cloud/*,\
cppba-spring-cloud/eureka-client/*
spring.cloud.config.server.git.uri:配置git仓库地址
spring.cloud.config.server.git.searchPaths:配置仓库路径,以逗号隔开
spring.cloud.config.label:配置仓库的分支
spring.cloud.config.server.git.username:访问git仓库的用户名
spring.cloud.config.server.git.password:访问git仓库的用户密码
启动项目
访问地址:http://127.0.0.1:8888
http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
根据我们自己的配置,我们可以这样访问:http://127.0.0.1:8888/eureka-server/default/master
application -> eureka-server (应用名)
profile -> default (启用的配置,通常是后缀,下面解释)
label -> master (分支)
访问到的结果就是:
profile比较重要,可以理解成读取哪些配置文件,假如我不止一个配置文件,可能会有:
eureka-server.properties(这个是通用配置文件,默认都会加载),
eureka-server-mysql.properties,
eureka-server-oracle.properties,
eureka-server-jpa.properties,
eureka-server-mysql.properties......
我们可能会选择性的加载其中的部分properties配置文件,那我们可以这样写:http://127.0.0.1:8888/eureka-server/default,mysql,jpa/master
到此,我们的spring-cloud-config-server就简单搭起来
关于“spring-cloud中如何创建配置项目”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。