Fix java check style issue
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / DiscoverApp.java
1 /**
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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
12  * the License.
13  */
14 package org.onap.msb.sdclient;
15
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;
22
23 import com.fasterxml.jackson.annotation.JsonInclude;
24
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
32 public class DiscoverApp extends Application<DiscoverAppConfig> {
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(DiscoverApp.class);
35
36     public static void main(String[] args) throws Exception {
37         new DiscoverApp().run(args);
38
39     }
40
41     private DiscoverAppConfig config;
42
43     @Override
44     public String getName() {
45         return " MicroService Bus ";
46     }
47
48     @Override
49     public void initialize(Bootstrap<DiscoverAppConfig> bootstrap) {
50         // bootstrap.addBundle(new AssetsBundle("/iui-metrics",
51         // "/iui/microservices/metrics", "index.html", "iui-metrics"));
52         // bootstrap.addBundle(new AssetsBundle("/iui-discover",
53         // "/iui/microservices", "index.html", "iui-microservices"));
54         // bootstrap.addBundle(new AssetsBundle("/iui-discover", "/iui",
55         // "index.html", "iui"));
56
57
58     }
59
60     @Override
61     public void run(DiscoverAppConfig configuration, Environment environment) {
62
63         environment.jersey().register(new MicroServiceResource());
64
65         config = configuration;
66
67         initSwaggerConfig(environment, configuration);
68
69         ConfigUtil.getInstance().initConsulClientInfo(configuration);
70
71         initApiGateWayServiceListen();
72
73         ConfigUtil.getInstance().initTCP_UDP_portRange();
74
75         ConfigUtil.getInstance().initConsulRegisterMode(configuration);
76
77     }
78
79
80
81     private void initSwaggerConfig(Environment environment, DiscoverAppConfig configuration) {
82
83         environment.jersey().register(new ApiListingResource());
84         environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
85
86         BeanConfig config = new BeanConfig();
87         config.setTitle("Service Discovery RESTful API");
88         config.setVersion("1.0.0");
89         config.setResourcePackage("org.onap.msb.sdclient.resources");
90         // 设置swagger里面访问rest api时的basepath
91         SimpleServerFactory simpleServerFactory = (SimpleServerFactory) configuration.getServerFactory();
92         // 必须以"/"开头,结尾可有可无"/"
93         String basePath = simpleServerFactory.getApplicationContextPath();
94         String rootPath = simpleServerFactory.getJerseyRootPath();
95
96         rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
97
98         basePath = basePath.equals("/") ? rootPath : (new StringBuilder()).append(basePath).append(rootPath).toString();
99
100         LOGGER.info("getApplicationContextPath: " + basePath);
101         config.setBasePath(basePath);
102         config.setScan(true);
103     }
104
105
106
107     /**
108      * @Title initApiGateWayServiceListen
109      * @Description TODO(开启对consul中ApiGateWay服务的监听和缓存)
110      * @return void
111      */
112     private void initApiGateWayServiceListen() {
113
114         String[] consulAddress = ConfigUtil.getInstance().getConsulAddress().split(":");
115         ConsulClientApp consulClientApp = new ConsulClientApp(consulAddress[0], Integer.parseInt(consulAddress[1]));
116
117         PublishAddressWrapper.getInstance().setConsulClientApp(consulClientApp);
118         // 监听服务变化
119         // consulClientApp.startHealthNodeListen(DiscoverUtil.APIGATEWAY_SERVINCE_ALL);
120         // LOGGER.info("start monitor ApiGateWay service--" +
121         // DiscoverUtil.CONSUL_ADDRESSS+"--"+DiscoverUtil.APIGATEWAY_SERVINCE);
122
123     }
124
125
126
127 }