小小又开始学习了,这次学习的内容是Spring Cloud 相干内容,这次学习的是Config相干的内容。
通过git实现分布式的配置文件的部署,达到更新git,就能够更新配置信息的性能、

Server端

增加相干maven

这里配置Server端
增加相干的依赖

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

更新相干配置文件

server:  port: 8080spring:  application:    name: spring-cloud-config-server  cloud:    config:      server:        git:          uri: https://github.com/meteor1993/SpringCloudLearning # git仓库的地址          search-paths: chapter6/springcloud-config  # git仓库地址下的绝对地址,能够配置多个,用,宰割。          username: #Git仓库用户名          password: #Git仓库明码

其中,uri表明git的配置地址,search-paths表明git相干的配置门路,这里应用git作为分布式的配置文件的存储

其配置文件的门路为

SpringCloudLearning/chapter6/springcloud-config/springcloud-config-pro.properties拜访地址为 https://github.com/meteor1993/SpringCloudLearning/blob/master/chapter6/springcloud-config/springcloud-config-pro.properties

增加启动类相干的注解

这里增加EnableConfigServer类相干的注解,

用于激活Spring Cloud 对配置核心的相干激活

package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }}

这个时候拜访 http://localhost:8080/springcloud-config/pro 就能够实现对配置文件信息的拜访
其中springcloud-config为配置核心的文件名称。pro为对应的相干的配置文件。
这里对应的相干的配置文件,命名有

springcloud-config-dev.properties  对应于dev开发模式springcloud-config-pro.properties  对应于pro开发模式springcloud-config-test.properties  对应于test开发模式

其拜访的pro更改为相干的配置类型即可

拜访成果

拜访连贯 http://localhost:8080/springcloud-config/pro 即可看到相干的配置信息

用户端

后面服务端曾经拜访实现,这里拜访客户端

新建子项目

增加相干的maven

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config --><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-config</artifactId>    <version>2.2.3.RELEASE</version></dependency>

增加配置文件

这里配置文件分为两个配置文件,别离为application.yml 和 bootstrap.yml 这两个配置文件

application.yml

这里配置application.yml 配置文件

server:  port: 8081spring:  application:    name: spring-cloud-config-client

再次配置bootstrap.yml 文件

spring:  cloud:    config:      name: springcloud-config      profile: dev      uri: http://localhost:8080/      label: master

这样就实现了一次的配置文件书写。
其中name为配置git的name,profile对应于版本。url对应于server的信息,label对应于相干的分支。

增加启动类

package com.springcloud.configclient;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ConfigClientApplication {    public static void main(String[] args) {        SpringApplication.run(ConfigClientApplication.class, args);    }}

增加配置信息读取类

增加配置信息的读取类

package com.springcloud.configclient.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @Author: shiyao.wei * @Date: 2019/7/4 16:19 * @Version: 1.0 * @Desc: */@RestControllerpublic class HelloController {    @Value("${springcloud.hello}")    private String hello;    @RequestMapping("/hello")    public String from() {        return this.hello;    }}

这样,就实现了配置信息的对立读取