Refactoring for running in a docker swarm cluster

and upgrade to version 7.28.0
This commit is contained in:
Nicolas Héron 2019-10-21 11:24:57 +02:00
commit e2c87e9d20
15 changed files with 254 additions and 117 deletions

View file

@ -20,6 +20,9 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.kie:kie-internal:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-api:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie.soup:kie-soup-maven-support:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.1.0.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.1.0.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.1.0.RELEASE" level="project" />
@ -147,8 +150,8 @@
<orderEntry type="library" name="Maven: com.google.guava:guava:13.0.1" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.6" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.7" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-internal:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-api:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie.soup:kie-soup-maven-support:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-internal:7.28.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-api:7.28.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie.soup:kie-soup-maven-support:7.28.0.Final" level="project" />
</component>
</module>

View file

@ -14,6 +14,9 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.kie:kie-internal:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-api:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie.soup:kie-soup-maven-support:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-elasticsearch:2.1.0.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.1.0.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.1.0.RELEASE" level="project" />
@ -92,8 +95,8 @@
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:13.0.1" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.1" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-internal:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-api:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie.soup:kie-soup-maven-support:7.23.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-internal:7.28.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-api:7.28.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.kie.soup:kie-soup-maven-support:7.28.0.Final" level="project" />
</component>
</module>

View file

@ -94,7 +94,10 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-quartz2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-quartz2</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>

View file

@ -18,12 +18,14 @@ package org.chtijbug.drools.proxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import java.net.InetAddress;
import java.net.UnknownHostException;
// CHECKSTYLE:OFF
@SpringBootApplication
@Configuration
public class DroolsBusinessProxyServer {

View file

@ -2,19 +2,14 @@ package org.chtijbug.drools.proxy.camel;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.chtijbug.drools.proxy.service.KieServiceCommon;
import org.kie.server.api.model.KieServerInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class AdminRouter extends RouteBuilder {
@Resource
KieServiceCommon kieServiceCommon;
@Value("${server.port}")
private int serverPort;
@Value("${camel.component.servlet.mapping.context-path}")

View file

@ -0,0 +1,12 @@
package org.chtijbug.drools.proxy.camel;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class AliveTimeStampRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
from("quartz2://myGroup/myTimeStampTimer?cron=0/5+*+*+?+*+*").to("bean:kieService?method=setTimeStamp()");
}
}

View file

