配置中心

git仓库

git仓库文件

server搭建

  1. 配置依赖
<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>
  1. 配置文件
# bootstrap.yml
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xiaolinzi12/server-config-repo.git
server:
  port: 9437
  1. 启动类

核心注解@EnableConfigServer不可丢弃

@SpringBootApplication
@EnableConfigServer
public class ServerConfigApplication {

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

}

client 配置

  1. 核心依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
  1. 配置文件
spring:
  application:
  # 注意命名
    name: Vblog
    
  cloud:
    config:
    # 注意
      profile: dev
      
      # 这个地址是配置server的地址
      uri: http://localhost:9437/
    bus:
      trace:
        enabled: true
        
# mq配置 
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    
management:
  endpoints:
    web:
      exposure:
        include: "*"

测试

  1. 启动服务后
  2. 测试
  3. 修改文件后
 调用客户端如下地址(post请求)
 http://localhost:8080/actuator/bus-refresh
  1. 再次测试

林小林
3 声望0 粉丝

努力努力