Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds ServiceRegistryEndpoint
  • Loading branch information
spencergibb committed Nov 4, 2016
commit 2c95bd26f6eb60fb897dd7da103833ba01f9d509
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author Spencer Gibb
*/
@SuppressWarnings("deprecation")
public abstract class AbstractAutoServiceRegistration<R> extends AbstractDiscoveryLifecycle {
public abstract class AbstractAutoServiceRegistration<R extends Registration> extends AbstractDiscoveryLifecycle {

private ServiceRegistry<R> serviceRegistry;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.springframework.cloud.client.serviceregistry;

/**
* @author Spencer Gibb
*/
public interface Registration {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is a marker interface needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, cause looking at the test I don't really see any benefit of having this Registration interface. Can you explain that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, leftover from a refactor where this interface had a method and I removed it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up putting this back in, because I needed to lookup the registration in the spring context without knowing the implementation.

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
* TODO: write javadoc
* @author Spencer Gibb
*/
public interface ServiceRegistry<R> {
public interface ServiceRegistry<R extends Registration> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here or somewhere else, we should qualify with an example of what a registration might be

void register(R registration);

void deregister(R registration);

void close();

// TODO: return value for success?
void setStatus(R registration, String status);

// TODO: concrete return value? Interface?
Object getStatus(R registration);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.springframework.cloud.client.serviceregistry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author Spencer Gibb
*/
@ConditionalOnBean(ServiceRegistry.class)
@Configuration
public class ServiceRegistryAutoConfiguration {

@Autowired(required = false)
private Registration registration;

@ConditionalOnClass(Endpoint.class)
@Bean
public ServiceRegistryEndpoint serviceRegistryEndpoint(ServiceRegistry serviceRegistry) {
ServiceRegistryEndpoint endpoint = new ServiceRegistryEndpoint(serviceRegistry);
endpoint.setRegistration(registration);
return endpoint;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.client.serviceregistry.endpoint;

import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* Endpoint to display and set the service instance status using the service registry.
*
* @author Spencer Gibb
*/
@ManagedResource(description = "Can be used to display and set the service instance status using the service registry")
@SuppressWarnings("unchecked")
public class ServiceRegistryEndpoint implements MvcEndpoint {

private final ServiceRegistry serviceRegistry;

private Registration registration;

public ServiceRegistryEndpoint(ServiceRegistry<?> serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}

public void setRegistration(Registration registration) {
this.registration = registration;
}

@RequestMapping(path = "instance-status", method = RequestMethod.POST)
@ResponseBody
@ManagedOperation
public ResponseEntity<?> setStatus(@RequestBody String status) {
Assert.notNull(status, "status may not by null");

if (this.registration == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("no registration found");
}

this.serviceRegistry.setStatus(this.registration, status);
// getEurekaClient().setStatus(status.getStatus());
// getEurekaClient().cancelOverrideStatus();
return ResponseEntity.ok().build();
}

@RequestMapping(path = "instance-status", method = RequestMethod.GET)
@ResponseBody
@ManagedAttribute
public ResponseEntity getStatus() {
if (this.registration == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("no registration found");
}

// return new Status(this.infoManager.getInfo().getStatus(), this.infoManager.getInfo().getOverriddenStatus());
return ResponseEntity.ok().body(this.serviceRegistry.getStatus(this.registration));
}

@Override
public String getPath() {
return "/service-registry";
}

@Override
public boolean isSensitive() {
return true;
}

@Override
public Class<? extends Endpoint<?>> getEndpointType() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ org.springframework.cloud.client.CommonsClientAutoConfiguration,\
org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration,\
org.springframework.cloud.client.hypermedia.CloudHypermediaAutoConfiguration,\
org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration,\
org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration,\
org.springframework.cloud.commons.util.UtilAutoConfiguration


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public TestAutoServiceRegistration testAutoServiceRegistration() {
}
}

public static class TestRegistration {
public static class TestRegistration implements Registration {
}

public static class TestServiceRegistry implements ServiceRegistry<TestRegistration> {
Expand All @@ -78,6 +78,17 @@ public void deregister(TestRegistration registration) {
@Override
public void close() { }

@Override
public void setStatus(TestRegistration registration, String status) {
//TODO: test setStatus
}

@Override
public Object getStatus(TestRegistration registration) {
//TODO: test getStatus
return null;
}

boolean isRegistered() {
return registered;
}
Expand Down