微服务(spring cloud配置中心)

 1:理解bootstrap.yamlhtml

它会在application以前加载,若是和application有同名属性,先启动的会被覆盖。java

2:配置中心与咱们的注册中心,必需要有一个先启动git

3:spring的配置与环境化github

  在spring3.0-》web

<beans profile =”test”>
   <bean id=””>
</beans>spring

4:搭建一个配置中心bootstrap

 Git svn 本地文件缓存

读取本地文件:springboot

理解一下 ${user.dir}服务器

在配置中心/resources下新建文件夹configs

建立三个文件 eurekaserver-dev.yml eurekaserver-prod.yml eurekaserver-prod.yml

 

引入jar

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--引入核心jar-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

配置文件:

server:
  port: 5000
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          ##定义去拉取配置的地址
          uri: D:\\lesson-1\\config-server\\src\\main\\resources\\configs


management:
  endpoints:
    web:
      exposure:
        include: "*"

打开git初始化:

   git init

   git add .

   git commit -m “frist commit”

 

在启动类上加上注解@EnableConfigServer

配置中心完成。

 

客户端链接:

新建文件---bootstrap.yml

配置以下: 

spring:
  application:
    name: eurekaserver
  cloud:
    config:
      name: eurekaserver
      uri : http://localhost:5000/
  profiles:
    active: dev
server:
  port: 10000

而后 引入jar包

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>

在启动类上加入@EnableDiscoveryClient

客户端完成,启动就能够访问

 

根据在线地址向git去拉取配置:

1:配置文件以下:

##定义一个名字
spring:
  application:
      name: dnconfig
##配置服务器的文件地址git仓库
  cloud:
      config:
          server:
              git:
                  #这是老师的git地址,能够本身去定义一个git地址去获取
                  uri: https://github.com/yaojin156/tmp.git
                   #${user.dir}\src\main\resources\configs
​
                   #定义为强制向git去拉取配置
                  force-pull: true
​
​
##定义当前这个端口
server:
  port: 9090
​
##定义env 和 health的开放 2.0中这个方式已经被取消
#management:
#   security:
#       enabled: true
​
​
##定义env 和health的开放 2.X版本应该这么写
​
management:
  endpoints:
      web:
          exposure:
              include: env

2:在计算机中新建三个配置文件(个人文件放在D:\download\tmp)

orderserver-prod.properties表示生产服务配置

orderserver-test.properties表示测试配置

orderserver-dev.properties表示开发配置

指定配置路径为:

cloud:
      config:
          server:
              git:
                  #这是老师的git地址,能够本身去定义一个git地址去获取
                  uri: https://github.com/yaojin156/tmp.git

3:将本身的配置文件提交到在线的git中

git clone https://github.com/yaojin156/tmp.git
​
git add orderserver-*.properties
git commit -m "fritst commit"
git push

4:配置强制拉取git文件

cloud:
      config:
          server:
              git:
                  uri: https://github.com/yaojin156/tmp.git
                   #${user.dir}\src\main\resources\configs
​
                   #定义为强制向git去拉取配置
                  force-pull: true

6:开始测试本地配置是否生效

http:localhost:9090/order/server-prod

 

搭建一个客户端configclient

首先了解bootstarp配置
bootstarp配置文件会被先加载,一旦在咱们的application中存在同名配置,会直接生效application中的配置,因此,咱们能够认为这个是一个覆盖的过程。

了解一下springboot的Actuator监控

Actuator是springboot提供的一个监控方式。

Actuator是Spring Boot提供的对应用系统的自省和监控的集成功能,能够查看应用配置的详细信息,例如自动化配置信息、建立的Spring beans以及一些环境属性等。

Actuator的监控分两类:原生端点和用户自定义端点;自定义端点主要是指扩展性,用户能够根据本身的实际应用,定义一些比较关心的指标,在运行期进行监控。

原声端点分为三类

  • 应用配置类:能够查看应用在运行期的静态信息:例如自动配置信息、加载的springbean信息、yml文件配置信息、环境信息、请求映射信息;
  • 度量指标类:主要是运行期的动态信息,例如堆栈、请求连、一些健康指标、metrics信息等;
  • 操做控制类:主要是指shutdown,用户能够发送一个请求将应用的监控功能关闭。

