Spring Cloud Config

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值