springcloud热部署
springcloud按照可运行jar包部署时,如果直接将业务脚本groovy打入jar则不支持热部署。
需要将业务脚本groovy放置到另一个git目录下编写,开发时使用linked目标放置到project中,部署是不打入jar中。
nhmicro框架代码地址:https://github.com/jeffreyning/nh-micro
借助nhmicro框架中micro-git-sync模块功能在启动时从git仓库中下载groovy脚本加载到springcloud应用中,
同时还可以支持动态发布,即提交新groovy到git仓库后,会自动下载并热部署到springcloud应用中。
使用micro-git-sync模块优点是:
1, 使应用按照可执行jar包运行时,也支持脚本热部署。
2, 准实时自动加载远程git中的新脚本代码。
3, 可以按照指定版本加载脚本。
配置MicroGitSync设置git远程地址和本地下载目录
如果设置了init-method="initRep",则准实时检查远程git仓库是否有代码更新,有则自动下载。
Version可以设置指定的版本,设置为head表示最新版本
cloneFlag表示启动时是否完全clone
openFlag表示是否有效,开发环境中可以设置为false,避免调试程序时从远程下载。
设置从本地下载目录中加载groovy
注意设置depends-on确保git下载完成后在启动加载
Micro-mvc不但可以与springmvc整合还可以与springcloud整合
Springcloud的controller层改为接口注解仍使用springcloud和springboot相关注解实现服务注册路由等配置,但controller接口上需要添加@InjectGroovy注解设置接口实现的关联groovy。
package com.nh.micro.springcloud.demo.web;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import com.nh.micro.service.InjectGroovy;@InjectGroovy(name="ComputeGroovy")@RestControllerpublic interface ComputeController { @RequestMapping(value = "/add" ,method = RequestMethod.GET) @ResponseBody public Integer add(@RequestParam(value="a") Integer a, @RequestParam(value="b") Integer b);}Spring配置中使用GroovyBeanScannerConfigurer扫描controller接口
ComputeController接口实现ComputeGroovy.groovy
package groovyimport javax.annotation.Resource;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.RequestParam;import org.apache.log4j.Logger;class ComputeGroovy {private final Logger logger = Logger.getLogger(getClass());//@Resource(name="discoveryClient")//public DiscoveryClient client; public Integer add(@RequestParam Integer a, @RequestParam Integer b) { //GroovyExecUtil.execGroovyRetObj("TestGroovy", "test"); Integer r = a + b; System.out.println(r); //ServiceInstance instance = client.getLocalServiceInstance(); //logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r); return r; }}