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

Spring Boot 提供了一种方便的方式来集成 Webhooks,这是一种服务器向其他系统发送通知的方法,通常用于处理事件驱动的应用场景,比如当某个外部服务发生更改时,Spring Boot 应用能够接收到这些通知并执行相应的操作。 以下是使用 Spring Boot 集成 Webhook 的基本步骤: 1. **添加依赖**:在 `pom.xml` 或 `build.gradle` 文件中添加 Websocket或WebSocketClient的相关依赖,如 `org.springframework.boot:spring-boot-starter-websocket`。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 2. **创建WebSocketEndpoint**:定义一个实现了 `org.springframework.web.socket.server.support.WebSocketHandler` 接口的类,这个类将处理客户端连接和消息的传递。在这里,你可以编写接收和处理Webhook请求的逻辑。 ```java import org.springframework.messaging.Message; import org.springframework.messaging.simp.SimpMessageHeaderAccessor; import org.springframework.stereotype.Component; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; @Component public class WebhookHandler extends TextMessageHandler { @Override protected void handleTextMessage(WebSocketSession session, Message<String> message) { // 在这里处理接收到的Webhook消息,解析、业务处理等 String payload = message.getPayload(); // 处理逻辑... } } ``` 3. **配置WebSocket**:在 `application.properties` 或 `application.yml` 中配置WebSocket相关设置,例如开启WebSocket支持,并指定处理程序。 ```yaml spring: web: socket: port: 8081 path: /webhook handler: webhookHandler ``` 4. **启动应用**:启动 Spring Boot 应用,它会监听指定的端口和路径,等待接收来自外部系统的Webhook请求。 5. **触发Webhook**:外部系统(如 GitLab, Slack 等)在发生特定事件时,会向服务器的 `/webhook` 地址发送 HTTP POST 请求,携带事件数据。Spring Boot 应用通过处理WebSocket消息来响应和处理这些事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值