From d51ccbff2e62811adb917b1ded06d3988eda15b4 Mon Sep 17 00:00:00 2001 From: sunqi310 Date: Fri, 18 Nov 2016 15:56:53 +0800 Subject: TOSCA-182 Catalog service does not relay on other services when it start Change-Id: I2d37b43f2905c7259196afd7f9b11df32be3ddf3 Issue-id: TOSCA-182 Signed-off-by: sunqi310 --- .../org/openo/commontosca/catalog/CatalogApp.java | 30 +++--- .../commontosca/catalog/db/dao/DaoManager.java | 7 +- .../catalog/db/hibernate/HibernateBundleAgent.java | 91 ++++++++++++++++++ .../catalog/db/hibernate/HibernateBundleExt.java | 87 +++++++++++++++++ .../catalog/externalservice/msb/ApiRouteInfo.java | 106 +++++++++++++++++++++ .../msb/MicroserviceBusConsumer.java | 26 ++++- .../externalservice/msb/MicroserviceBusRest.java | 10 ++ .../catalog/externalservice/msb/RouteServer.java | 53 +++++++++++ .../src/main/assembly/tomcat/conf/web.xml | 2 +- 9 files changed, 388 insertions(+), 24 deletions(-) create mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleAgent.java create mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleExt.java create mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ApiRouteInfo.java create mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/RouteServer.java (limited to 'catalog-core') diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogApp.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogApp.java index 88e3b16f..1528edc7 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogApp.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogApp.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.openo.commontosca.catalog; import com.fasterxml.jackson.annotation.JsonInclude; @@ -28,10 +29,6 @@ import io.dropwizard.setup.Environment; import io.swagger.jaxrs.config.BeanConfig; import io.swagger.jaxrs.listing.ApiListingResource; -import java.util.EnumSet; - -import javax.servlet.DispatcherType; - import org.eclipse.jetty.servlets.CrossOriginFilter; import org.glassfish.jersey.media.multipart.MultiPartFeature; import org.openo.commontosca.catalog.common.Config; @@ -44,6 +41,7 @@ import org.openo.commontosca.catalog.db.entity.NodeTemplateData; import org.openo.commontosca.catalog.db.entity.PackageData; import org.openo.commontosca.catalog.db.entity.ServiceTemplateData; import org.openo.commontosca.catalog.db.entity.ServiceTemplateMappingData; +import org.openo.commontosca.catalog.db.hibernate.HibernateBundleAgent; import org.openo.commontosca.catalog.health.ConsoleHealthCheck; import org.openo.commontosca.catalog.resources.PackageResource; import org.openo.commontosca.catalog.resources.TemplateResource; @@ -51,6 +49,11 @@ import org.openo.commontosca.catalog.resources.TemplateResource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.EnumSet; +import javax.servlet.DispatcherType; + + + public class CatalogApp extends Application { @@ -64,25 +67,16 @@ public class CatalogApp extends Application { public String getName() { return "OPENO-Catalog"; } - - private final HibernateBundle bundle = - new HibernateBundle(ServiceTemplateData.class, PackageData.class, - NodeTemplateData.class, ServiceTemplateMappingData.class) { - @Override - public DataSourceFactory getDataSourceFactory(CatalogAppConfiguration configuration) { - return configuration.getDataSourceFactory(); - } - }; - + private final HibernateBundleAgent bundle = new HibernateBundleAgent(); @Override public void initialize(Bootstrap bootstrap) { bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc")); initDb(bootstrap); } - private void initDao() { - DaoManager.getInstance().setSessionFactory(bundle.getSessionFactory()); - } + // private void initDao() { + // DaoManager.getInstance().setSessionFactory(bundle.getSessionFactory()); + // } private void initDb(Bootstrap bootstrap) { bootstrap.addBundle(bundle); @@ -100,7 +94,7 @@ public class CatalogApp extends Application { MsbAddrConfig.setMsbAddress(configuration.getMsbServerAddr()); HttpServerAddrConfig.setHttpServerAddress(configuration.getHttpServerAddr()); HttpServerPathConfig.setHttpServerPath(configuration.getHttpServerPath()); - initDao(); + //initDao(); final ConsoleHealthCheck healthCheck = new ConsoleHealthCheck(configuration.getTemplate()); environment.healthChecks().register("template", healthCheck); diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/dao/DaoManager.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/dao/DaoManager.java index c7d2cc1a..cdd1f666 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/dao/DaoManager.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/dao/DaoManager.java @@ -16,7 +16,9 @@ package org.openo.commontosca.catalog.db.dao; import org.hibernate.SessionFactory; + import org.openo.commontosca.catalog.db.common.CatalogResuorceType; +import org.openo.commontosca.catalog.db.exception.CatalogResourceException; /** * DAO manager class. @@ -44,7 +46,10 @@ public class DaoManager { * @param type data type * @return BaseDao */ - public BaseDao getDao(String type) { + public BaseDao getDao(String type) throws CatalogResourceException{ + if (sessionFactory == null) { + throw new CatalogResourceException("errorMsg:database connect init faild!"); + } switch (CatalogResuorceType.getType(type)) { case SERVICETEMPLATE: return getServiceTemplateDao(); diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleAgent.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleAgent.java new file mode 100644 index 00000000..43b9333e --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleAgent.java @@ -0,0 +1,91 @@ +/** + * Copyright 2016 [ZTE] and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openo.commontosca.catalog.db.hibernate; + +import io.dropwizard.ConfiguredBundle; +import io.dropwizard.setup.Bootstrap; +import io.dropwizard.setup.Environment; + +import org.openo.commontosca.catalog.CatalogAppConfiguration; +import org.openo.commontosca.catalog.db.dao.DaoManager; +import org.openo.commontosca.catalog.db.entity.NodeTemplateData; +import org.openo.commontosca.catalog.db.entity.PackageData; +import org.openo.commontosca.catalog.db.entity.ServiceTemplateData; +import org.openo.commontosca.catalog.db.entity.ServiceTemplateMappingData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HibernateBundleAgent implements ConfiguredBundle { + + private final HibernateBundleExt bundle = new HibernateBundleExt(ServiceTemplateData.class, + PackageData.class, NodeTemplateData.class, ServiceTemplateMappingData.class); + private static final Logger LOGGER = LoggerFactory.getLogger(HibernateBundleAgent.class); + + @Override + public void run(final CatalogAppConfiguration configuration, final Environment environment) + throws Exception { + Thread thread = new Thread(new Runnable() { + int retry = 0; + boolean flag = true; + + public void run() { + while (retry < 1000) { + LOGGER.info("init hibernateBundle.retry:" + retry); + retry++; + try { + bundle.runExt(configuration, environment); + } catch (Exception e1) { + flag = false; + LOGGER.warn( + "init hibernateBundle failed, sleep 15S and try again.errorMsg:" + e1.getMessage()); + threadSleep(15000); + } + if (flag) { + LOGGER.info("init hibernateBundle success!"); + initDao(); + break; + } + } + } + }); + thread.setName("init hibernateBundle"); + thread.start(); + } + + private void initDao() { + DaoManager.getInstance().setSessionFactory(bundle.getSessionFactory()); + } + + private void threadSleep(int second) { + LOGGER.info("start sleep ...."); + try { + Thread.sleep(second); + } catch (InterruptedException error) { + LOGGER.error("thread sleep error.errorMsg:" + error.getMessage()); + } + LOGGER.info("sleep end ."); + } + + @Override + public void initialize(Bootstrap bootstrap) { + bundle.initializeExt(bootstrap); + } + // + // public SessionFactory getSessionFactory() { + // return bundle.getSessionFactory(); + // } +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleExt.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleExt.java new file mode 100644 index 00000000..20759491 --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleExt.java @@ -0,0 +1,87 @@ +/** + * Copyright 2016 [ZTE] and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openo.commontosca.catalog.db.hibernate; + +import com.google.common.collect.ImmutableList; + +import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module; + +import io.dropwizard.db.DataSourceFactory; +import io.dropwizard.hibernate.HibernateBundle; +import io.dropwizard.hibernate.SessionFactoryFactory; +import io.dropwizard.setup.Bootstrap; +import io.dropwizard.setup.Environment; + +import org.hibernate.SessionFactory; +import org.openo.commontosca.catalog.CatalogAppConfiguration; + + +public class HibernateBundleExt extends HibernateBundle { + + private static final String DEFAULT_NAME = "hibernate"; + + private SessionFactory sessionFactory; + + private final ImmutableList> entities; + private final SessionFactoryFactory sessionFactoryFactory; + + protected HibernateBundleExt(Class entity, Class... entities) { + this(ImmutableList.>builder().add(entity).add(entities).build(), + new SessionFactoryFactory()); + } + + protected HibernateBundleExt(ImmutableList> entities, + SessionFactoryFactory sessionFactoryFactory) { + super(entities, sessionFactoryFactory); + this.entities = entities; + this.sessionFactoryFactory = sessionFactoryFactory; + } + + public final void initializeExt(Bootstrap bootstrap) { + bootstrap.getObjectMapper().registerModule(createHibernate4Module()); + } + + /** + * Override to configure the {@link Hibernate4Module}. + */ + protected Hibernate4Module createHibernate4Module() { + return new Hibernate4Module(); + } + + protected String name() { + return DEFAULT_NAME; + } + + public final void runExt(CatalogAppConfiguration configuration, Environment environment) + throws Exception { + final DataSourceFactory dbConfig = getDataSourceFactory(configuration); + this.sessionFactory = + sessionFactoryFactory.build(this, environment, dbConfig, entities, name()); + + } + + public SessionFactory getSessionFactory() { + return sessionFactory; + } + + + @Override + public DataSourceFactory getDataSourceFactory(CatalogAppConfiguration configuration) { + return configuration.getDataSourceFactory(); + } + +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ApiRouteInfo.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ApiRouteInfo.java new file mode 100644 index 00000000..d98af250 --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ApiRouteInfo.java @@ -0,0 +1,106 @@ +/** + * Copyright 2016 [ZTE] and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openo.commontosca.catalog.externalservice.msb; + +import java.io.Serializable; + +public class ApiRouteInfo implements Serializable { + private static final long serialVersionUID = 1L; + private String serviceName; + private String version = ""; + private String url; + private String apiJson = ""; + private String apiJsonType = "1"; + private String metricsUrl = ""; + private String control = "0"; + private String status = "1"; + + private RouteServer []servers; + + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getApiJson() { + return apiJson; + } + + public void setApiJson(String apiJson) { + this.apiJson = apiJson; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public RouteServer[] getServers() { + return servers; + } + + public void setServers(RouteServer[] servers) { + this.servers = servers; + } + + public String getApiJsonType() { + return apiJsonType; + } + + public void setApiJsonType(String apiJsonType) { + this.apiJsonType = apiJsonType; + } + + public String getMetricsUrl() { + return metricsUrl; + } + + public void setMetricsUrl(String metricsUrl) { + this.metricsUrl = metricsUrl; + } + + public String getControl() { + return control; + } + + public void setControl(String control) { + this.control = control; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusConsumer.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusConsumer.java index 33e17009..6732d6e5 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusConsumer.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusConsumer.java @@ -13,12 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.openo.commontosca.catalog.externalservice.msb; import com.eclipsesource.jaxrs.consumer.ConsumerFactory; import org.glassfish.jersey.client.ClientConfig; -import org.openo.commontosca.catalog.db.util.CatalogDbUtil; + import org.openo.commontosca.catalog.common.Config; +import org.openo.commontosca.catalog.db.util.CatalogDbUtil; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,6 +31,7 @@ public class MicroserviceBusConsumer { /** * register service to MSB. + * * @param entity ServiceRegisterEntity * @return boolean */ @@ -35,9 +39,8 @@ public class MicroserviceBusConsumer { ClientConfig config = new ClientConfig(); LOG.info("microservice register body:" + CatalogDbUtil.objectToString(entity)); try { - MicroserviceBusRest resourceserviceproxy = - ConsumerFactory.createConsumer(Config.getConfigration().getMsbServerAddr(), config, - MicroserviceBusRest.class); + MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer( + Config.getConfigration().getMsbServerAddr(), config, MicroserviceBusRest.class); resourceserviceproxy.registerServce("false", entity); } catch (Exception e1) { LOG.error("microservice register failed!" + e1.getMessage()); @@ -45,4 +48,19 @@ public class MicroserviceBusConsumer { } return true; } + + public static ApiRouteInfo queryApiRouteInfo(String serviceName, String version) { + ClientConfig config = new ClientConfig(); + LOG.info("microservice register body:" + "serviceName:" + serviceName + " version:" + version); + ApiRouteInfo apiRouteInfo = null; + try { + MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer( + Config.getConfigration().getMsbServerAddr(), config, MicroserviceBusRest.class); + apiRouteInfo = resourceserviceproxy.queryApiRouteInfo(serviceName, version); + } catch (Exception e1) { + LOG.error("query api route failed!" + e1.getMessage()); + + } + return apiRouteInfo; + } } diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusRest.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusRest.java index 11c23497..51a5971d 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusRest.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusRest.java @@ -13,11 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.openo.commontosca.catalog.externalservice.msb; import javax.ws.rs.Consumes; +import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; @@ -31,4 +34,11 @@ public interface MicroserviceBusRest { @Produces(MediaType.APPLICATION_JSON) public ServiceRegisterEntity registerServce(@QueryParam("createOrUpdate") String createOrUpdate, ServiceRegisterEntity entity) throws Exception; + + @Path("") + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public ApiRouteInfo queryApiRouteInfo(@PathParam("serviceName") String serviceName, + @PathParam("version") String version ) throws Exception; } diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/RouteServer.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/RouteServer.java new file mode 100644 index 00000000..8aa22a0d --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/RouteServer.java @@ -0,0 +1,53 @@ +/** + * Copyright 2016 [ZTE] and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openo.commontosca.catalog.externalservice.msb; + +import java.io.Serializable; + +public class RouteServer implements Serializable { + private static final long serialVersionUID = 1L; + private String ip; + private int weight; + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + + + public int getWeight() { + return weight; + } + + public void setWeight(int weight) { + this.weight = weight; + } + + public RouteServer() { + + } + + public RouteServer(String ip) { + this.ip = ip; + this.weight = 0; + } + +} diff --git a/catalog-core/httpserver/src/main/assembly/tomcat/conf/web.xml b/catalog-core/httpserver/src/main/assembly/tomcat/conf/web.xml index acaeaa50..9e4756d3 100644 --- a/catalog-core/httpserver/src/main/assembly/tomcat/conf/web.xml +++ b/catalog-core/httpserver/src/main/assembly/tomcat/conf/web.xml @@ -109,7 +109,7 @@ listings - false + true 1 -- cgit 1.2.3-korg