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,避免調試程序時從遠程下載。

<bean id="gitSync" class="com.nh.micro.sync.git.MicroGitSync"
    init-method="initRep" lazy-init="false">
    <property name="localPath" value="h:/temp/git"></property>
    <property name="remotePath" value="https://github.com/nhmicro/test-sync-groovy.git"></property>
    <property name="cloneFlag" value="true"></property>
    <property name="openFlag" value="${openFlag}"></property>
    <property name="version" value="head"></property>
</bean>

設置從本地下載目錄中加載groovy

注意設置depends-on確保git下載完成後在啓動加載

    <bean id="groovyInitUtil" class="com.nh.micro.rule.engine.core.GroovyInitUtil"
        init-method="initGroovy" lazy-init="false" depends-on="gitSync" >
        <property name="fileList">
            <list>
                <bean class="com.nh.micro.rule.engine.core.GFileBean">
                    <property name="ruleStamp" value="true"></property>
                    <property name="jarFileFlag" value="true"></property>
                    <property name="dirFlag" value="true"></property>
                    <property name="rulePath" value="/groovy/"></property>
                </bean>
                <bean class="com.nh.micro.rule.engine.core.GFileBean">
                    <property name="ruleStamp" value="true"></property>
                    <property name="jarFileFlag" value="false"></property>
                    <property name="dirFlag" value="true"></property>
                    <property name="rulePath" value="H:/temp/git/test-sync/groovy/"></property>
                </bean>                 
            </list>
        </property>
    </bean>

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")
@RestController
public 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接口

    <bean class="com.nh.micro.service.GroovyBeanScannerConfigurer">
        <property name="scanPath" value="com.nh.micro.springcloud.demo.web"></property>
    </bean>

ComputeController接口實現ComputeGroovy.groovy

package groovy

import 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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章