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.httpclient.msb;
16 import java.util.concurrent.Callable;
18 import org.onap.msb.sdk.discovery.MSBService;
19 import org.onap.msb.sdk.discovery.common.RouteException;
20 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
21 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
22 import org.onap.msb.sdk.discovery.entity.NodeAddress;
23 import org.onap.msb.sdk.discovery.entity.RouteResult;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 public class MSBServiceWrapperClient {
30 private static final Logger logger = LoggerFactory.getLogger(MSBServiceWrapperClient.class);
32 private String msbSvrAddress;
34 private MSBService msbService = new MSBService();
36 private String nameSpace;
38 public MSBServiceWrapperClient(String msbSvrIp, int msbSvrPort) {
40 this.msbSvrAddress = msbSvrIp + ":" + msbSvrPort;
42 logger.info("msb service info:msbSvrAddress:{},nameSpace:{}", this.msbSvrAddress,
47 public MicroServiceFullInfo queryMicroServiceInfo(String serviceName, String version)
48 throws RouteException {
50 return invokeMsb(() -> msbService.queryMicroServiceInfo(msbSvrAddress, serviceName, version));
55 private <V> V invokeMsb(Callable<V> callable) throws RouteException {
57 return callable.call();
58 } catch (Exception e) {
59 logger.error("msb service info:msbSvrAddress:" + this.msbSvrAddress, e);
60 if (e instanceof RouteException) {
61 throw (RouteException) e;
63 throw new RuntimeException(e);
71 * 注册微服务-默认追加方式,在msb上注册服务信息,服务的信息都可以填充在MicroServiceInfo上,包括:租户的信息等
73 * @param microServiceInfo 微服务注册实体类
74 * @throws RouteException
75 * @return MicroServiceFullInfo
77 public MicroServiceFullInfo registerMicroServiceInfo(MicroServiceInfo msinfo)
78 throws RouteException {
80 return invokeMsb(() -> msbService.registerMicroServiceInfo(msbSvrAddress, msinfo));
86 * 注册微服务,在msb上注册服务信息,服务的信息都可以填充在MicroServiceInfo上,包括:租户的信息等
88 * @param microServiceInfo 微服务注册实体类
89 * @param createOrUpdate true:新增或追加更新 ,false:重新添加
90 * @throws RouteException
91 * @return MicroServiceFullInfo
93 public MicroServiceFullInfo registerMicroServiceInfo(MicroServiceInfo msinfo,
94 boolean createOrUpdate) throws RouteException {
97 () -> msbService.registerMicroServiceInfo(msbSvrAddress, msinfo, createOrUpdate));
103 * @Title cancelMicroServiceInfo
104 * @Description TODO(注销全部微服务)
105 * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
106 * @param version 版本号[若无版本号,传空字符串]
107 * @throws RouteException
108 * @return RouteResult
110 public RouteResult cancelMicroServiceInfo(String serviceName, String version)
111 throws RouteException {
112 return invokeMsb(() -> msbService.cancelMicroServiceInfo(msbSvrAddress, serviceName, version));
120 * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
121 * @param version 版本号[若无版本号,传空字符串]
124 * @throws RouteException
125 * @return RouteResult
128 public RouteResult cancelMicroServiceInfo(String serviceName, String version, String ip,
129 String port) throws RouteException {
132 () -> msbService.cancelMicroServiceInfo(msbSvrAddress, serviceName, version, ip, port));
136 public String getMsbSvrAddress() {
137 return msbSvrAddress;
143 * 请求服务实例TTL健康检查,默认使用本租户的信息
145 * @param serviceName 服务名
146 * @param version 版本号[若无版本号,传空字符串]
149 * @throws RouteException
152 public NodeAddress healthCheckbyTTL(String serviceName, String version, String ip, String port)
153 throws RouteException {
156 () -> msbService.healthCheckbyTTL(msbSvrAddress, serviceName, version, ip, port));