序言
Spring Cloud Config为分布式系统中的外部配置,提供了服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器映射的概念与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可用于管理内容的各种工具。可以轻松添加替代实现,并使用Spring配置将其插入 。
此文章仅限入门 SpringCloud版本为 Greenwich
不过现在有阿里的Nacos 和 携程的 Apollo 替代方案有很多。
使用
我们先创建一个配置中心服务端
首先加入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
然后是启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
在然后是yml
spring:
application:
name: spring-cloud-action-config
profiles:
active:
- native
cloud:
config:
name: config-server
enabled: false
server:
port: 8888
eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
hostname: localhost
client:
service-url:
#defaultZone 千万别写成 default-zone
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
这里 首先说一下这个 - native
这个意思表明 我们使用的是本地配置中心,所以我们在该项目的resources
下创建的配置文件就可以让 config client 使用。
我们就在下面创建一个spring-cloud-action-server-order.yml
的配置文件
fulinlin:
name: my name is localfulinlin
启动下访问 http://localhost:8888/spring-cloud-action-server-order.yml
看看网页是否显示了yml的内容,如果显示了则表示没问题。
然后怎么使用呢?
在使用config 的配置的依赖中加入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
然后在resources
目录下创建一个 bootstrap.yml
,这里需要注意一下的是,Spring Cloud Config是不认 application.yml
的配置,这也是Cloud官方有说明的,不然配置的 uri属性是无效的
spring:
application:
name: spring-cloud-action-server-order
cloud:
config:
# profile: dev
uri: http://localhost:8888
name: spring-cloud-action-server-order
这里的 name
和 profile
是做多环境使用的 比如我的配置文件叫做 spring-cloud-action-server-order-dev.yml
那么就应该在profile 里写上 dev。
然后在使用 config 的服务里写个方法测试下呗
@Value("${fulinlin.name}")
String fulinlin;
@GetMapping("/getConfig")
public String getOrder() {
return fulinlin;
}
查看控制台是否打印 my name is localfulinlin
集成git
yml里稍做修改即可
spring:
application:
name: spring-cloud-action-config
cloud:
config:
label: master
server:
git:
uri: https://gitlab.com/fulinlin/spring-cloud-action-gitlab-config.git
search-paths: respo
username:
password:
这个search-paths
就是仓库的目录 respo
就是仓库目录下的 文件夹
然后在使用config的时候
spring:
application:
name: spring-cloud-action-server-order
cloud:
config:
name: spring-cloud-action-server-order
# profile: dev
label: master
uri: http://localhost:8888
这样配置即可。
附:HTTP 请求地址和资源文件映射
http://ip:port/{application}/{profile}[/{label}]
http://ip:port/{application}-{profile}.yml
http://ip:port/{label}/{application}-{profile}.yml
http://ip:port/{application}-{profile}.properties
http://ip:port/{label}/{application}-{profile}.properties
此文章仅限入门,切记啊不会用找不到多去官网看文档!