关于config:SpringCloudConfig

46次阅读

共计 3825 个字符,预计需要花费 10 分钟才能阅读完成。

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. 启动服务器,查看服务器是否已胜利拉起配置文件。

正文完
 0