@ -36,10 +36,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
@ -67,25 +67,30 @@ public class KieServiceCommon {
@Value("${server.port}")
private int serverPort;
private Map<String,DroolsRouter> routes = new HashMap<>();
@Autowired
private ApplicationContext appContext;
private String hostName= "localhost";
private Map<String, DroolsRouter> routes = new HashMap<>();
public KieServiceCommon() {
// for now, if no server impl is passed as parameter, create one
// this.server = KieServerLocator.getInstance();
// this.server = KieServerLocator.getInstance();
}
public static String getKieServerID(){
public static String getKieServerID() {
return System.getProperty("org.kie.server.id");
}
@PostConstruct
private void initCamelBusinessRoutes() {
this.server = new KieServerImpl(new PlatfomKieServerStateRepository(containerRepository,this));
this.server = new KieServerImpl(new PlatfomKieServerStateRepository(containerRepository, this));
this.server.init();
List<KieServerExtension> serverExtensions = this.server.getServerExtensions();
if (registry==null || droolsChtijbugKieServerExtension==null) {
if (registry == null || droolsChtijbugKieServerExtension == null) {
for (KieServerExtension serverExtension : serverExtensions) {
if (serverExtension instanceof DroolsChtijbugKieServerExtension) {
droolsChtijbugKieServerExtension = (DroolsChtijbugKieServerExtension) serverExtension;
@ -103,72 +108,94 @@ public class KieServiceCommon {
String serverName = KieServiceCommon.getKieServerID();
String sftpPort = System.getProperty("org.chtijbug.server.sftpPort");
try {
InetAddress inetAddress = InetAddress.getLocalHost();
hostName = inetAddress.getHostName();
} catch (UnknownHostException e) {
logger.info("initCamelBusinessRoutes.getLocalHost", e);
}
List<RuntimePersist> itIsMes = runtimeRepository.findByServerName(serverName);
ServiceResponse<KieServerInfo> result = server.getInfo();
String version = result.getResult().getVersion();
String isSwarm=System.getProperty("org.kie.server.swarm");
if (itIsMes.size() == 0) {
ServiceResponse<KieServerInfo> result = server.getInfo();
String version = result.getResult().getVersion();
String hostName = "localhost";
try {
InetAddress inetAddress = InetAddress.getLocalHost();
hostName = inetAddress.getHostName();
} catch (UnknownHostException e) {
logger.info("initCamelBusinessRoutes.getLocalHost", e);
RuntimePersist runtimePersist = new RuntimePersist(serverName, version, hostName ,
String.valueOf(serverPort), sftpPort,
hostName, RuntimePersist.STATUS.UP.toString());
if ("1".equals(isSwarm)) {
runtimePersist.setServerUrl("http://" + serverName + ":" + serverPort);
}else {
runtimePersist.setServerUrl("http://" + hostName + ":" + serverPort);
}
RuntimePersist runtimePersist = new RuntimePersist(serverName, version, "http://" + hostName + ":" + serverPort,
String.valueOf(serverPort), sftpPort,
hostName,RuntimePersist.STATUS.UP.toString());
runtimeRepository.save(runtimePersist);
}else{
RuntimePersist runtimePersist =itIsMes.get(0);
runtimePersist.setStatus(RuntimePersist.STATUS.UP.toString());
try {
/**
* HostName may have changed by reboot so retake it and save it
*/
InetAddress inetAddress = InetAddress.getLocalHost();
String hostName = inetAddress.getHostName();
runtimePersist.setHostname("http://" + hostName + ":" + serverPort);
} else {
List<RuntimePersist> byServerNameAndHostname = runtimeRepository.findByServerNameAndHostname(serverName, hostName);
if (byServerNameAndHostname.size() == 0) {
RuntimePersist runtimePersist = new RuntimePersist(serverName, version, hostName ,
String.valueOf(serverPort), sftpPort,
hostName, RuntimePersist.STATUS.UP.toString());
if ("1".equals(isSwarm)) {
runtimePersist.setServerUrl("http://" + serverName + ":" + serverPort);
}else {
runtimePersist.setServerUrl("http://" + hostName + ":" + serverPort);
}
runtimePersist.setSftpHost(hostName);
runtimePersist.setSftpPort(sftpPort);
} catch (UnknownHostException e) {
logger.info("initCamelBusinessRoutes.getLocalHost", e);
runtimeRepository.save(runtimePersist);
}else{
RuntimePersist runtimePersist =byServerNameAndHostname.get(0);
runtimePersist.setStatus(RuntimePersist.STATUS.UP.toString());
runtimeRepository.save(runtimePersist);
}
runtimeRepository.save(runtimePersist);
}
try {
for (KieContainerResource kieContainerResource : this.server.getServerState().getResult().getContainers()){
for (KieContainerResource kieContainerResource : this.server.getServerState().getResult().getContainers()) {
// this.createContainer(container.getContainerId(),)
// this.createContainer(container.getContainerId(),)
this.createContainer(kieContainerResource.getContainerId(), kieContainerResource);
List<ContainerPojoPersist> containers = containerRepository.findByContainerId(kieContainerResource.getContainerId());
if (containers.size()==1) {
if (containers.size() == 1) {
this.initCamelBusinessRoute(containers.get(0));
}
}
} catch (Exception e) {
logger.info("initCamelBusinessRoutes", e);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
String serverName = KieServiceCommon.getKieServerID();
List<RuntimePersist> itIsMes = runtimeRepository.findByServerNameAndHostname(serverName,hostName);
if (itIsMes.size() == 1) {
RuntimePersist runtimePersist = itIsMes.get(0);
runtimePersist.setStatus(RuntimePersist.STATUS.DOWN.toString());
runtimeRepository.delete(runtimePersist);
}
}});
}
@PreDestroy
public void stopRuntime(){
String serverName =KieServiceCommon.getKieServerID();
List<RuntimePersist> itIsMes = runtimeRepository.findByServerName(serverName);
if (itIsMes.size()==1){
RuntimePersist runtimePersist =itIsMes.get(0);
runtimePersist.setStatus(RuntimePersist.STATUS.DOWN.toString());
public void setTimeStamp(){
String serverName = KieServiceCommon.getKieServerID();
List<RuntimePersist> itIsMes = runtimeRepository.findByServerNameAndHostname(serverName,hostName);
if (itIsMes.size() == 1) {
RuntimePersist runtimePersist = itIsMes.get(0);
runtimePersist.setTimeStamp(new Date());
runtimeRepository.save(runtimePersist);
}
}
public void deleteCamelBusinessRoute(String containerId) throws Exception {
if (routes.containsKey(containerId)){
DroolsRouter routeToDelete = routes.get(containerId);
if (routes.containsKey(containerId)) {
DroolsRouter routeToDelete = routes.get(containerId);
String target = routeToDelete.getRouteCollection().getRoutes().get(0).getId();
for (Route route : camelContext.getRoutes()){
if (route.getId().equals(target)){
for (Route route : camelContext.getRoutes()) {
if (route.getId().equals(target)) {
camelContext.stopRoute(route.getId());
camelContext.removeRoute(route.getId());
}
@ -200,7 +227,7 @@ public class KieServiceCommon {
this.deleteCamelBusinessRoute(projectName);
DroolsRouter droolsRouter = new DroolsRouter(camelContext, theClass, projectName, kieContainerInstance, processId);
camelContext.addRoutes(droolsRouter);
routes.put(containerId,droolsRouter);
routes.put(containerId, droolsRouter);
} finally {
if (localClassLoader != null) {
Thread.currentThread().setContextClassLoader(localClassLoader);
@ -359,7 +386,7 @@ public class KieServiceCommon {
public ServiceResponse<Void> disposeContainer(String id) {
ServiceResponse<Void> result = server.disposeContainer(id);
String serverName =KieServiceCommon.getKieServerID();
String serverName = KieServiceCommon.getKieServerID();
ContainerPojoPersist element = containerRepository.findByServerNameAndContainerId(serverName, id);
if (element != null) {
containerRepository.delete(element);

View file

@ -7,6 +7,6 @@ import org.springframework.stereotype.Component;
public class AutodeployRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
from("quartz2://myGroup/myTimerName?cron=0/30+*+*+?+*+*").to("bean:updateService?method=updateConfig()");
from("quartz2://myGroup/myTimerName?cron=0/10+*+*+?+*+*").to("bean:updateService?method=updateConfig()");
}
}

View file

@ -49,8 +49,12 @@ public class UpdateService {
boolean result = false;
runtimes.clear();
List<RuntimePersist> runtimePersists = runtimeRepository.findAll();
Map<String, String> urlMap = new HashMap<>();
for (RuntimePersist runtimePersist : runtimePersists) {
runtimes.put(runtimePersist.getServerName(), runtimePersist.duplicate());
if (urlMap.containsKey(runtimePersist.getServerName()) == false) {
urlMap.put(runtimePersist.getServerName(), runtimePersist.getServerUrl());
runtimes.put(runtimePersist.getServerName(), runtimePersist.duplicate());
}
}
List<ProjectPersist> projectPersists = projectRepository.findAll();
List<String> projectOk = new ArrayList<>();
@ -95,6 +99,14 @@ public class UpdateService {
projects.clear();
List<MappingProperties> paths = new ArrayList<>();
Collection<ProjectPersist> projectPersists = projectRepository.findAll();
Map<String, String> urlMap = new HashMap<>();
List<RuntimePersist> runtimePersists = runtimeRepository.findAll();
for (RuntimePersist runtimePersist : runtimePersists) {
if (urlMap.containsKey(runtimePersist.getServerName()) == false) {
urlMap.put(runtimePersist.getServerName(), runtimePersist.getServerUrl());
runtimes.put(runtimePersist.getServerName(), runtimePersist.duplicate());
}
}
for (ProjectPersist projectPersist : projectPersists) {
if (projectPersist.getServerNames().size() > 0) {
projects.put(projectPersist.getContainerID(), projectPersist.duplicate());
@ -103,7 +115,7 @@ public class UpdateService {
for (String serverName : projectPersist.getServerNames()) {
RuntimePersist runtimePersist = runtimes.get(serverName);
if (runtimePersist!= null) {
String hostName = runtimePersist.getHostname() + "/api/" + projectPersist.getContainerID();
String hostName = runtimePersist.getServerUrl() + "/api/" + projectPersist.getContainerID();
mappingProperties2.getDestinations().add(hostName);
if (servList==null){
servList=serverName;
@ -120,7 +132,7 @@ public class UpdateService {
mappingProperties2.setStripPath(true);
if ( mappingProperties2.getDestinations().size()>0) {
paths.add(mappingProperties2);
logger.info("Project "+projectPersist.getContainerID()+" defined on servers - "+servList);
logger.info("Project "+projectPersist.getContainerID()+" defined on servers - "+mappingProperties2.getDestinations().toString());
}else{
logger.error("Project "+projectPersist.getContainerID()+" defined on non existing server");
@ -136,16 +148,6 @@ public class UpdateService {
@PostConstruct
public void initConfig() {
List<RuntimePersist> runtimePersists = runtimeRepository.findAll();
for (RuntimePersist runtimePersist : runtimePersists) {
runtimes.put(runtimePersist.getServerName(), runtimePersist);
}
List<ProjectPersist> projectPersists = projectRepository.findAll();
for (ProjectPersist projectPersist : projectPersists) {
if (projectPersist.getServerNames().size() > 0) {
projects.put(projectPersist.getContainerID(), projectPersist.duplicate());
}
}
generateMappings();
}

View file

@ -5,6 +5,8 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
@Document
public class RuntimePersist {
public enum STATUS {
@ -18,9 +20,11 @@ public class RuntimePersist {
private String serverName;
private String version;
@Indexed
private String hostname;
private String serverUrl;
private String serverPort;
private String sftpHost;
@ -29,6 +33,11 @@ public class RuntimePersist {
private String status;
private Date creationDate;
private Date timeStamp;
public RuntimePersist(String serverName, String version, String hostname,String serverPort,String sftpPort,String sftpHost,String status) {
this.serverName = serverName;
this.version = version;
@ -37,10 +46,19 @@ public class RuntimePersist {
this.sftpPort = sftpPort;
this.sftpHost = sftpHost;
this.status=status;
this.creationDate = new Date();
}
public RuntimePersist(String serverName) {
this.serverName = serverName;
}
public RuntimePersist() {
}
public RuntimePersist duplicate(){
RuntimePersist duplicate = new RuntimePersist(serverName,version,hostname,serverPort,sftpPort,sftpHost,status);
duplicate.setServerUrl(this.serverUrl);
return duplicate;
}
@ -107,4 +125,24 @@ public class RuntimePersist {
public void setStatus(String status) {
this.status = status;
}
public String getServerUrl() {
return serverUrl;
}
public void setServerUrl(String serverUrl) {
this.serverUrl = serverUrl;
}
public Date getCreationDate() {
return creationDate;
}
public Date getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Date timeStamp) {
this.timeStamp = timeStamp;
}
}

View file

@ -10,4 +10,5 @@ import java.util.List;
public interface RuntimeRepository extends MongoRepository<RuntimePersist, String> {
public List<RuntimePersist> findByServerName(String serverName);
public List<RuntimePersist> findByServerNameAndHostname(String serverName,String hostname);
}