Browse Source

Adds: RegistrationLifecycle (#1044)

pull/1123/head
Zen Huifer 3 years ago committed by GitHub
parent
commit
aa273f8af3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 70
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java
  2. 66
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/RegistrationLifecycle.java
  3. 55
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/RegistrationManagementLifecycle.java
  4. 258
      spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationRegistrationLifecycleTests.java

70
spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java

@ -16,6 +16,8 @@ @@ -16,6 +16,8 @@
package org.springframework.cloud.client.serviceregistry;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@ -42,6 +44,7 @@ import org.springframework.core.env.Environment; @@ -42,6 +44,7 @@ import org.springframework.core.env.Environment;
*
* @param <R> Registration type passed to the {@link ServiceRegistry}.
* @author Spencer Gibb
* @author Zen Huifer
*/
public abstract class AbstractAutoServiceRegistration<R extends Registration>
implements AutoServiceRegistration, ApplicationContextAware, ApplicationListener<WebServerInitializedEvent> {
@ -50,20 +53,27 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration> @@ -50,20 +53,27 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration>
private final ServiceRegistry<R> serviceRegistry;
private boolean autoStartup = true;
private final boolean autoStartup = true;
private AtomicBoolean running = new AtomicBoolean(false);
private final AtomicBoolean running = new AtomicBoolean(false);
private int order = 0;
private final int order = 0;
private final AtomicInteger port = new AtomicInteger(0);
private ApplicationContext context;
private Environment environment;
private AtomicInteger port = new AtomicInteger(0);
private AutoServiceRegistrationProperties properties;
private List<RegistrationManagementLifecycle<R>> registrationManagementLifecycles = new ArrayList<>();
private List<RegistrationLifecycle<R>> registrationLifecycles = new ArrayList<>();
/**
* @deprecated This function is deprecated and can be replaced by another constructor
*/
@Deprecated
protected AbstractAutoServiceRegistration(ServiceRegistry<R> serviceRegistry) {
this.serviceRegistry = serviceRegistry;
@ -75,6 +85,32 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration> @@ -75,6 +85,32 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration>
this.properties = properties;
}
protected AbstractAutoServiceRegistration(ServiceRegistry<R> serviceRegistry,
AutoServiceRegistrationProperties properties,
List<RegistrationManagementLifecycle<R>> registrationManagementLifecycles,
List<RegistrationLifecycle<R>> registrationLifecycles) {
this.serviceRegistry = serviceRegistry;
this.properties = properties;
this.registrationManagementLifecycles = registrationManagementLifecycles;
this.registrationLifecycles = registrationLifecycles;
}
protected AbstractAutoServiceRegistration(ServiceRegistry<R> serviceRegistry,
AutoServiceRegistrationProperties properties, List<RegistrationLifecycle<R>> registrationLifecycles) {
this.serviceRegistry = serviceRegistry;
this.properties = properties;
this.registrationLifecycles = registrationLifecycles;
}
public void addRegistrationManagementLifecycle(
RegistrationManagementLifecycle<R> registrationManagementLifecycle) {
this.registrationManagementLifecycles.add(registrationManagementLifecycle);
}
public void addRegistrationLifecycle(RegistrationLifecycle<R> registrationLifecycle) {
this.registrationLifecycles.add(registrationLifecycle);
}
protected ApplicationContext getContext() {
return this.context;
}
@ -129,9 +165,20 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration> @@ -129,9 +165,20 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration>
// because of containerPortInitializer below
if (!this.running.get()) {
this.context.publishEvent(new InstancePreRegisteredEvent(this, getRegistration()));
registrationLifecycles.forEach(
registrationLifecycle -> registrationLifecycle.postProcessBeforeStartRegister(getRegistration()));
register();
this.registrationLifecycles.forEach(
registrationLifecycle -> registrationLifecycle.postProcessAfterStartRegister(getRegistration()));
if (shouldRegisterManagement()) {
registerManagement();
this.registrationManagementLifecycles
.forEach(registrationManagementLifecycle -> registrationManagementLifecycle
.postProcessBeforeStartRegisterManagement(getManagementRegistration()));
this.registerManagement();
registrationManagementLifecycles
.forEach(registrationManagementLifecycle -> registrationManagementLifecycle
.postProcessAfterStartRegisterManagement(getManagementRegistration()));
}
this.context.publishEvent(new InstanceRegisteredEvent<>(this, getConfiguration()));
this.running.compareAndSet(false, true);
@ -260,9 +307,20 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration> @@ -260,9 +307,20 @@ public abstract class AbstractAutoServiceRegistration<R extends Registration>
public void stop() {
if (this.getRunning().compareAndSet(true, false) && isEnabled()) {
this.registrationLifecycles.forEach(
registrationLifecycle -> registrationLifecycle.postProcessBeforeStopRegister(getRegistration()));
deregister();
this.registrationLifecycles.forEach(
registrationLifecycle -> registrationLifecycle.postProcessAfterStopRegister(getRegistration()));
if (shouldRegisterManagement()) {
this.registrationManagementLifecycles
.forEach(registrationManagementLifecycle -> registrationManagementLifecycle
.postProcessBeforeStopRegisterManagement(getManagementRegistration()));
deregisterManagement();
this.registrationManagementLifecycles
.forEach(registrationManagementLifecycle -> registrationManagementLifecycle
.postProcessAfterStopRegisterManagement(getManagementRegistration()));
}
this.serviceRegistry.close();
}

66
spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/RegistrationLifecycle.java

@ -0,0 +1,66 @@ @@ -0,0 +1,66 @@
/*
* Copyright 2012-2020 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
*
* https://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.client.serviceregistry;
import org.springframework.core.Ordered;
/**
* Service registration life cycle. This life cycle is only related to
* {@link Registration}.
*
* @author Zen Huifer
*/
public interface RegistrationLifecycle<R extends Registration> extends Ordered {
/**
* default order.
*/
int DEFAULT_ORDER = 0;
/**
* A method executed before registering the local service with the
* {@link ServiceRegistry}.
* @param registration registration
*/
void postProcessBeforeStartRegister(R registration);
/**
* A method executed after registering the local service with the
* {@link ServiceRegistry}.
* @param registration registration
*/
void postProcessAfterStartRegister(R registration);
/**
* A method executed before de-registering the local service with the
* {@link ServiceRegistry}.
* @param registration registration
*/
void postProcessBeforeStopRegister(R registration);
/**
* A method executed after de-registering the local service with the
* {@link ServiceRegistry}.
* @param registration registration
*/
void postProcessAfterStopRegister(R registration);
default int getOrder() {
return DEFAULT_ORDER;
}
}

55
spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/RegistrationManagementLifecycle.java

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
/*
* Copyright 2012-2020 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
*
* https://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.client.serviceregistry;
/**
* Service registration life cycle. This life cycle is only related to
* {@link org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration#getManagementRegistration()}.
*
* @author Zen Huifer
*/
public interface RegistrationManagementLifecycle<R extends Registration> extends RegistrationLifecycle<R> {
/**
* A method executed before registering the local management service with the
* {@link ServiceRegistry}.
* @param registrationManagement registrationManagement
*/
void postProcessBeforeStartRegisterManagement(R registrationManagement);
/**
* A method executed after registering the local management service with the
* {@link ServiceRegistry}.
* @param registrationManagement registrationManagement
*/
void postProcessAfterStartRegisterManagement(R registrationManagement);
/**
* A method executed before de-registering the management local service with the
* {@link ServiceRegistry}.
* @param registrationManagement registrationManagement
*/
void postProcessBeforeStopRegisterManagement(R registrationManagement);
/**
* A method executed after de-registering the management local service with the
* {@link ServiceRegistry}.
* @param registrationManagement registrationManagement
*/
void postProcessAfterStopRegisterManagement(R registrationManagement);
}

258
spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationRegistrationLifecycleTests.java

@ -0,0 +1,258 @@ @@ -0,0 +1,258 @@
/*
* Copyright 2012-2020 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
*
* https://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.client.serviceregistry;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Zen Huifer
*/
@SpringBootTest(classes = AbstractAutoServiceRegistrationRegistrationLifecycleTests.Config.class)
class AbstractAutoServiceRegistrationRegistrationLifecycleTests {
@Autowired
@Qualifier("testAutoServiceRegistration")
private TestAutoServiceRegistrationLifecycle autoRegistration;
@Test
void startTest() {
autoRegistration.start();
}
@Test
void stopTest() {
autoRegistration.stop();
}
static class RegistrationLifecycleImpl implements RegistrationLifecycle<TestRegistrationLifecycleRegistration> {
@Override
public void postProcessBeforeStartRegister(TestRegistrationLifecycleRegistration registration) {
registration.getMetadata().putIfAbsent("test_data", "test_data");
registration.getMetadata().putIfAbsent("is_start", "false");
}
@Override
public void postProcessAfterStartRegister(TestRegistrationLifecycleRegistration registration) {
assertThat(registration.getMetadata()).containsKey("test_data");
assertThat(registration.getMetadata().get("test_data")).isEqualTo("test_data");
assertThat(registration.getMetadata()).containsKey("is_start");
registration.getMetadata().putIfAbsent("is_start", "true");
}
@Override
public void postProcessBeforeStopRegister(TestRegistrationLifecycleRegistration registration) {
assertThat(registration.getMetadata().get("is_start")).isEqualTo("true");
Map<String, String> metadata = registration.getMetadata();
metadata.putIfAbsent("is_stop", "false");
metadata.putIfAbsent("is_start", "false");
}
@Override
public void postProcessAfterStopRegister(TestRegistrationLifecycleRegistration registration) {
assertThat(registration.getMetadata().get("test_data")).isEqualTo("test_data");
assertThat(registration.getMetadata().get("is_stop")).isEqualTo("false");
assertThat(registration.getMetadata().get("is_start")).isEqualTo("false");
registration.getMetadata().putIfAbsent("is_stop", "true");
}
}
@EnableAutoConfiguration
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
public TestAutoServiceRegistrationLifecycle testAutoServiceRegistration(
AutoServiceRegistrationProperties properties) {
List<RegistrationLifecycle<TestRegistrationLifecycleRegistration>> registrationLifecycles = new ArrayList<>();
registrationLifecycles.add(new RegistrationLifecycleImpl());
return new TestAutoServiceRegistrationLifecycle(properties, registrationLifecycles);
}
}
static class TestAutoServiceRegistrationLifecycle
extends AbstractAutoServiceRegistration<TestRegistrationLifecycleRegistration> {
private final int port = 0;
TestRegistrationLifecycleRegistration testRegistrationLifecycleRegistration = new TestRegistrationLifecycleRegistration();
protected TestAutoServiceRegistrationLifecycle() {
super(new TestRegistrationLifecycleServiceRegistration());
}
TestAutoServiceRegistrationLifecycle(AutoServiceRegistrationProperties properties) {
super(new TestRegistrationLifecycleServiceRegistration(), properties);
}
TestAutoServiceRegistrationLifecycle(AutoServiceRegistrationProperties properties,
List<RegistrationManagementLifecycle<TestRegistrationLifecycleRegistration>> registrationManagementLifecycles,
List<RegistrationLifecycle<TestRegistrationLifecycleRegistration>> registrationLifecycles) {
super(new TestRegistrationLifecycleServiceRegistration(), properties, registrationManagementLifecycles,
registrationLifecycles);
}
TestAutoServiceRegistrationLifecycle(AutoServiceRegistrationProperties properties,
List<RegistrationLifecycle<TestRegistrationLifecycleRegistration>> registrationLifecycles) {
super(new TestRegistrationLifecycleServiceRegistration(), properties, registrationLifecycles);
}
@Override
protected AtomicInteger getPort() {
return super.getPort();
}
@Override
protected String getAppName() {
return super.getAppName();
}
@Override
protected TestRegistrationLifecycleRegistration getRegistration() {
return testRegistrationLifecycleRegistration;
}
@Override
protected TestRegistrationLifecycleRegistration getManagementRegistration() {
return null;
}
@Override
protected Object getConfiguration() {
return null;
}
@Override
protected boolean isEnabled() {
return true;
}
}
static class TestRegistrationLifecycleServiceRegistration
implements ServiceRegistry<TestRegistrationLifecycleRegistration> {
private boolean registered = false;
private boolean deregistered = false;
@Override public void register(
TestRegistrationLifecycleRegistration registration) {
if (registration == null) {
throw new NullPointerException();
}
if (!(registration instanceof TestRegistrationLifecycleRegistration)) {
this.registered = true;
}
}
@Override
public void deregister(TestRegistrationLifecycleRegistration registration) {
if (registration == null) {
throw new NullPointerException();
}
if (!(registration instanceof TestRegistrationLifecycleRegistration)) {
this.deregistered = true;
}
}
@Override
public void close() {
}
@Override
public void setStatus(TestRegistrationLifecycleRegistration registration, String status) {
}
@Override
public <T> T getStatus(TestRegistrationLifecycleRegistration registration) {
return null;
}
boolean isRegistered() {
return registered;
}
boolean isDeregistered() {
return deregistered;
}
}
static class TestRegistrationLifecycleRegistration implements Registration {
private final Map<String, String> metadata = new HashMap<>(8);
@Override
public String getInstanceId() {
return "TestRegistrationLifecycleRegistration";
}
@Override
public String getServiceId() {
return "test";
}
@Override
public String getHost() {
return null;
}
@Override
public int getPort() {
return 0;
}
@Override
public boolean isSecure() {
return false;
}
@Override
public URI getUri() {
return null;
}
@Override
public Map<String, String> getMetadata() {
return this.metadata;
}
}
}
Loading…
Cancel
Save