SpringCloud中Eureka注册中心实现优雅停服(2.x版本)

SpringCloud中Eureka注册中心实现优雅停服(2.x版本)

1、实现优雅停服,跟服务器没有关系,所以不需要关闭服务端的自我保护机制,注释掉或者这个不写配置。

##关闭自我保护:true为开启,false为关闭
#eureka.server.enable-self-preservation=false
##清理间隔(单位:毫秒,默认60*1000)
#eureka.server.eviction-interval-timer-in-ms=60000

2、在客户端的pom文件中添加actuator的依赖jar包

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  </dependency>

或者

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>

3、在全局配置文件中添加配置信息
(1)yml格式的配置文件

management:
  endpoints:
    web:
      exposure:
        include: shutdown #暴漏shutdown端点服务
  endpoint:
    shutdown:
      enabled: true

(2)properties格式的配置文件

#暴露shutdown端点服务
management.endpoints.web.exposure.include=shutdown
#启用shutdown,优雅停服
management.endpoint.shutdown.enabled=true

4、实现优雅停服方式一:

可以利用工具类发送shutdown请求实现优雅停服。

注意:实现优雅停服必须使用dopost请求发送。

http://ip:port/actuator/shutdown

http工具类:

public class HttpClientUtil {

	public static String doGet(String url, Map<String, String> param) {

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

		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			// 创建uri
			URIBuilder builder = new URIBuilder(url);
			if (param != null) {
				for (String key : param.keySet()) {
					builder.addParameter(key, param.get(key));
				}
			}
			URI uri = builder.build();

			// 创建http GET请求
			HttpGet httpGet = new HttpGet(uri);

			// 执行请求
			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;
	}

	public static String doGet(String url) {
		return doGet(url, null);
	}

	public static String doPost(String url, Map<String, String> param) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			// 创建参数列表
			if (param != null) {
				List<NameValuePair> paramList = new ArrayList<>();
				for (String key : param.keySet()) {
					paramList.add(new BasicNameValuePair(key, param.get(key)));
				}
				// 模拟表单
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
				httpPost.setEntity(entity);
			}
			// 执行http请求
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return resultString;
	}

	public static String doPost(String url) {
		return doPost(url, null);
	}
	
	public static String doPostJson(String url, String json) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			// 创建请求内容
			StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
			httpPost.setEntity(entity);
			// 执行http请求
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return resultString;
	}
	
	public static void main(String[] args) {
		String url ="http://127.0.0.1:9090/actuator/shutdown";
		//该url必须要使用dopost方式来发送
		HttpClientUtil.doPost(url);
	}
}

5、实现优雅停服方式二:

利用idea工具实现优雅停服
(1)点击控制台上的endpoints
SpringCloud中Eureka注册中心实现优雅停服(2.x版本)_第1张图片
(2)点击mappings
SpringCloud中Eureka注册中心实现优雅停服(2.x版本)_第2张图片
(3)点击shutdown,运行,则可以优雅停服
SpringCloud中Eureka注册中心实现优雅停服(2.x版本)_第3张图片

你可能感兴趣的:(个人)