SpringBoot -- 配置中心服务/webhook

Spring Cloud 配置中心实战

配置中心服务器


  • 配置中心服务器,以版本的管理方式对分布式系统提供外部配置支持;
  • 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中实现Webhook #### 创建一个新的Spring Boot项目 为了创建一个能够接收Webhook请求的应用程序,首先需要建立一个新的Spring Boot应用程序。这可以通过Spring Initializr完成。 #### 添加依赖项 确保`pom.xml`文件中有必要的依赖项来处理HTTP请求。通常情况下,Spring Web Starter就足够了[^2]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </version> ``` #### 配置控制器以接受POST请求 接下来,在应用内定义一个RESTful端点用于监听来自外部服务的POST请求。此操作通过编写带有`@RestController`注解的类并指定路径模式以及支持的方法类型(`RequestMethod.POST`)来完成: ```java @RestController public class WebHookController { @PostMapping("/webhook") public ResponseEntity<String> handleWebhook(@RequestBody String payload){ // 处理接收到的数据... System.out.println("Received Payload: " + payload); return new ResponseEntity<>("Success", HttpStatus.OK); } } ``` 上述代码片段展示了如何设置一个简单的处理器方法,该方法会打印出所接收到的有效负载,并向调用方返回成功的响应状态码。 对于安全性考虑,可以利用环境变量或配置属性存储敏感信息如用户名密码等,而不是硬编码到源代码里。例如,可以从application.properties读取认证凭证[^1]: ```properties spring.boot.admin.client.username=adminUser spring.boot.admin.client.password=adminPass ``` 最后一步是启动服务器实例,默认情况下它会在8080端口上运行;但是也可以自定义端口号和其他参数以便更好地适应特定需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值