Hot deployment of springcloud
When springcloud is deployed as a runnable jar package, hot deployment is not supported if the business script groovy is directly typed into the jar.
The business script groovy needs to be placed in another git directory to be written, and the linked target is used to be placed in the project during development, and the deployment is not entered into the jar.
nhmicro Framework Code Address: github.com/jeffreyning/nh-micro
download groovy script from git repository at startup and load it into springcloud application with micro-git-sync module function in nhmicro framework
At the same time, it can also support dynamic publishing, that is, after submitting a new groovy to the git repository, it will be automatically downloaded and hot deployed to the springcloud application.
the advantage of using that micro-git-sync module are:
1. When the application runs according to the executable jar package, it also supports script hot deployment.
2, quasi-real-time automatic loading of new script code in remote git.
3, you can load scripts according to the specified version.
Configure MicroGitSync Set git remote address and local download directory
If init-method="initRep" is set, the remote git repository is checked for code updates in quasi-real time, and if so, it is automatically downloaded.
Version can be set to the specified version, set to head to indicate the latest version
cloneFlag indicates whether it is completely clone at startup
openFlag indicates whether it is valid. It can be set to false in the development environment to avoid downloading remotely when debugging programs.
Settings Load groovy from local download directory
Note Set depends-on to ensure git loads at boot after download is complete
Micro-mvc can be integrated not only with springmvc but also with springcloud
The controller layer of Springcloud is changed to interface annotation. Springcloud and springboot related annotations are still used to implement configuration such as service registration route, but @InjectGroovy annotation needs to be added to the controller interface to set the association groovy implemented by the interface.
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 configuration uses GroovyBeanScannerConfigurer scan controller interface
ComputeController interface implementation 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; }}