migrate discovery source codes from OPEN-O
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / DiscoverApp.java
1 /**
2  * Copyright 2016 ZTE, Inc. and others.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.msb.sdclient;
17
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;
24
25 import com.fasterxml.jackson.annotation.JsonInclude;
26
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;
33
34 public class DiscoverApp extends Application<DiscoverAppConfig> {
35
36         private static final Logger LOGGER = LoggerFactory
37                         .getLogger(DiscoverApp.class);
38
39         public static void main(String[] args) throws Exception {
40                 new DiscoverApp().run(args);
41
42         }
43         
44         private DiscoverAppConfig config;
45
46         @Override
47         public String getName() {
48                 return " MicroService Bus ";
49         }
50
51         @Override
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"));
59
60                 
61         }
62
63         @Override
64         public void run(DiscoverAppConfig configuration, Environment environment) {
65                 
66                 environment.jersey().register(new MicroServiceResource());
67                 
68                 config=configuration;
69                 
70                 initSwaggerConfig(environment, configuration);
71                 
72                 ConfigUtil.getInstance().initConsulClientInfo(configuration);           
73                 
74                 initApiGateWayServiceListen();
75                 
76                 ConfigUtil.getInstance().initTCP_UDP_portRange();
77                 
78                 ConfigUtil.getInstance().initConsulRegisterMode(configuration);
79         
80         }
81
82         
83
84         private void initSwaggerConfig(Environment environment,
85                                        DiscoverAppConfig configuration) {
86
87                 environment.jersey().register(new ApiListingResource());
88                 environment.getObjectMapper().setSerializationInclusion(
89                                 JsonInclude.Include.NON_NULL);
90
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
97                                 .getServerFactory();
98                 // 必须以"/"开头,结尾可有可无"/"
99                 String basePath = simpleServerFactory.getApplicationContextPath();
100                 String rootPath = simpleServerFactory.getJerseyRootPath();
101
102                 rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
103
104                 basePath = basePath.equals("/") ? rootPath : (new StringBuilder())
105                                 .append(basePath).append(rootPath).toString();
106
107                 LOGGER.info("getApplicationContextPath: " + basePath);
108                 config.setBasePath(basePath);
109                 config.setScan(true);
110         }
111
112         
113         
114  
115         
116         
117         
118         
119         
120         /** 
121         * @Title initApiGateWayServiceListen 
122         * @Description TODO(开启对consul中ApiGateWay服务的监听和缓存)       
123         * @return void    
124         */
125         private void initApiGateWayServiceListen(){
126            
127             String[] consulAddress= ConfigUtil.getInstance().getConsulAddress().split(":");
128             ConsulClientApp consulClientApp = new ConsulClientApp(consulAddress[0],Integer.parseInt(consulAddress[1]));
129             
130             PublishAddressWrapper.getInstance().setConsulClientApp(consulClientApp);
131         // 监听服务变化
132         //consulClientApp.startHealthNodeListen(DiscoverUtil.APIGATEWAY_SERVINCE_ALL);
133         //LOGGER.info("start monitor ApiGateWay service--" + DiscoverUtil.CONSUL_ADDRESSS+"--"+DiscoverUtil.APIGATEWAY_SERVINCE);
134                
135         }
136         
137         
138         
139
140 }