2 * Copyright 2016 ZTE, Inc. and others.
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 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
19 * Licensed under the Apache License, Version 2.0 (the "License");
20 * you may not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
23 * http://www.apache.org/licenses/LICENSE-2.0
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS,
27 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
32 package org.openo.msb.wrapper.consul;
34 import static org.openo.msb.wrapper.consul.util.ClientUtil.response;
36 import java.util.List;
38 import javax.ws.rs.client.WebTarget;
39 import javax.ws.rs.core.GenericType;
41 import org.openo.msb.wrapper.consul.async.ConsulResponseCallback;
42 import org.openo.msb.wrapper.consul.model.ConsulResponse;
43 import org.openo.msb.wrapper.consul.model.health.ServiceHealth;
44 import org.openo.msb.wrapper.consul.option.CatalogOptions;
45 import org.openo.msb.wrapper.consul.option.QueryOptions;
48 * HTTP Client for /v1/health/ endpoints.
50 public class HealthClient {
53 private static final GenericType<List<ServiceHealth>> TYPE_SERVICE_HEALTH_LIST =
54 new GenericType<List<ServiceHealth>>() {};
56 private final WebTarget webTarget;
59 * Constructs an instance of this class.
61 * @param webTarget The {@link javax.ws.rs.client.WebTarget} to base requests from.
63 HealthClient(WebTarget webTarget) {
64 this.webTarget = webTarget;
70 * Retrieves the healthchecks for all healthy service instances.
72 * GET /v1/health/service/{service}?passing
74 * @param service The service to query.
75 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
76 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
78 public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service) {
79 return getHealthyServiceInstances(service, null, QueryOptions.BLANK);
83 * Retrieves the healthchecks for all healthy service instances in a given datacenter.
85 * GET /v1/health/service/{service}?dc={datacenter}&passing
87 * @param service The service to query.
88 * @param catalogOptions The catalog specific options to use.
89 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
90 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
92 public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service, CatalogOptions catalogOptions) {
93 return getHealthyServiceInstances(service, catalogOptions, QueryOptions.BLANK);
97 * Retrieves the healthchecks for all healthy service instances with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
99 * GET /v1/health/service/{service}?passing
101 * @param service The service to query.
102 * @param queryOptions The Query Options to use.
103 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
104 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
106 public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service, QueryOptions queryOptions) {
107 return getHealthyServiceInstances(service, null, queryOptions);
111 * Retrieves the healthchecks for all healthy service instances in a given datacenter with
112 * {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
114 * GET /v1/health/service/{service}?dc={datacenter}&passing
116 * @param service The service to query.
117 * @param catalogOptions The catalog specific options to use.
118 * @param queryOptions The Query Options to use.
119 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
120 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
122 public ConsulResponse<List<ServiceHealth>> getHealthyServiceInstances(String service, CatalogOptions catalogOptions,
123 QueryOptions queryOptions) {
124 return response(webTarget.path("service").path(service).queryParam("passing", "true"),
125 catalogOptions, queryOptions, TYPE_SERVICE_HEALTH_LIST);
129 * Asynchronously retrieves the healthchecks for all healthy service instances in a given
130 * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
132 * GET /v1/health/service/{service}?dc={datacenter}&passing
136 * @param service The service to query.
137 * @param catalogOptions The catalog specific options to use.
138 * @param queryOptions The Query Options to use.
139 * @param callback Callback implemented by callee to handle results.
141 public void getHealthyServiceInstances(String service, CatalogOptions catalogOptions,
142 QueryOptions queryOptions,
143 ConsulResponseCallback<List<ServiceHealth>> callback) {
144 response(webTarget.path("service").path(service).queryParam("passing", "true"),
145 catalogOptions, queryOptions, TYPE_SERVICE_HEALTH_LIST, callback);
149 * Asynchronously retrieves the healthchecks for all healthy service instances in a given
150 * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
152 * GET /v1/health/service/{service}?dc={datacenter}&passing
156 * @param service The service to query.
157 * @param queryOptions The Query Options to use.
158 * @param callback Callback implemented by callee to handle results.
160 public void getHealthyServiceInstances(String service, QueryOptions queryOptions,
161 ConsulResponseCallback<List<ServiceHealth>> callback) {
162 response(webTarget.path("service").path(service).queryParam("passing", "true"),
163 CatalogOptions.BLANK, queryOptions, TYPE_SERVICE_HEALTH_LIST, callback);
167 * Retrieves the healthchecks for all nodes.
169 * GET /v1/health/service/{service}
171 * @param service The service to query.
172 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
173 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
175 public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service) {
176 return getAllServiceInstances(service, null, QueryOptions.BLANK);
180 * Retrieves the healthchecks for all nodes in a given datacenter.
182 * GET /v1/health/service/{service}?dc={datacenter}
184 * @param service The service to query.
185 * @param catalogOptions The catalog specific options to use.
186 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
187 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
189 public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service, CatalogOptions catalogOptions) {
190 return getAllServiceInstances(service, catalogOptions, QueryOptions.BLANK);
194 * Retrieves the healthchecks for all nodes with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
196 * GET /v1/health/service/{service}
198 * @param service The service to query.
199 * @param queryOptions The Query Options to use.
200 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
201 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
203 public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service, QueryOptions queryOptions) {
204 return getAllServiceInstances(service, null, queryOptions);
208 * Retrieves the healthchecks for all nodes in a given datacenter with
209 * {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
211 * GET /v1/health/service/{service}?dc={datacenter}
213 * @param service The service to query.
214 * @param catalogOptions The catalog specific options to use.
215 * @param queryOptions The Query Options to use.
216 * @return A {@link org.openo.msb.wrapper.consul.model.ConsulResponse} containing a list of
217 * {@link com.orbitz.consul.model.health.HealthCheck} objects.
219 public ConsulResponse<List<ServiceHealth>> getAllServiceInstances(String service, CatalogOptions catalogOptions,
220 QueryOptions queryOptions) {
221 return response(webTarget.path("service").path(service), catalogOptions, queryOptions,
222 TYPE_SERVICE_HEALTH_LIST);
226 * Asynchronously retrieves the healthchecks for all nodes in a given
227 * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
229 * GET /v1/health/service/{service}?dc={datacenter}
233 * @param service The service to query.
234 * @param catalogOptions The catalog specific options to use.
235 * @param queryOptions The Query Options to use.
236 * @param callback Callback implemented by callee to handle results.
238 public void getAllServiceInstances(String service, CatalogOptions catalogOptions,
239 QueryOptions queryOptions,
240 ConsulResponseCallback<List<ServiceHealth>> callback) {
241 response(webTarget.path("service").path(service), catalogOptions, queryOptions,
242 TYPE_SERVICE_HEALTH_LIST, callback);
246 * Asynchronously retrieves the healthchecks for all nodes in a given
247 * datacenter with {@link org.openo.msb.wrapper.consul.option.QueryOptions}.
249 * GET /v1/health/service/{service}?dc={datacenter}
253 * @param service The service to query.
254 * @param queryOptions The Query Options to use.
255 * @param callback Callback implemented by callee to handle results.
257 public void getAllServiceInstances(String service, QueryOptions queryOptions,
258 ConsulResponseCallback<List<ServiceHealth>> callback) {
259 response(webTarget.path("service").path(service), CatalogOptions.BLANK,
260 queryOptions, TYPE_SERVICE_HEALTH_LIST, callback);