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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
prometheus-webhook-dingtalk 是一个用于将 Prometheus 监控告警消息发送到钉钉的 Webhook 工具。它可以帮助你将 Prometheus 监控告警通过钉钉机器人发送到指定的群组或用户。 你可以通过以下步骤来配置和使用 prometheus-webhook-dingtalk: 1. 安装 prometheus-webhook-dingtalk:你可以使用 Go 工具链来安装 prometheus-webhook-dingtalk,运行以下命令: ``` go get github.com/timonwong/prometheus-webhook-dingtalk/cmd/dingtalk ``` 2. 创建钉钉机器人:在钉钉中创建一个自定义机器人,并获取到它的 Webhook 地址,用于将告警消息发送到指定的群组或用户。 3. 创建配置文件:在 prometheus-webhook-dingtalk 的配置文件中,你需要指定钉钉机器人的 Webhook 地址以及其他相关参数。你可以创建一个名为 config.yml 的配置文件,并将以下示例内容填入: ```yaml listen: 0.0.0.0:8060 dingtalk: webhook: https://oapi.dingtalk.com/robot/send?access_token=your_webhook_token ``` 4. 启动 prometheus-webhook-dingtalk:运行以下命令来启动 prometheus-webhook-dingtalk: ``` dingtalk -config.file=config.yml ``` 5. 配置 Prometheus:在 Prometheus 的配置文件中,添加以下内容来指定告警消息的接收端: ```yaml receivers: - name: 'dingtalk' webhook_configs: - url: 'http://prometheus-webhook-dingtalk:8060/dingtalk/webhook' ``` 6. 重新启动 Prometheus:确保 Prometheus 已经重新加载了配置文件,并重启 Prometheus 服务。 现在,当 Prometheus 监控触发告警时,prometheus-webhook-dingtalk 将会将告警消息发送到钉钉机器人的 Webhook 地址,从而通知到指定的群组或用户。 请注意,以上步骤仅为一般示例,实际操作可能会因环境和需求而有所不
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值