13、config-server 配置中心 本地版

  • 项目地址

  • 配置中心项目结构,并做配置的拆分

    • 拆分如下所示

      • ms-config-client-2244、ms-config-client-3355 ( 微服务模块中的一个服务各个版本的配置文件 )

      • mysql ( mysql 各个版本的配置文件 ,同理可以拆分出 redis, mongodb…等配置)

      • 如果这样拆分 配置中心需要扫描 git 库下的二级目录,否则拉取不到配置(如图2)

      • 在如果 使用的 是 mysql-dev.yml 这样的配置文件,默认的 mysql.yml 也会自动拉取到优先级低于-dev

 

  • 创建 配置中心模块(config-server 端)

    • pom

<dependencies>

    <!-- eureka -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-eureka</artifactId>

    </dependency>

    <!-- config -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-config-server</artifactId>

    </dependency>

    <dependency>

        <groupId>org.eclipse.jgit</groupId>

        <artifactId>org.eclipse.jgit</artifactId>

        <version>4.10.0.201712302008-r</version>

    </dependency>

    <!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->

    <dependency>

        <groupId>com.spcd</groupId>

        <artifactId>ms-api</artifactId>

        <version>1.0-SNAPSHOT</version>

    </dependency>

    <!-- actuator监控信息完善 -->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

 

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-config</artifactId>

    </dependency>

    <!-- 修改后立即生效,热部署 -->

    <!--<dependency>

        <groupId>org.springframework</groupId>

        <artifactId>springloaded</artifactId>

    </dependency>-->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-devtools</artifactId>

    </dependency>

    <!-- hystrix -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-hystrix</artifactId>

    </dependency>

</dependencies>

  •   bootstrap.yml 配置 (git 库如果是私有的 得在 git 下配置 username  password, 此处是共有的库)

server:

  port: 2233

spring:

  application:

    name: ms-config-server-local

  profiles:

    active: native        # 指定使用本地文件

  cloud:

    config:

      server:

        native:

          search-locations: classpath:/ms-config-client-3355/,classpath:/ms-config-client-2244/,classpath:/mysql/  # 扫描本地配置文件

eureka:

  client: #客户端注册进eureka服务列表内

    service-url:

      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

  instance:

    instance-id: ms-config-server-local    # 防止 Eureka web页面报错 ,主机服务名称修改

    prefer-ip-address: true     # 访问路径可以显示IP地址,(左下角IP显示)

  • 启动类 ( @EnableConfigServer )

@EnableEurekaClient

@SpringBootApplication

@EnableConfigServer

public class MsConfigServerApplication3344 {

    public static void main(String[] args) {

        SpringApplication.run(MsConfigServerApplication2233.class, args);

    }

}

  • 创建 config-client 端 

    • pom (spring-cloud-starter-config)

<dependencies>

    <!--config client -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-config</artifactId>

    </dependency>

    <dependency>

        <groupId>com.spcd</groupId>

        <artifactId>ms-api</artifactId>

        <version>1.0-SNAPSHOT</version>

    </dependency>

    <!-- actuator监控信息完善 -->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

    <!-- 将微服务provider侧注册进eureka -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-eureka</artifactId>

    </dependency>

    <dependency>

        <groupId>junit</groupId>

        <artifactId>junit</artifactId>

    </dependency>

    <dependency>

        <groupId>mysql</groupId>

        <artifactId>mysql-connector-java</artifactId>

    </dependency>

    <dependency>

        <groupId>com.alibaba</groupId>

        <artifactId>druid</artifactId>

    </dependency>

    <dependency>

        <groupId>ch.qos.logback</groupId>

        <artifactId>logback-core</artifactId>

    </dependency>

    <dependency>

        <groupId>org.mybatis.spring.boot</groupId>

        <artifactId>mybatis-spring-boot-starter</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-jetty</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-test</artifactId>

    </dependency>

    <!-- 修改后立即生效,热部署 -->

    <!--<dependency>

        <groupId>org.springframework</groupId>

        <artifactId>springloaded</artifactId>

    </dependency>-->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-devtools</artifactId>

    </dependency>

</dependencies>

  • yml (此处得 使用 bootstrap.yml, 它优先于 application.yml 加载,注意黄色和绿色部分)

server:

  port: 3355

spring:

  application:

    name: ms-config-client-3355           # 项目名称

  profiles:

    active: dev                           # 启动的环境

  cloud:

    config:

      fail-fast: true

      name: ms-config-client-3355, mysql  # 要加载的配置文件所在的路径(如果没有二级文件则是要加载配置文件)

      profile: ${spring.profiles.active}  # 要加载的版本 对应 spring.cloud.config.name 的版本

      label: master                       # 对应的 git 的环境

#      uri: http://localhost:3344/        # 配置中心url(与discovery 2选1)

      discovery:                          # 配置拉取的服务(从eureka中发现)

        enabled: true

        service-id: ms-config-server-local #必须是配置项目的 eureka ID

eureka:

  client: #客户端注册进eureka服务列表内

    service-url:

      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

  instance:

    instance-id: ms-config-client-3355    # 防止 Eureka web页面报错 ,主机服务名称修改

    prefer-ip-address: true     # 访问路径可以显示IP地址,(左下角IP显示)

 

  • 启动类(不用做特殊处理)

@EnableEurekaClient

@SpringBootApplication

public class MsConfigClientApplication3355 {

 

    public static void main(String[] args) {

        SpringApplication.run(MsConfigClientApplication3355.class, args);

    }

}

  • 配置文件( 此处得 使用 bootstrap.yml, 它优先于 application.yml 加载 )

    • 如果不使用 bootstrap.yml ,项目启动默认会从 http://localhost:8888/ 拉取配置,从而发生异常
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值