当前位置: 首页 > news >正文

Spring Cloud Config

为什么80%的码农都做不了架构师?>>>   hot3.png

Spring Cloud

Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。

Spring Cloud就是这些微服务的大管家,自然需要很多小弟来帮忙。主要的小弟有:Spring Cloud Config、Spring Cloud Netflix(Eureka、Hystrix、Zuul、Archaius…)、Spring Cloud Bus、Spring Cloud for Cloud Foundry、Spring Cloud Cluster、Spring Cloud Consul、Spring Cloud Security、Spring Cloud Sleuth、Spring Cloud Data Flow、Spring Cloud Stream、Spring Cloud Task、Spring Cloud Zookeeper、Spring Cloud Connectors、Spring Cloud Starters、Spring Cloud CLI.

和Spring boot 是什么关系

Spring boot 是 Spring 的一套快速配置脚手架,可以基于spring boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具;

spring -> spring booot > spring cloud 这样的关系。

Spring Cloud Config

俗称的配置中心,配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。就是以后大家武器、枪火什么的东西都集中放到一起,别随便自己带,方便以后统一管理、升级装备。

image

手脚架

git@git.oschina.net:2449149803/spring-cloud-netflix-example.git

SpringCloudConfig提供基于以下3个维度的配置管理:

  • 应用

这个比较好理解,每个配置都是属于某一个应用的

  • 环境

每个配置都是区分环境的,如dev, test, prod等

  • 版本

这个可能是一般的配置中心所缺乏的,就是对同一份配置的不同版本管理 Spring Cloud Config提供版本的支持,也就是说对于一个应用的不同部署实例,可以从服务端获取到不同版本的配置,这对于一些特殊场景如:灰度发布,A/B测试等提供了很好的支持。

  • application:应用名
  • profile:环境
  • label:版本
{application}/{profile}[/{label}]
{application}-{profile}.yml
{label}/{application}-{profile}.yml
{application}-{profile}.properties
{label}/{application}-{profile}.properties

springBoot 快速构建 config server

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

yml 配置

spring:
  profiles:
    active: native
  application:
    name: config-server
  rabbitmq:
    host: localhost
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/
        git:
          uri: https://git.oschina.net/2449149803/gittest.git

  • RSA 验证

spring:
    cloud:
      config:
        server:
          git:
            uri: git@gitserver.com:team/repo1.git
            ignoreLocalSshSettings: true
            hostKey: someHostKey
            hostKeyAlgorithm: ssh-rsa
            privateKey: |
                         -----BEGIN RSA PRIVATE KEY-----
                         MIIEpgIBAAKCAQEAx4UbaDzY5xjW6hc9jwN0mX33XpTDVW9WqHp5AKaRbtAC3DqX
                         -----END RSA PRIVATE KEY-----

参考 spring-cloud-config

SpringCloudConfig Client

spring.cloud.config.uri=http://127.0.0.1:${config.port:8888}
spring.cloud.config.name=alan-provider-data-config
spring.cloud.config.profile=${config.profile:dev}

1.特别注意 配置中心的地址一定要放在bootstrap.properties中,这个配置文件是由“根”上下文优先加载,可以保证程序启动之初就感知到远程配置中心的存在,并从远程获取配置,随后继续启动系统,这点十分重要。

2.而application.properties是由子上下文加载,加载顺序低于前者,如果配置中心地址放在这里,并且你远程配置了一些启动相关的必要参数,那么,你的程序很可能由于缺少参数而启动失败。

3.下面这段代码,最关键的是第一行,第二行如果不配置系统默认读取spring.application.name,第三行如果不配置,系统默认default,即:${spring.application.name}.properties

4.我们一般的做法是,在系统启动的时候,用命令行传入--spring.cloud.config.profile=dev|prod|test的方式,因为在启动的时候,我们是明确知道我要获取哪套配置的

引用配置的类要加@RefreshScope注解

@RefreshScope
@RestController
@Api
public class AServiceController {

    @Value("${name:unknown}")
    private String name;

    @Autowired
    private HystrixWrappedServiceBClient serviceBClient;
    @Autowired
    DiscoveryClient discoveryClient;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String printServiceA() {
        //ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
        //return serviceInstance.getServiceId() + " (" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + ")" + "===>name:" + name + "<br/>" + serviceBClient.printServiceB();
        return "name = " + name;
    }
}

RefreshScope 注解

我们知道Spring原生提供了一些scope,如singleton,prototype,request等。 为了实现配置更新后,已经注入bean的值也能更新的目的,Spring Cloud提供了一个新的scope - RefreshScope。 所以,对于那些有注入值的bean,我们可以把它们标记为RefreshScope,这样当运行时发现有配置更新的时候,通过调用RefreshScope.refresh(beanName)或RefreshScope.refreshAll(),从而下次这些bean被使用时会被重新初始化,进而会被重新注入值,所以也就达到了更新的目的。

转载于:https://my.oschina.net/orgsky/blog/1491263

相关文章:

  • HADOOP单机系统安装
  • ASCLL
  • 关于mybatis的学习笔记
  • qt-4.8.4安装和环境变量配置
  • Cacti和Nagios整合
  • linux 运维shell习题
  • iOS Xcode 打包之后,不能输出日志
  • 我们每个人其实都有周杰伦的“影子”
  • 删除heroku上的数据库记录
  • 哈尔滨铁路千余列车加装无线WIFI网络
  • Redis操作命令总结
  • 如何成为一个Java Web工程师
  • 磁盘将死:到2020年将有四分之三PC设备采用SSD
  • linux网络命令(八)之top、htop
  • 《计算机网络课程设计(第2版)》——1.4节课程设计要求及评分标准
  • 08.Android之View事件问题
  • 10个确保微服务与容器安全的最佳实践
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • java中具有继承关系的类及其对象初始化顺序
  • js操作时间(持续更新)
  • nfs客户端进程变D,延伸linux的lock
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • Vue学习第二天
  • 阿里云Kubernetes容器服务上体验Knative
  • 大整数乘法-表格法
  • 容器化应用: 在阿里云搭建多节点 Openshift 集群
  • 使用agvtool更改app version/build
  • 用element的upload组件实现多图片上传和压缩
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 智能网联汽车信息安全
  • 最简单的无缝轮播
  • #Z2294. 打印树的直径
  • #经典论文 异质山坡的物理模型 2 有效导水率
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (M)unity2D敌人的创建、人物属性设置,遇敌掉血
  • (python)数据结构---字典
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (循环依赖问题)学习spring的第九天
  • (转) RFS+AutoItLibrary测试web对话框
  • *(长期更新)软考网络工程师学习笔记——Section 22 无线局域网
  • *Algs4-1.5.25随机网格的倍率测试-(未读懂题)
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .cn根服务器被攻击之后
  • .NET Framework杂记
  • .net wcf memory gates checking failed
  • [ C++ ] 继承
  • [ CTF ]【天格】战队WriteUp- 2022年第三届“网鼎杯”网络安全大赛(青龙组)
  • [AIGC codze] Kafka 的 rebalance 机制
  • [Android]使用Retrofit进行网络请求
  • [BUG] Authentication Error
  • [BZOJ1089][SCOI2003]严格n元树(递推+高精度)