1 /*******************************************************************************
2 * Copyright 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
13 ******************************************************************************/
14 package org.onap.msb.sdk.discovery;
16 import org.apache.commons.lang3.StringUtils;
17 import org.onap.msb.sdk.discovery.common.RouteConst;
18 import org.onap.msb.sdk.discovery.common.RouteException;
19 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
20 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
21 import org.onap.msb.sdk.discovery.entity.NodeAddress;
22 import org.onap.msb.sdk.discovery.entity.RouteResult;
23 import org.onap.msb.sdk.discovery.util.HttpClientUtil;
24 import org.onap.msb.sdk.discovery.util.JacksonJsonUtil;
25 import org.onap.msb.sdk.discovery.util.MsbUtil;
26 import org.onap.msb.sdk.discovery.util.RegExpTestUtil;
30 public class MSBService {
33 * @Title queryMicroServiceInfo
34 * @Description TODO(查询单个微服务)
35 * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
36 * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
37 * @param version 版本号[若无版本号,传空字符串]
38 * @throws RouteException
39 * @return MicroServiceFullInfo
41 public MicroServiceFullInfo queryMicroServiceInfo(String msbAddress, String serviceName,
42 String version) throws RouteException {
44 MsbUtil.checkServiceName(serviceName);
45 version=MsbUtil.checkVersion(version);
47 MicroServiceFullInfo microServiceInfo = null;
50 String apiRouteUrl = (new StringBuilder().append("http://").append(msbAddress)
51 .append(RouteConst.MSB_ROUTE_URL).append("/").append(serviceName).append("/version/")
52 .append(version).append("?ifPassStatus=true")).toString();
54 String resultJson = HttpClientUtil.httpGet(apiRouteUrl);
56 (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);
58 return microServiceInfo;
63 * @Title registerMicroServiceInfo
64 * @Description TODO(注册微服务-默认追加方式)
65 * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
66 * @param microServiceInfo 微服务注册实体类
67 * @throws RouteException
68 * @return MicroServiceFullInfo
70 public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
71 MicroServiceInfo microServiceInfo) throws RouteException {
72 return this.registerMicroServiceInfo(msbAddress, microServiceInfo, true);
77 * @Title registerMicroServiceInfo
78 * @Description TODO(注册微服务)
79 * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
80 * @param microServiceInfo 微服务注册实体类
81 * @param createOrUpdate true:新增或追加更新 ,false:重新添加
82 * @throws RouteException
83 * @return MicroServiceFullInfo
85 public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
86 MicroServiceInfo microServiceInfo, boolean createOrUpdate) throws RouteException {
89 if (StringUtils.isBlank(microServiceInfo.getServiceName())
90 || StringUtils.isBlank(microServiceInfo.getProtocol())
91 || microServiceInfo.getNodes().size() == 0) {
93 throw new RouteException("register MicroServiceInfo FAIL: Some MicroServiceInfo's required fields are empty","DATA_FORMAT_ERROR");
99 if (StringUtils.isNotBlank(microServiceInfo.getVersion())) {
100 if (!RegExpTestUtil.versionRegExpTest(microServiceInfo.getVersion())) {
101 throw new RouteException("register MicroServiceInfo FAIL:version is not a valid format","DATA_FORMAT_ERROR");
108 if (!RouteConst.checkExistProtocol(microServiceInfo.getProtocol().trim())) {
109 throw new RouteException("register MicroServiceInfo FAIL:Protocol is wrong,value range:("+ RouteConst.listProtocol() + ")", "DATA_FORMAT_ERROR");
115 String apiRouteJson = JacksonJsonUtil.beanToJson(microServiceInfo);
118 (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
119 .append("?createOrUpdate=").append(createOrUpdate)).toString();
121 String resultJson = HttpClientUtil.httpPostWithJSON(apiRouteUrl, apiRouteJson);
123 MicroServiceFullInfo microServiceFullInfo =
124 (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);
126 return microServiceFullInfo;
130 * @Title cancelMicroServiceInfo
131 * @Description TODO(注销全部微服务)
132 * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
133 * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
134 * @param version 版本号[若无版本号,传空字符串]
135 * @throws RouteException
136 * @return RouteResult
138 public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version)
139 throws RouteException {
140 RouteResult result = new RouteResult();
142 MsbUtil.checkServiceName(serviceName);
143 version=MsbUtil.checkVersion(version);
148 (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
149 .append("/").append(serviceName).append("/version/").append(version).toString();
151 HttpClientUtil.delete(url, null);
154 result.setResult(RouteConst.REQUEST_SUCCESS);
155 result.setInfo("cancel MicroServiceInfo success");
162 * @Title cancelMicroServiceInfo
163 * @Description TODO(注销单个微服务)
164 * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
165 * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
166 * @param version 版本号[若无版本号,传空字符串]
169 * @throws RouteException
170 * @return RouteResult
172 public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version,
173 String ip, String port) throws RouteException {
175 RouteResult result = new RouteResult();
177 MsbUtil.checkServiceName(serviceName);
178 version=MsbUtil.checkVersion(version);
180 MsbUtil.checkHost(ip,port);
186 (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
187 .append("/").append(serviceName).append("/version/").append(version).append("/nodes/")
188 .append(ip).append("/").append(port).toString();
190 HttpClientUtil.delete(url, null);
193 result.setResult(RouteConst.REQUEST_SUCCESS);
194 result.setInfo("cancel MicroServiceInfo success");
201 * @Title healthCheckbyTTL
202 * @Description TODO(请求服务实例TTL健康检查)
204 * @param serviceName 服务名
205 * @param version 版本号[若无版本号,传空字符串]
208 * @throws RouteException
211 public NodeAddress healthCheckbyTTL(String msbAddress, String serviceName, String version,
212 String ip, String port) throws RouteException {
214 MsbUtil.checkServiceName(serviceName);
215 version=MsbUtil.checkVersion(version);
216 MsbUtil.checkHost(ip,port);
219 NodeAddress checkNode = new NodeAddress(ip, port);
222 String healthCheckJson = JacksonJsonUtil.beanToJson(checkNode);
224 String healthCheckUrl =
225 (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
226 .append("/").append(serviceName).append("/version/").append(version).append("/ttl"))
229 HttpClientUtil.httpPutWithJSON(healthCheckUrl, healthCheckJson);