diff options
author | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-09-11 02:57:01 +0000 |
---|---|---|
committer | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-09-11 02:57:05 +0000 |
commit | 6f03acfb7ab96ba2363eabf2b5cc0cf63e31fe4f (patch) | |
tree | 517dd53949307adf9eb3ec4145ad7cd361587a10 /apiroute/apiroute-service/src/main | |
parent | baa01e13c73002978a9d19756a5e77e6830b8287 (diff) |
Route service requests to discovery service
Route service registration request to discovery service
Route service discovery request to discovery service
Issue-Id: MSB-73
Change-Id: I88a37a075245f7d6923d96e2b59307d2b1caf4dc
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
Diffstat (limited to 'apiroute/apiroute-service/src/main')
2 files changed, 192 insertions, 15 deletions
diff --git a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/resources/MicroServiceResource.java b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/resources/MicroServiceResource.java index be33613..d7ed83d 100644 --- a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/resources/MicroServiceResource.java +++ b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/resources/MicroServiceResource.java @@ -13,7 +13,6 @@ ******************************************************************************/ package org.onap.msb.apiroute.resources; -import java.net.URI; import java.util.List; import javax.servlet.http.HttpServletRequest; @@ -32,12 +31,23 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.apache.http.HttpStatus; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.onap.msb.apiroute.api.DiscoverInfo; import org.onap.msb.apiroute.api.MicroServiceFullInfo; import org.onap.msb.apiroute.api.exception.ExtendedInternalServerErrorException; import org.onap.msb.apiroute.health.ConsulLinkHealthCheck; import org.onap.msb.apiroute.health.RedisHealthCheck; import org.onap.msb.apiroute.wrapper.MicroServiceWrapper; +import org.onap.msb.apiroute.wrapper.util.ConfigUtil; +import org.onap.msb.apiroute.wrapper.util.HttpClientUtil; +import org.onap.msb.apiroute.wrapper.util.JacksonJsonUtil; import org.onap.msb.apiroute.wrapper.util.MicroServiceUtil; +import org.onap.msb.apiroute.wrapper.util.RouteUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,7 +63,6 @@ import io.swagger.annotations.ApiResponses; // @Api(tags = {"MSB-Service Resource"}) @Produces(MediaType.APPLICATION_JSON) public class MicroServiceResource { - private static final Logger LOGGER = LoggerFactory.getLogger(MicroServiceResource.class); @Context @@ -71,7 +80,7 @@ public class MicroServiceResource { return MicroServiceWrapper.getInstance().getAllMicroServiceInstances(); } - /*@POST + @POST @Path("/") @ApiOperation(value = "add one microservice ", code = HttpStatus.SC_CREATED, response = MicroServiceFullInfo.class) @ApiResponses(value = { @@ -91,14 +100,60 @@ public class MicroServiceResource { required = false) @QueryParam("createOrUpdate") @DefaultValue("true") boolean createOrUpdate, @ApiParam(value = "port", required = false) @QueryParam("port") @DefaultValue("") String port) { - String ip = MicroServiceUtil.getRealIp(request); + MicroServiceFullInfo microServiceFullInfo = + createRegistrationMicroserviceInfo(microServiceInfo, request, createOrUpdate, port); + String registrationUrl = getUrlForRegistration(); + Response response = routeRegistration2DiscoveryService(microServiceFullInfo, registrationUrl); + return response; + } + + /** + * @param microServiceFullInfo + * @param registrationUrl + * @return + */ + private Response routeRegistration2DiscoveryService(MicroServiceFullInfo microServiceFullInfo, + String registrationUrl) { + CloseableHttpClient httpClient = null; + CloseableHttpResponse httpResponse = null; + try { + HttpPost httpPost = HttpClientUtil.createHttpPost(registrationUrl, + JacksonJsonUtil.beanToJson(microServiceFullInfo)); + httpClient = HttpClients.createDefault(); + httpResponse = httpClient.execute(httpPost); + String jsonString = EntityUtils.toString(httpResponse.getEntity()); + return Response.status(httpResponse.getStatusLine().getStatusCode()).entity(jsonString).build(); + } catch (Exception e) { + throw new ExtendedInternalServerErrorException(e.getMessage()); + } finally { + HttpClientUtil.closeHttpClient(httpClient); + HttpClientUtil.closeHttpResponse(httpResponse); + } + } + + /** + * @return + */ + private String getUrlForRegistration() { + DiscoverInfo discoverInfo = ConfigUtil.getInstance().getDiscoverInfo(); + String registrationUrl = new StringBuilder().append("http://").append(discoverInfo) + .append(RouteUtil.MSB_ROUTE_URL).toString(); + return registrationUrl; + } + /** + * @param microServiceInfo + * @param request + * @param createOrUpdate + * @param port + * @return + */ + private MicroServiceFullInfo createRegistrationMicroserviceInfo(MicroServiceFullInfo microServiceInfo, + HttpServletRequest request, boolean createOrUpdate, String port) { + String ip = MicroServiceUtil.getRealIp(request); MicroServiceFullInfo microServiceFullInfo = MicroServiceWrapper.getInstance() .saveMicroServiceInstance(microServiceInfo, createOrUpdate, ip, port); - URI returnURI = uriInfo.getAbsolutePathBuilder() - .path("/" + microServiceInfo.getServiceName() + "/version/" + microServiceInfo.getVersion()) - .build(); - return Response.created(returnURI).entity(microServiceFullInfo).build(); + return microServiceFullInfo; } @@ -115,14 +170,44 @@ public class MicroServiceResource { response = String.class)}) @Produces(MediaType.APPLICATION_JSON) @Timed - public MicroServiceFullInfo getMicroService( + public Response getMicroService( @ApiParam(value = "microservice serviceName") @PathParam("serviceName") String serviceName, @ApiParam(value = "microservice version,if the version is empty, please enter \"null\"") @PathParam("version") @DefaultValue("") String version) { + String discoveryUrl = getUrlForDiscovery(serviceName, version); + return routeDiscovery2DiscoveryService(discoveryUrl); + } + /** + * @param discoveryUrl + * @return + */ + private Response routeDiscovery2DiscoveryService(String discoveryUrl) { + CloseableHttpClient httpClient = null; + CloseableHttpResponse httpResponse = null; + try { + HttpGet httpGet = HttpClientUtil.createHttpGet(discoveryUrl); + httpClient = HttpClients.createDefault(); + httpResponse = httpClient.execute(httpGet); + String jsonString = EntityUtils.toString(httpResponse.getEntity()); + return Response.status(httpResponse.getStatusLine().getStatusCode()).entity(jsonString).build(); + } catch (Exception e) { + throw new ExtendedInternalServerErrorException(e.getMessage()); + } finally { + HttpClientUtil.closeHttpClient(httpClient); + HttpClientUtil.closeHttpResponse(httpResponse); + } + } - return MicroServiceWrapper.getInstance().getMicroServiceInstance(serviceName, version); - - + /** + * @param serviceName + * @param version + * @return + */ + private String getUrlForDiscovery(String serviceName, String version) { + DiscoverInfo discoverInfo = ConfigUtil.getInstance().getDiscoverInfo(); + String discoveryUrl = new StringBuilder().append("http://").append(discoverInfo).append(RouteUtil.MSB_ROUTE_URL) + .append("/").append(serviceName).append("/version/").append(version).toString(); + return discoveryUrl; } @PUT @@ -251,7 +336,5 @@ public class MicroServiceResource { } return Response.ok("apigateway healthy check:ok").build(); - }*/ - - + } } diff --git a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java index cb80159..163387f 100644 --- a/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java +++ b/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/util/HttpClientUtil.java @@ -14,13 +14,22 @@ package org.onap.msb.apiroute.wrapper.util; import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,6 +40,91 @@ public class HttpClientUtil { private static int connectionTimeOut = 2 * 1000; + public static HttpPost createHttpPost(String url, String params){ + HttpPost httpPost = new HttpPost(url); + httpPost.addHeader("Content-type", "application/json; charset=utf-8"); + httpPost.setHeader("Accept", "application/json"); + httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8"))); + return httpPost; + } + + public static HttpGet createHttpGet(String url){ + HttpGet httpGet = new HttpGet(url); + httpGet.addHeader("Content-type", "application/json; charset=utf-8"); + httpGet.setHeader("Accept", "application/json"); + return httpGet; + } + + public static void closeHttpClient(CloseableHttpClient httpClient) { + if (httpClient != null) { + try { + httpClient.close(); + } catch (IOException e) { + logger.error(httpClient + ":close httpClient faild"); + } + } + } + + public static void closeHttpResponse(CloseableHttpResponse response) { + if (response != null) { + try { + response.close(); + } catch (IOException e) { + logger.error(response + ":close response faild"); + } + } + } + + public static CloseableHttpResponse httpGetWithResponse(String url) throws Exception { + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(url); + httpGet.addHeader("Content-type", "application/json; charset=utf-8"); + httpGet.setHeader("Accept", "application/json"); + try { + return httpClient.execute(httpGet); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + logger.error(url + ":close httpClient faild"); + } + } + } + + public static void delete(String url, String parameter) throws Exception { + String result = null; + String baseUrl; + if (parameter != null) { + List<NameValuePair> params = new ArrayList<NameValuePair>(); + params.add(new BasicNameValuePair("serviceName", parameter)); + baseUrl = url + "?" + URLEncodedUtils.format(params, "UTF-8"); + } else { + baseUrl = url; + } + + CloseableHttpClient httpClient = HttpClients.createDefault();; + try { + + HttpDelete httpDelete = new HttpDelete(baseUrl); + CloseableHttpResponse res = httpClient.execute(httpDelete); + + if (res.getStatusLine().getStatusCode() != 200) { + throw new Exception("delete fail"); + } + + res.close(); + } catch (IOException e) { + String errorMsg = baseUrl + ":delete connect faild"; + } finally { + try { + httpClient.close(); + } catch (IOException e) { + String errorMsg = baseUrl + ":close httpClient faild"; + } + } + + } + public static String httpGet(String url) { String result = null; |