remove not required docs and .readthedocs.yaml
[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 import java.util.Optional;
32
33 public class DiscoverApp extends Application<DiscoverAppConfig> {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(DiscoverApp.class);
36
37     public static void main(String[] args) throws Exception {
38         new DiscoverApp().run(args);
39
40     }
41
42     private DiscoverAppConfig config;
43
44     @Override
45     public String getName() {
46         return " MicroService Bus ";
47     }
48
49     @Override
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"));
57
58
59     }
60
61     @Override
62     public void run(DiscoverAppConfig configuration, Environment environment) {
63
64         environment.jersey().register(new MicroServiceResource());
65
66         config = configuration;
67
68         initSwaggerConfig(environment, configuration);
69
70         ConfigUtil.getInstance().initConsulClientInfo(configuration);
71
72         initApiGateWayServiceListen();
73
74         ConfigUtil.getInstance().initTCP_UDP_portRange();
75
76         ConfigUtil.getInstance().initConsulRegisterMode(configuration);
77
78     }
79
80
81
82     private void initSwaggerConfig(Environment environment, DiscoverAppConfig configuration) {
83
84         environment.jersey().register(new ApiListingResource());
85         environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
86
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();
93         // 必须以"/"开头,结尾可有可无"/"
94         String basePath = simpleServerFactory.getApplicationContextPath();
95         Optional<String> optRootPath = simpleServerFactory.getJerseyRootPath();
96
97         String rootPath = optRootPath.get();        
98
99         rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
100
101         basePath = basePath.equals("/") ? rootPath : (new StringBuilder()).append(basePath).append(rootPath).toString();
102
103         LOGGER.info("getApplicationContextPath: " + basePath);
104         config.setBasePath(basePath);
105         config.setScan(true);
106     }
107
108
109
110     /**
111      * @Title initApiGateWayServiceListen
112      * @Description TODO(开启对consul中ApiGateWay服务的监听和缓存)
113      * @return void
114      */
115     private void initApiGateWayServiceListen() {
116
117         String[] consulAddress = ConfigUtil.getInstance().getConsulAddress().split(":");
118         ConsulClientApp consulClientApp = new ConsulClientApp(consulAddress[0], Integer.parseInt(consulAddress[1]));
119
120         PublishAddressWrapper.getInstance().setConsulClientApp(consulClientApp);
121         // 监听服务变化
122         // consulClientApp.startHealthNodeListen(DiscoverUtil.APIGATEWAY_SERVINCE_ALL);
123         // LOGGER.info("start monitor ApiGateWay service--" +
124         // DiscoverUtil.CONSUL_ADDRESSS+"--"+DiscoverUtil.APIGATEWAY_SERVINCE);
125
126     }
127
128
129
130 }