1、為什麼要服務登出
當服務升級上線時,為了平滑過渡,一般會先把老的服務從註冊中心上摘除(服務本身不停止),等該服務完全沒有流量時,再進行下線的操作。採用SpringCloudConsul時,內部預設每3s和註冊中心同步一次心跳,並以此重新整理各個服務的ip列表。當某個服務從註冊中心登出時,不需要進行廣播,由心跳機制保證該註冊中心剩餘的所有服務能夠感知到。
2、SpringCloudConsul提供的方法
SpringCloudConsul提供了一個ConsulAutoServiceRegistration類,該類主要提供服務的註冊和登出功能,原始碼如下:
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.consul.serviceregistry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.retry.annotation.Retryable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* @author Spencer Gibb
*/
public class ConsulAutoServiceRegistration extends AbstractAutoServiceRegistration<ConsulRegistration> {
private static Log log = LogFactory.getLog(ConsulAutoServiceRegistration.class);
private ConsulDiscoveryProperties properties;
private ConsulAutoRegistration registration;
public ConsulAutoServiceRegistration(ConsulServiceRegistry serviceRegistry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
ConsulDiscoveryProperties properties, ConsulAutoRegistration registration) {
super(serviceRegistry, autoServiceRegistrationProperties);
this.properties = properties;
this.registration = registration;
}
void setPortIfNeeded(int port) {
getPort().compareAndSet(0, port);
}
@Override
protected ConsulAutoRegistration getRegistration() {
if (this.registration.getService().getPort() == null && this.getPort().get() > 0) {
this.registration.initializePort(this.getPort().get());
}
Assert.notNull(this.registration.getService().getPort(), "service.port has not been set");
return this.registration;
}
@Override
protected ConsulAutoRegistration getManagementRegistration() {
return this.registration.managementRegistration();
}
@Override
@Retryable(interceptor = "consulRetryInterceptor")
public void start() {
super.start();
}
@Override
protected void register() {
if (!this.properties.isRegister()) {
log.debug("Registration disabled.");
return;
}
super.register();
}
@Override
protected void registerManagement() {
if (!this.properties.isRegister()) {
return;
}
super.registerManagement();
}
@Override
protected Object getConfiguration() {
return properties;
}
@Override
protected void deregister() {
if (!this.properties.isRegister() || !this.properties.isDeregister()) {
return;
}
super.deregister();
}
@Override
protected void deregisterManagement() {
if (!this.properties.isRegister() || !this.properties.isDeregister()) {
return;
}
super.deregisterManagement();
}
@Override
protected boolean isEnabled() {
return this.properties.getLifecycle().isEnabled();
}
@Override
@SuppressWarnings("deprecation")
protected String getAppName() {
String appName = properties.getServiceName();
return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
}
@Override
public void bind(WebServerInitializedEvent event) {
// do nothing so we can listen for this event in a different class
// this ensures start() can be retried if spring-retry is available
}
}
複製程式碼
其中服務登出的方法邏輯是:
該方法會最終是呼叫consul提供的restfulAPI去進行服務的登出:
3、測試SpringCloudConsul的登出方法
我們寫一段程式碼進行服務登出的測試:
@RestController
public class ConsulController {
private final ConsulAutoServiceRegistration serviceRegistration;
public ConsulController(ConsulAutoServiceRegistration serviceRegistration) {
this.serviceRegistration = serviceRegistration;
}
@DeleteMapping("deregister")
public void deregister() {
serviceRegistration.stop();
}
}
複製程式碼
服務啟動後consul介面顯示服務狀態為綠色,代表正常:
執行該介面後,正常情況下consul會摘除這個服務的註冊資訊,但是多試驗幾次後,會發現服務資訊還在,這個時候如果服務停止的話,必然會出現呼叫異常的情況,consul上的服務狀態為橙色:
4、完整的登出操作
顯然以上的做法並不能真正的完成服務登出的操作,consul的登出操作需要到叢集中的節點上才能完成,具體程式碼如下:
@RestController
@Slf4j
public class ConsulController {
private final ConsulClient consulClient;
private final ConsulRegistration consulRegistration;
public ConsulController(ConsulClient consulClient, ConsulRegistration consulRegistration) {
this.consulClient = consulClient;
this.consulRegistration = consulRegistration;
}
@DeleteMapping("api/deregister")
public void deregister() {
String currentInstanceId = consulRegistration.getInstanceId();
List<Member> members = consulClient.getAgentMembers().getValue();
for (Member member : members) {
String address = member.getAddress();
ConsulClient clearClient = new ConsulClient(address);
try {
Map<String, Service> serviceMap = clearClient.getAgentServices().getValue();
for (Entry<String, Service> entry : serviceMap.entrySet()) {
Service service = entry.getValue();
String instanceId = service.getId();
if (currentInstanceId.equals(instanceId)) {
log.warn("在{}客戶端上的服務 :{}為無效服務,準備清理...................", address, currentInstanceId);
clearClient.agentServiceDeregister(currentInstanceId);
}
}
} catch (Exception e) {
log.error("異常資訊: {}", e);
}
}
}
}
複製程式碼
5、如何讓服務在consul上進入維護模式(臨時摘除)
-
暴露
/service-registry
端點(需要引入spring-boot-starter-actuator支援) -
SpringCloudConsul提供【上線】和【離線】兩種服務狀態的設定,具體程式碼在ConsulServiceRegistry類中
-
傳送POST請求到
/actuator/service-registry
端點,此時狀態應為OUT_OF_SERVICEcurl -X "POST" "http://localhost:8081/actuator/service-registry?status=OUT_OF_SERVICE" -H "Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8" 複製程式碼
-
此時consul中顯示服務狀態進入維護模式:
-
恢復操作時,只需要將狀態變更為UP即可,執行完畢後consul上的服務狀態恢復:
curl -X "POST" "http://localhost:8081/actuator/service-registry?status=UP" -H "Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8" 複製程式碼