-
Notifications
You must be signed in to change notification settings - Fork 2
中文使用说明
曹鑫 edited this page Apr 12, 2019
·
4 revisions
支持灰度发布功能,服务降级,运维可视化操作。增加运维人员干扰服务的执行逻辑的工具
maven 依赖
<dependency>
<groupId>com.github.xincao9</groupId>
<artifactId>jswitcher-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
Controller 接口定义
package com.github.xincao9.jswitcher.sample.controller;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RootController {
@GetMapping("/")
public ResponseEntity<String> root () {
return ResponseEntity.ok(RandomStringUtils.randomAscii(128));
}
}
Service Application 启动
package com.github.xincao9.jswitcher.sample;
import com.github.xincao9.jswitcher.api.service.SwitcherService;
import com.github.xincao9.jswitcher.api.vo.QoS;
import com.github.xincao9.jswitcher.spring.boot.starter.EnableJswitcher;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
/**
* 模拟服务运行时,运维人员在管理后台上操作开关状态. 干扰服务的执行逻辑
*
* @author xincao9@gmail.com
*/
@SpringBootApplication
@EnableJswitcher // 开启注解
public class ServiceApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceApplication.class);
@Autowired
private SwitcherService switcherService;
private static final String KEY = ServiceApplication.class.getCanonicalName(); // 定义日志是否打印开关
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner() {
return (String... args) -> {
switcherService.register(KEY, Boolean.TRUE, "Recording ServiceApplication Logger", QoS.API); // 注册开关
for (int no = 0; no < 100; no++) {
if (switcherService.isOpen(KEY)) { // 判断开关状态
LOGGER.info(RandomStringUtils.randomAscii(128));
}
TimeUnit.SECONDS.sleep(1);
}
};
}
}
application.properties
jswitcher.application.name=jswitcher-sample // 应用名字
jswitcher.server.port=12306 // 管理端口
jswitcher.discovery.zookeeper=localhost:2181 // 服务注册zookeeper集群地址
jswitcher.database.name=switcher // 开关存储的数据库
jswitcher.database.user=root // 开关存储的用户
jswitcher.database.pass= // 开关存储的密码
jswitcher.database.host=127.0.0.1 // 开关存储的地址
jswitcher.database.port=3306 // 开关存储的端口
创建开关的数据库表结构
CREATE TABLE `switcher` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`application` varchar(64) NOT NULL DEFAULT 'default',
`key` varchar(64) NOT NULL DEFAULT '',
`open` tinyint(1) NOT NULL DEFAULT '0',
`describe` varchar(128) DEFAULT NULL,
`qos` varchar(24) NOT NULL DEFAULT 'API',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `key` (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
命令
- 下载 jswitcher-ui
- 启动命令 java -jar jswitcher-ui-1.2.2.jar --jsonrpc.discovery.zookeeper=localhost:2181
- 管理后台访问地址
界面
启动后,通过切换开关状态来控制日志的输出和 http://localhost:8080/ 是否可用性