springcloud~配置中心实例搭建

server端

build.gradle相关

dependencies {
    compile('org.springframework.cloud:spring-cloud-config-server',
            'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    )
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

bootstrap.yml配置

server:
  port: 8200
spring:
  application:
    name: lind-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/bfyxzls/lindconfig.repo.git/
          search-paths: config-repo
          username: bfyxzls@sina.com
          password: xxxx

启动项相关

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
class Application {
  public static void main(String[] args) {
    // demo http://localhost://8200/email-svt.yml
    SpringApplication.run(Application.class, args);
  }
}

客户端

build.gradle相关

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web',
            'org.springframework.cloud:spring-cloud-starter-config',
            'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

bootstrap.yml配置

spring:
  application:
    name: lind
  cloud:
    config:
      uri: http://localhost:8200
      profile: svt
      label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

启动项相关

@EnableEurekaClient
@SpringBootApplication
public class Application {

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

@RestController
public class HomeController {

  @Autowired
  private Environment profile;

  @RequestMapping("/")
  public String index(@RequestParam String key) {

    return key + "=" + profile.getProperty(key);
  }
}

配置文件仓库

这是一个GIT仓库,主要存储我们的配置文件的,以application.name作为文件名,profile作为后缀,我们客户端就是某个应用程序,它以后从仓库获取配置信息。

程序配置例子

lind.yml
lind项目最终配置
lind.svt.yml
lind.production.yml

lind.yml 默认的配置

lind.svt.yml 测试环境配置,将集成默认配置

lind.production.yml 生成环境配置,将集成默认配置

配置中心例子

用户服务
配置中心
订单服务
商品服务
Git仓库
posted @   张占岭  阅读(1702)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2017-06-20 docker~Dockerfile优化程序的部署
2016-06-20 插件~使用ECharts动态在地图上标识点
2013-06-20 EF架构~引入规约(Specification)模式,让程序扩展性更强
2012-06-20 面向对象的故事~数据底层操作告诉了我们接口,抽象类,继承与多态性的使用
点击右上角即可分享
微信分享提示