2 * Copyright 2016 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.msb.sdclient;
18 import org.onap.msb.sdclient.resources.MicroServiceResource;
19 import org.onap.msb.sdclient.wrapper.ConsulClientApp;
20 import org.onap.msb.sdclient.wrapper.PublishAddressWrapper;
21 import org.onap.msb.sdclient.wrapper.util.ConfigUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.fasterxml.jackson.annotation.JsonInclude;
27 import io.dropwizard.Application;
28 import io.dropwizard.server.SimpleServerFactory;
29 import io.dropwizard.setup.Bootstrap;
30 import io.dropwizard.setup.Environment;
31 import io.swagger.jaxrs.config.BeanConfig;
32 import io.swagger.jaxrs.listing.ApiListingResource;
34 public class DiscoverApp extends Application<DiscoverAppConfig> {
36 private static final Logger LOGGER = LoggerFactory
37 .getLogger(DiscoverApp.class);
39 public static void main(String[] args) throws Exception {
40 new DiscoverApp().run(args);
44 private DiscoverAppConfig config;
47 public String getName() {
48 return " MicroService Bus ";
52 public void initialize(Bootstrap<DiscoverAppConfig> bootstrap) {
53 // bootstrap.addBundle(new AssetsBundle("/iui-metrics",
54 // "/iui/microservices/metrics", "index.html", "iui-metrics"));
55 // bootstrap.addBundle(new AssetsBundle("/iui-discover",
56 // "/iui/microservices", "index.html", "iui-microservices"));
57 // bootstrap.addBundle(new AssetsBundle("/iui-discover", "/iui",
58 // "index.html", "iui"));
64 public void run(DiscoverAppConfig configuration, Environment environment) {
66 environment.jersey().register(new MicroServiceResource());
70 initSwaggerConfig(environment, configuration);
72 ConfigUtil.getInstance().initConsulClientInfo(configuration);
74 initApiGateWayServiceListen();
76 ConfigUtil.getInstance().initTCP_UDP_portRange();
78 ConfigUtil.getInstance().initConsulRegisterMode(configuration);
84 private void initSwaggerConfig(Environment environment,
85 DiscoverAppConfig configuration) {
87 environment.jersey().register(new ApiListingResource());
88 environment.getObjectMapper().setSerializationInclusion(
89 JsonInclude.Include.NON_NULL);
91 BeanConfig config = new BeanConfig();
92 config.setTitle("Service Discovery RESTful API");
93 config.setVersion("1.0.0");
94 config.setResourcePackage("org.onap.msb.sdclient.resources");
95 // 设置swagger里面访问rest api时的basepath
96 SimpleServerFactory simpleServerFactory = (SimpleServerFactory) configuration
99 String basePath = simpleServerFactory.getApplicationContextPath();
100 String rootPath = simpleServerFactory.getJerseyRootPath();
102 rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
104 basePath = basePath.equals("/") ? rootPath : (new StringBuilder())
105 .append(basePath).append(rootPath).toString();
107 LOGGER.info("getApplicationContextPath: " + basePath);
108 config.setBasePath(basePath);
109 config.setScan(true);
121 * @Title initApiGateWayServiceListen
122 * @Description TODO(开启对consul中ApiGateWay服务的监听和缓存)
125 private void initApiGateWayServiceListen(){
127 String[] consulAddress= ConfigUtil.getInstance().getConsulAddress().split(":");
128 ConsulClientApp consulClientApp = new ConsulClientApp(consulAddress[0],Integer.parseInt(consulAddress[1]));
130 PublishAddressWrapper.getInstance().setConsulClientApp(consulClientApp);
132 //consulClientApp.startHealthNodeListen(DiscoverUtil.APIGATEWAY_SERVINCE_ALL);
133 //LOGGER.info("start monitor ApiGateWay service--" + DiscoverUtil.CONSUL_ADDRESSS+"--"+DiscoverUtil.APIGATEWAY_SERVINCE);