Actuator 提供了 13 个接口

咱们只须要去了解咱们今天要用的 env/{name} 根据名称得到特定的属性值

1:配置bootstrap.yml

spring:
cloud:
  config:
   #定义为文件的名字
    name: order-server
     #去获取到配置中心的地址
    uri: http://127.0.0.1:9090/
     #获取到咱们的profile名字
    profile: test

2:配置application.yml

##定义端口号和实例名称
server:
port: 8091
​
spring:
application:
  name: spring-cloud-config-client
###定义开启env
management:
endpoints:
  web:
    exposure:
      include: env

3:启动config-client 访问

http://localhost:8091/actuator/env

能够看到咱们的配置中心内部的配置信息

4:开始从配置中心获取配置信息

@Value("${name}")
   private String name;
​
   @Value("${id}")
   private String id;
​
   @RequestMapping("/hi")
   public String hi(){
       System.out.println("输出的东西是:id:"+id+",name:"+name);
       return "id:"+id+",name:"+name;
  }

5: 若是服务端配置发生变化,客户端如何发生变化?

咱们的配置并非一成不变的,若是我去移动了服务器,或者我须要新加入一个节点,那么我就须要在配置项中把配置进行修改,那么在这个时候,咱们难道要去重启咱们的全部服务节点吗?

在配置文件orderserver-test.properties中新加入配置

dn.name = nick
dn.age = 17

刷新服务,配置已经在配置中心能够显示了。

可是,在咱们的客户端

并无相应的配置项存在,那么问题就来了。

这时候咱们该怎么办?

spring给咱们提供了一个刷新的方式,能够经过刷新配置端点来解决当前的问题。

具体操做以下:修改客户端配置application.yml

management:
endpoints:
  web:
    exposure:
     #让客户端支持env和refresh方式
      include: env,refresh

经过postman向咱们的配置服务器发送请求http://localhost:8091/actuator/refresh 记得是POST方式

这样咱们就能够获取到修改后最新的配置了。

正常状况下,咱们修改了配置须要cloud的Bus也就是事件总线来对咱们客户端通知进行拉取,如今暂时不讲。留在后面一块儿完成。这里只须要知道这种方式能够去完成咱们的目标便可。

 

理解refresh的做用

官方文档地址:

https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.1.0.RC3/single/spring-cloud-config.html

在这段话中,官方文档提到了一个叫作spring-cloud-config-monitor的东西,这一段话中主要的意思其实就是咱们的配置中心的变动能够用spring cloud Bus来对咱们进行通知。若是在配置服务器中添加Spring - Cloud配置监视器库的依赖项并激活Spring云总线,则启用/monitor端点。

注意文档中这一段:

When the webhook is activated, the Config Server sends a RefreshRemoteApplicationEvent targeted at the applications it thinks might have changed.

当webhook被激活时,配置服务器发送一个RefreshRemoteApplicationEvent,目标是它认为可能已经更改的应用程序。

好,这一块东西咱们暂时不讲,先寄存着,咱们先了解一下refresh的原理

public synchronized Set<String> refresh() {
       //调用下面的刷新方法。
        Set<String> keys = this.refreshEnvironment();
       //清空RefreshScope缓存
    // RefreshScope用新的环境参数从新生成Bean
        this.scope.refreshAll();
        return keys;
    }


public synchronized Set<String> refreshEnvironment() {
       //提取标准参数(SYSTEM,JNDI,SERVLET)以外全部参数变量
        Map<String,Object>before=
            this.extract(this.context.getEnvironment().getPropertySources());
      //把原来的Environment里的参数放到一个新建的Spring Context容器下从新加载,完事以后关闭新容器
        this.addConfigFilesToEnvironment();
         
    //比较出变动项
        Set<String> keys = this.changes(before,                             this.extract(this.context.getEnvironment().getPropertySources())).keySet();//提取更新过的参数(排除标准参数)  
        //发送EnvironmentChangeEvent
        //发布环境变动事件,接收:EnvironmentChangeListener/LoggingRebinder
        this.context.publishEvent(new EnvironmentChangeEvent(this.context, keys));
        return keys;
    }
相关文章
相关标签/搜索