2 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.msb.wrapper.consul;
19 import static org.openo.msb.wrapper.consul.util.ClientUtil.response;
21 import java.util.List;
23 import javax.ws.rs.client.WebTarget;
24 import javax.ws.rs.core.GenericType;
26 import org.openo.msb.wrapper.consul.async.ConsulResponseCallback;
27 import org.openo.msb.wrapper.consul.model.ConsulResponse;
28 import org.openo.msb.wrapper.consul.model.health.ServiceHealth;
29 import org.openo.msb.wrapper.consul.option.CatalogOptions;
30 import org.openo.msb.wrapper.consul.option.QueryOptions;
33 * HTTP Client for /v1/health/ endpoints.
35 public class HealthClient {
38 private static final GenericType<List<ServiceHealth>> TYPE_SERVICE_HEALTH_LIST =
39 new GenericType<List<ServiceHealth>>() {};
41 private final WebTarget webTarget;
44 * Constructs an instance of this class.
46 * @param webTarget The {@link javax.ws.rs.client.WebTarget} to base requests from.
48 HealthClient(WebTarget webTarget) {
49 this.webTarget = webTarget;
55 * Retrieves the healthchecks for all healthy service instances.
57 * GET /v1/health/service/{service}?passing
59 * @param service The service to query.
60 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
61 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
63 public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service) {
64 return getHealthyServiceInstances(service, null, QueryOptions.BLANK);
68 * Retrieves the healthchecks for all healthy service instances in a given datacenter.
70 * GET /v1/health/service/{service}?dc={datacenter}&passing
72 * @param service The service to query.
73 * @param catalogOptions The catalog specific options to use.
74 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
75 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
77 public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service, CatalogOptions catalogOptions) {
78 return getHealthyServiceInstances(service, catalogOptions, QueryOptions.BLANK);
82 * Retrieves the healthchecks for all healthy service instances with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
84 * GET /v1/health/service/{service}?passing
86 * @param service The service to query.
87 * @param queryOptions The Query Options to use.
88 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
89 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
91 public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service, QueryOptions queryOptions) {
92 return getHealthyServiceInstances(service, null, queryOptions);
96 * Retrieves the healthchecks for all healthy service instances in a given datacenter with
97 * {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
99 * GET /v1/health/service/{service}?dc={datacenter}&passing
101 * @param service The service to query.
102 * @param catalogOptions The catalog specific options to use.
103 * @param queryOptions The Query Options to use.
104 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
105 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
107 public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service, CatalogOptions catalogOptions,
108 QueryOptions queryOptions) {
109 return response(webTarget.path("service").path(service).queryParam("passing", "true"),
110 catalogOptions, queryOptions, TYPE_SERVICE_HEALTH_LIST);
114 * Asynchronously retrieves the healthchecks for all healthy service instances in a given
115 * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
117 * GET /v1/health/service/{service}?dc={datacenter}&passing
121 * @param service The service to query.
122 * @param catalogOptions The catalog specific options to use.
123 * @param queryOptions The Query Options to use.
124 * @param callback Callback implemented by callee to handle results.
126 public void getHealthyServiceInstances(String service, CatalogOptions catalogOptions,
127 QueryOptions queryOptions,
128 ConsulResponseCallback<List<ServiceHealth>> callback) {
129 response(webTarget.path("service").path(service).queryParam("passing", "true"),
130 catalogOptions, queryOptions, TYPE_SERVICE_HEALTH_LIST, callback);
134 * Asynchronously retrieves the healthchecks for all healthy service instances in a given
135 * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
137 * GET /v1/health/service/{service}?dc={datacenter}&passing
141 * @param service The service to query.
142 * @param queryOptions The Query Options to use.
143 * @param callback Callback implemented by callee to handle results.
145 public void getHealthyServiceInstances(String service, QueryOptions queryOptions,
146 ConsulResponseCallback<List<ServiceHealth>> callback) {
147 response(webTarget.path("service").path(service).queryParam("passing", "true"),
148 CatalogOptions.BLANK, queryOptions, TYPE_SERVICE_HEALTH_LIST, callback);
152 * Retrieves the healthchecks for all nodes.
154 * GET /v1/health/service/{service}
156 * @param service The service to query.
157 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
158 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
160 public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service) {
161 return getAllServiceInstances(service, null, QueryOptions.BLANK);
165 * Retrieves the healthchecks for all nodes in a given datacenter.
167 * GET /v1/health/service/{service}?dc={datacenter}
169 * @param service The service to query.
170 * @param catalogOptions The catalog specific options to use.
171 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
172 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
174 public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service, CatalogOptions catalogOptions) {
175 return getAllServiceInstances(service, catalogOptions, QueryOptions.BLANK);
179 * Retrieves the healthchecks for all nodes with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
181 * GET /v1/health/service/{service}
183 * @param service The service to query.
184 * @param queryOptions The Query Options to use.
185 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
186 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
188 public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service, QueryOptions queryOptions) {
189 return getAllServiceInstances(service, null, queryOptions);
193 * Retrieves the healthchecks for all nodes in a given datacenter with
194 * {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
196 * GET /v1/health/service/{service}?dc={datacenter}
198 * @param service The service to query.
199 * @param catalogOptions The catalog specific options to use.
200 * @param queryOptions The Query Options to use.
201 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
202 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
204 public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service, CatalogOptions catalogOptions,
205 QueryOptions queryOptions) {
206 return response(webTarget.path("service").path(service), catalogOptions, queryOptions,
207 TYPE_SERVICE_HEALTH_LIST);
211 * Asynchronously retrieves the healthchecks for all nodes in a given
212 * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
214 * GET /v1/health/service/{service}?dc={datacenter}
218 * @param service The service to query.
219 * @param catalogOptions The catalog specific options to use.
220 * @param queryOptions The Query Options to use.
221 * @param callback Callback implemented by callee to handle results.
223 public void getAllServiceInstances(String service, CatalogOptions catalogOptions,
224 QueryOptions queryOptions,
225 ConsulResponseCallback<List<ServiceHealth>> callback) {
226 response(webTarget.path("service").path(service), catalogOptions, queryOptions,
227 TYPE_SERVICE_HEALTH_LIST, callback);
231 * Asynchronously retrieves the healthchecks for all nodes in a given
232 * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
234 * GET /v1/health/service/{service}?dc={datacenter}
238 * @param service The service to query.
239 * @param queryOptions The Query Options to use.
240 * @param callback Callback implemented by callee to handle results.
242 public void getAllServiceInstances(String service, QueryOptions queryOptions,
243 ConsulResponseCallback<List<ServiceHealth>> callback) {
244 response(webTarget.path("service").path(service), CatalogOptions.BLANK,
245 queryOptions, TYPE_SERVICE_HEALTH_LIST, callback);