最近需要将原来的平台云化掉,所以就用到了docker 将原来的单个计算逻辑都封装到docker内,想通过restful 形式来驱动docker 进行计算,就引入了 spring-boot + com.spotify.dockerclient,为了在docker内可以看见运行日志和后期计算节点优化,就引入了ESservice 做日志提交,(通过restful 提交)选择了bboss-elasticsearch-spring-boot-starter确实挺好用,整体任务是通过restful 将任务信息放置到redis里面,通过com.spotify.dockerclient 来处理任务
docker 内设计:
(1) docker 先获取服务器接口列表
(2) docker 获取参数列表和APPname json 形式传参
(3) docker OSS 下载计算文件
(4) docker 通过APPname 进行业务判断和业务执行
(5) docker 执行计算过程
(6) docker 将计算为文件的结果回传到OSS
(7) docker 将获取的参数列表更改后回传到webservice
webservice 关键代码:
maven 介绍
spring-boot,
org.projectlombok,
mybatis,
tkmybatis,
log4j,
com.aliyun.oss
bboss-elasticsearch-spring-boot-starter,
druid-spring-boot-starter
com.spotify-docker-client,
spring-boot-starter-data-redis,
springfox-swagger2
ES 操作:
@Service
public class LoggerCrud {
@Autowired
private BBossESStarter bbossESStarter;
@Value("${es.indicename}")
private String indicename;
@Value("${es.esmapper}")
private String esmapper;
@Value("${es.createindicename}")
private String createindicename;
public void createIndice(){
//创建加载配置文件的客户端工具,单实例多线程安全
ClientInterface clientUtil = bbossESStarter.getConfigRestClient(esmapper);
try {
//判读索引表demo是否存在,存在返回true,不存在返回false
boolean exist = clientUtil.existIndice(indicename);
//如果索引表demo已经存在先删除mapping
if(exist) {
clientUtil.dropIndice(indicename);
clientUtil.existIndice(indicename);
clientUtil.getIndice(indicename);//获取最新建立的索引表结构
}
//创建索引表demo
clientUtil.createIndiceMapping(indicename,createindicename);//索引表mapping dsl脚本名称,在esmapper/demo.xml中定义createDemoIndice
clientUtil.getIndice(indicename);//获取最新建立的索引表结构
} catch (ElasticSearchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addAndUpdateDocument(LoggerDemo demo) throws ParseException {
//创建创建/修改/获取/删除文档的客户端对象,单实例多线程安全
ClientInterface clientUtil = bbossESStarter.getRestClient();
clientUtil.addDocument(indicename,indicename, demo);
}
}
doker 驱动:
@Component
public class DockerConsumer extends Thread{
private static final Logger logger = LoggerFactory.getLogger(DockerConsumer.class);
@Autowired
private ListOperations<String, Object> listOperations;
@Value("${docker.consumerheartbeat}")
private String consumerheartbeat;
@Value("${docker.url}")
private String url;
private DefaultDockerClient dockerClient;
@Override
public void run()
{
super.run();
try
{
dockerClient = DefaultDockerClient.builder()
.uri(url).build();
dockerClient.pull("hello-world:latest");
ContainerCreation container = dockerClient.createContainer( ContainerConfig.builder().image("hello-world").build());
// dockerClient.execStartCmd("docker run -itd --name test1 hello-world").exec();
//logger.info("DockerConsumer run begin:"+info.toString());
while(true)
{
Thread.sleep(Integer.valueOf( consumerheartbeat ));
Task objtask = (Task)listOperations.leftPop( "list:task" );
}
}
catch(Exception e)
{
e.printStackTrace();
logger.error("DockerConsumer run error");
}
logger.info("DockerConsumer run end");
}
}
git地址:https://gitee.com/ligeandldd/webservicedocker.git