SpringBoot -- 配置中心服務/webhook

配置中心服務器


  • 配置中心服務器,以版本的管理方式對分佈式系統提供外部配置支持;
  • SpringCloud中採用Spring Cloud Config 進行集成,而想要進行實施更新則需要採用spring cloud bus的方式, 如 Rabbit、Kafka 等。
  • 版本管理採用 Git
  • 這裏採用Kafka,因爲本地有開發測試環境
  • 服務註冊與發現參考
  • /bus/refresh 爲消息總線帶來的刷新方式,可以手動調用

  • destination參數:用於刷新指定的應用服務
  • e.g. /bus/refresh?destination=feignserver:8084

配置中心服務

創建配置中心服務module

build.gradle引入:cloud:spring-cloud-config-serverspring-boot-autoconfigure; 因爲我們還想可以自動更新所以引入 spring-cloud-starter-bus-kafka,引入spring-boot-starter-actuator、spring-cloud-config-monitor用於監聽

build.gradle

apply plugin: 'org.springframework.boot'

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:"+ springCloudVersion
        mavenBom "org.springframework.boot:spring-boot-dependencies:"+ springBootVersion
        mavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion
    }
}
dependencies {
    compile('org.springframework.cloud:spring-cloud-config-server')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.boot:spring-boot-autoconfigure')
    compile('org.springframework.cloud:spring-cloud-starter-bus-kafka')
    compile('org.springframework.boot:spring-boot-starter-log4j2')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.cloud:spring-cloud-config-monitor')
    compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion)
    compile('org.xerial.snappy:snappy-java:' + snappyVersion)
    testCompile('org.springframework.boot:spring-boot-starter-test')

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

configurations {
    all*.exclude module: 'spring-boot-starter-logging'
    all*.exclude module: 'logback-classic'
    all*.exclude module: 'log4j-over-slf4j'
    all*.exclude module: 'snappy-java'
}

sourceSets {
    main {
        resources.srcDirs = ['src/main/resources', 'src/main/java']
        resources.includes = ['**/*.xml', '**/*.yml']
    }
}

jar {
    baseName = 'configserver-bootcwenao'
}

因爲引入了spring-cloud-starter-bus-kafka需要排除掉這個組件自帶 logback,然後又需要兼容本身的log,所以還需要引入org.apache.logging.log4j:log4j-1.2-api

排除logging

configurations {
    all*.exclude module: 'spring-boot-starter-logging'
    all*.exclude module: 'logback-classic'
    all*.exclude module: 'log4j-over-slf4j'
    all*.exclude module: 'snappy-java'
}

application.yml中配置Git

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git.oschina.net/youraccount/project/
          search-paths: dir
          username: xxxxxxxx@xx.com
          password: xxxxxx
          default-label: master

application.yml中配置Kafka

spring:
  cloud:
    stream:
      kafka:
        binder:
          brokers: localhost:9092
          zk-nodes: localhost:2181

通過 @EnableConfigServer 啓用配置服務

/**
 * @author cwenao
 * @version $Id ConfigserverBootcwenaoApplication.java, v 0.1 2017-01-12 14:21 cwenao Exp $$
 */
@EnableDiscoveryClient
@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigserverBootcwenaoApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(ConfigserverBootcwenaoApplication.class).web(true).run(args);
    }

}

創建Config-Client服務

build.gradle引入:cloud:spring-cloud-starter-config,同時引入 spring-cloud-starter-bus-kafka

apply plugin: 'org.springframework.boot'
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:"+ springCloudVersion
        mavenBom "org.springframework.boot:spring-boot-dependencies:"+ springBootVersion
        mavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion
    }
}
dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.cloud:spring-cloud-netflix-sidecar')
    compile('org.springframework.cloud:spring-cloud-starter-bus-kafka')
    compile('org.springframework.cloud:spring-cloud-stream')
    compile('org.xerial.snappy:snappy-java:' + snappyVersion)
    compile ('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-log4j2')
    compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion)

    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile group: 'junit', name: 'junit', version: '4.11'

}

configurations {
    all*.exclude module: 'spring-boot-starter-logging'
    all*.exclude module: 'logback-classic'
    all*.exclude module: 'log4j-over-slf4j'
    all*.exclude module: 'snappy-java'
}

sourceSets {
    main {
        resources.srcDirs = ['src/main/resources', 'src/main/java']
        resources.includes = ['**/*.xml', '**/*.yml']
    }
}
jar {
    baseName = 'apigateway-bootcwenao'
}

配置 bootstarp.ymldiscovery開啓訪問配置服務中心

server:
  port: 10002
sidecar:
  port: 20001
#configServer
spring:
  application:
    name: apigateway
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8888/
    discovery:
      enabled: true
    stream:
      kafka:
        binder:
          brokers: localhost:9092
          zk-nodes: localhost:2181

註解 @EnableDiscoveryClient

@SpringCloudApplication
@EnableDiscoveryClient
@EnableSidecar
public class ApiGatewayBootcwenaoApplication {

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

Git 中文件名字 apigateway-dev.yml
內容爲原先在本地的配置

啓動順序

服務註冊中心 –> 配置中心 –> 其他服務

訪問 http://localhost:8761

可以看到配置中心服務於想依賴的服務都已經啓動註冊成功

註冊中心

手動調用刷新

  • 修改任意的配置文件
  • postman中調用 /bus/refresh ,post方式調用

自動更新配置/webhook

webhook配置

application.yml

encrypt:
    key: cwenao

git倉庫 webhook配置

Github webhook

webhook入口

url與 secret

配置完成

可能的錯誤

  • zookeeper沒有讀取到
  • kafka沒啓動
  • log4j2與其他衝突
  • git uri地址錯誤,需要最後加“/”

代碼

代碼請移步 Github參考地址

如有疑問請加公衆號(K171),如果覺得對您有幫助請 github start
公衆號_k171

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章