您现在的位置是:首页 > 学无止境 > Spring Cloud

Spring cloud入门基础(8)实现git云配置及配置中心

贾继涛 2019-01-22 2273人围观

1.首先看下我整理的项目目录结构如下

 png

2. 创建File->New->Directory创建文件夹hello-spring-cl-config目录结构类似如下

配置中心服务端

png

3.pom.xml文件



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.jjt.cloud</groupId>
        <artifactId>hello-spring-cl-den</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cl-den/pom.xml</relativePath>
    </parent>
    <artifactId>hello-spring-cl-config</artifactId>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--eureka server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
      
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.jjt.hello.hello.ConfigServerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


4.启动类



package com.jjt.hello.hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}


5.application.yml



eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
#此个端口不能改要向改用boostrap.propertis
server:
  port: 8888
spring:
  cloud:
    config:
      label: master
      server:
        git:
          uri: https://gitee.com/jiajitao/springCloudConfig
          searchPaths: respo
          username: 用户名
          password: 密码
  application:
    name: hello-spring-cl-config
#http://127.0.0.1:8888/sumer-feign/dev/master


6.配置文件中的是码云的Url在其中建立respo文件夹,建议配置文件如下截图

访问地址 http://127.0.0.1:8888/sumer-feign/dev/master


7.配置客户端在继续修改 如下项目

Spring Clound入门基础(4)客户端Feign实现

8.在Feign上述项目pom.xml中增加

<!-- 读取git中的配置文件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 读取git中的配置文件 刷新 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

9.修改application.yml文件配置如下

其中sumer-feign是git中配置文件的名称

下边的配置是手动刷新线上配置的操作



spring:
  cloud:
    config:
      label: master
      profile: pro
      uri: http://127.0.0.1:8888/
      name: sumer-feign
#2.x后需要保留接口 这里在启动的时候更新配置文件
#用postman 调用这个
# http://127.0.0.1:9001/actuator/refresh
management:
 endpoints:
  web:
   exposure:
    include: refresh


10.启动观察启动端口来判断是否读取git文件配置了

11.新建Controller测试线上刷新配置

package com.jjt.hello.hello.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigRefreshController {
    @Value("${myself.feign.config.value}")
    private String configValue;
    /*@RefreshScope  这个类注解必须引用
    *否则
    * @Value("${myself.feign.config.value}")
    * 无法获取到最新的值
    * */
    @RequestMapping(value = "/configvalue")
    public String configvalue() {
        return "配置文件的myself.feign.config.value值为"+configValue;
    }
}

12.用postman 调用这个 http://127.0.0.1:9001/actuator/refresh 

如果git中配置改变调用这个 线上会自动改变



我的代码公开在码云 其中包括pom中 父依赖

https://gitee.com/jiajitao/springCloudFirst


您的支持