config概述
配置核心提供了一个中心化的内部配置,默认应用git存储配置信息,这样就能够对配置信息进行版本治理,下边配置核心的搭建就是以git为根底进行的。配置核心是独自作为一个服务运行的。
spring cloud 容许运行时动静刷新配置,能够从新从配置核心获取新的配置信息。
bootstrap.yml疏导配置文件,先于 application.yml 加载。
config实现
第一步:新建java我的项目config
当作一个文件夹,用来寄存配置文件。
第二步:赋值sp02、03、04、11的配置文件至config,并重命名
spring的profile文件阐明:
#item-service.yml - 主配置
#item-service-dev.yml - 开发 开发时启动,主+开发合并,同时启动。
#item-service-test.yml - 测试
#item-service-dev.prod - 生产
#item-service-xxx.yml -
第三步:config中的4个配置文件均增加配置并正文sp02、sp03、sp04、sp11的配置文件
#增加配置,当近程仓库的配置与本地仓库的配置抵触时,以本地仓库的配置为主
cloud:config:override-none: true
别离创立配置文件item-service-dev.yml:
# item-service-dev.yml
#给利用起个名,向注册核心注册时用这个名称注册
#注册信息:item-service ----->localhost:
spring:
application:
name: item-service
#设置禁止配置核心的配置将客户端配置笼罩掉
cloud:
config:
override-none: true
server:
port: 8001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
正文sp02、sp03、sp04、sp11的配置文件:
## application.yml
#
##给利用起个名,向注册核心注册时用这个名称注册
##注册信息:item-service ----->localhost:
#spring:
# application:
# name: item-service
#
#server:
# port: 8001
#
#eureka:
# client:
# service-url:
# defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
第四步:创立本地仓库
抉择本地仓库目录和文件
第五步:把本地仓库提交推送到gitee近程仓库
1.点击commit
2.勾选要提交的文件,填写提交信息,进行提交
点击commit,提交文件至本地仓库
3.gitee上创立仓库springcloud1
复制仓库地址
4.点击push,开始从本地仓库推送至近程仓库。
填写链接点击ok
push推送
查看是否推送胜利
第六步:创立springboot我的项目sp12-config
第七步:增加config server、eureka依赖
<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>
第八步:配置pom文件
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/duchaosa/springcloud1
searchPaths: config
#username: your-username
#password: your-password
server:
port: 6001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
第九步:主程序增加注解@EnableConfigServer 和 @EnableDiscoveryClient
package cn.tedu.sp12;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class Sp12ComfigApplication {
public static void main(String[] args) {
SpringApplication.run(Sp12ComfigApplication.class, args);
}
}
第十步:启动服务器sp12,拜访测试
http://localhost:6001/item-service-dev.yml
http://localhost:6001/item-service/dev
第十一步:配置核心客户端
1.在sp02、sp03、sp04、sp11我的项目的pom
文件中别离增加config依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.在sp02、sp03、sp04、sp11我的项目中别离创立bootstrap.yml文件
从eureka注册核心拉取config服务器,并获取客户端配置文件。
疏导配置文件,先于 application.yml 加载。
item-service:
# bootstrap.yml
# 获取地址表,要从注册表获取配置核心得地址
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server #配置核心的服务id
# item-service-dev.yml
name: item-service
profile: dev
user-service:
# bootstrap.yml
# 获取地址表,要从注册表获取配置核心得地址
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server #配置核心的服务id
# user-service-dev.yml
name: user-service
profile: dev
order-service:
# bootstrap.yml
# 获取地址表,要从注册表获取配置核心得地址
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server #配置核心的服务id
# order-service-dev.yml
name: order-service
profile: dev
zuul-service:
# bootstrap.yml
# 获取地址表,要从注册表获取配置核心得地址
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server #配置核心的服务id
# zuul-dev.yml
name: zuul
profile: dev
3.启动服务器,查看服务器是否已胜利拉起配置文件。
发表回复