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