spring cloud config 高可用配置

来源:互联网 发布:防伪码生成软件 编辑:程序博客网 时间:2024/05/16 09:56

spring cloud config

spring cloud 的配置管理中心,用来统一管理各个微服务的配置文件的。分为服务端和客户端 ,下面直接上代码为大家建立一个config-server config-client

创建项目 bw-config-server

1.bootstrap.yml

server:  port: 8084eureka: client:  service-url:   defaultZone: http://localhost:1111/eureka/ #自己创建好的eureka-serverspring:  application:    name: config-server  cloud:    config:      server:        git:          uri: https://gitee.com/*****/config.git #git仓库的地址          searchPaths: bw*  #搜索的文件夹名字  注意区分 search-paths          username: username          password: password

2 pom.xml

<dependency>            <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency>          <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>

3启动类

@SpringBootApplication@EnableConfigServer@EnableDiscoveryClientpublic class BwConfigServerApplication {    public static void main(String[] args) {        SpringApplication.run(BwConfigServerApplication.class, args);    }}

这样一个config-server 创建完成,在创建之前要在git上面创建一个项目,config/ bw-sms文件夹下 bw-sms-dev.properties

创建一个bw-sms

跟普通的springBoot项目基本一样
1.bootstrop.yml

server:  port: 8083eureka:  client:    service-url:      defaultZone: http://localhost:1111/eureka/spring:  application:    name: bw-sms  cloud:    config:      discovery:              enabled: true #作为config client 去发现服务        service-id: config-server      profile: dev      label: master

2.pom.xml

<!-- eureka客户端依赖 -->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka</artifactId>        </dependency>        <!-- config客户端依赖 -->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-config</artifactId>        </dependency>

3.启动类
@SpringBootApplication
@EnableDiscoveryClient
public class BwSmsApplication {

public static void main(String[] args) {    SpringApplication.run(BwSmsApplication.class, args);}

}

这样就完成了一个简单的客户端
测试的话自己随便写一个 测试一下就可以
@value(“${value}”)
private String value;`
总结一下我在做这个的时候遇到的坑;
1.配置文件的名字 要用bootstrap.yml 不能用application.yml或者application.properties,原因bootstrap的加载顺序先于application,所以要在application加载之前 指定application的位置;
2.在配置 配置文件搜索路径的时候注意区分
searchPaths: bw* #搜索的 以bw开通的文件
search-paths: /config #搜索的路径 在config文件夹下面的文件
3.检测配置中心config-server 是否配置正确可以通过访问
http://localhost:8084/bw-sms/dev
得到下面内容

<Environment><name>bw-sms</name><profiles><profiles>dev</profiles></profiles><label/><version>1ffe10c7bf4e89dbec74fdfca004e5757acddf10</version><state/><propertySources><propertySources><name>https://gitee.com/shanshaowei/config.git/bw-sms/bw-sms-dev.properties</name><source><content>success</content><title>success11222222</title><id>12</id></source></propertySources></propertySources></Environment>
阅读全文
0 0
原创粉丝点击