consul剔除无效服务

剔除无效的consul服务 

获取无效服务    http://ip:8500/v1/health/state/critical 

返回样式:json

[
	{
		"Node": "server-xh-1",
		"CheckID": "service:cmp-clue-10235",
		"Name": "Service 'cmp-clue' check",
		"Status": "critical",
		"Notes": "",
		"Output": "Get http://172.16.0.158:18121/actuator/health: dial tcp 172.16.0.158:18121: connect: connection refused",
		"ServiceID": "cmp-clue-10235",
		"ServiceName": "cmp-clue",
		"ServiceTags": [
			"secure=false"
		],
		"Definition": {},
		"CreateIndex": 942733,
		"ModifyIndex": 961477
	},
	{
		"Node": "server-xh-1",
		"CheckID": "service:cmp-clue-96450",
		"Name": "Service 'cmp-clue' check",
		"Status": "critical",
		"Notes": "",
		"Output": "Get http://172.16.0.159:18121/actuator/health: dial tcp 172.16.0.159:18121: connect: connection refused",
		"ServiceID": "cmp-clue-96450",
		"ServiceName": "cmp-clue",
		"ServiceTags": [
			"secure=false"
		],
		"Definition": {},
		"CreateIndex": 934001,
		"ModifyIndex": 961479
	}
]

 我们需要获取出无效服务的   ServiceID 这个参数

然后进行剔除

剔除方式:  http://ip:8500/v1/agent/service/deregister/+ServiceID 注意的是需要是PUT请求

下面是完整代码

package cn.com.provider.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author 荡漾
 * @title: ConsuleliminateInvalidServicesMain
 * @projectName 
 * @description: TODO
 * @date 2019/10/17  10:43
 */
public class ConsuleliminateInvalidServicesMain {

    public static void main(String[] args) throws Exception {
        //获取出cosnul中无效服务集合,返回json
        String respStr = doGet("http://ip:8500/v1/health/state/critical");
        System.out.println(respStr);
        JSONArray jsonObject = JSONObject.parseArray(respStr);
        //遍历集合获取出无效服务的serviceID
        jsonObject.forEach(object->{
            JSONObject consul = JSONObject.parseObject(object.toString());
            String serviceID = consul.getString("ServiceID");
            //发送剔除请求剔除需要是put请求
            doPut("http://ip:8500/v1/agent/service/deregister/".concat(serviceID));
        });
    }

     private static String doPut(String url) {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建http GET请求
            HttpPut httpGet = new HttpPut(url);
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
            return resultString;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
         return resultString;
     }
    public static String doGet(String url ) {

        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(url);

            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }
}

解决

发布了66 篇原创文章 · 获赞 85 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/qq_38380025/article/details/102601799