<commit_before># sample.consulservicediscovery

## Using Consul for Service Discovery in Liberty

Service discovery is an important element of any microservices architecture. A number of technologies exist in this space, including Eureka, Apache Zookeeper + Curator, Kubernetes, etcd, and Consul. [Consul by Hashicorp](https://www.consul.io) is growing in popularity and has a number of attractive features, including broad platform support and a DNS interface. 

In general, registration of a service with a service discovery engine is a manual process. This sample shows how WebSphere Liberty Profile's extensibility can be used to automatically register all REST endpoints as Consul services. It provides a user feature which hooks into Liberty's annotation scanning function and publishes jax-rs endpoints with Consul.  

### Why not do this with reflection? 

It's possible to achieve something similar without the Liberty profile, using reflection. See, for example, [<NAME>'s blog post](http://dalelane.co.uk/blog/?p=1871) describing how to use reflection to produce a web page documenting endpoints. The limitation of this approach is that reflection will only see classes which have already been loaded. In a simple application, this would probably include the whole class space, but in an optimised application server, applications may only be loaded when they're first called. This makes reflectively scanning the class space unsuitable for service discovery.  

### Layout 

There are two user features, one which provides the basic consul api, and one which hooks annotation scanning to automatically register endpoints. 

The consul project wraps the consul jars and dependencies into an OSGi bundle, and the discovery.annotation project is the source for the annotation scanning. 

### Example server configuration 

To use and configure the feature, add the following to the server.xml of your Liberty server: 

	<!-- Enable features -->
	<featureManager>
		<feature>usr:consulDiscovery</feature>
	</featureManager>

	<consul.annotation.scanner server="my.consul.hostname" />

### Configuring the location of the Consul server

If you’re running in an environment like Bluemix, configuring services via environment variables is preferable to setting them up in the server.xml. If the CONSUL_SERVER environment variable is set, it will be preferred instead of the configured server. CONSUL_SERVER should be set to a host name or IP address. <commit_msg>Cross reference microservices sample<commit_after># sample.consulservicediscovery

## Using Consul for Service Discovery in Liberty

Service discovery is an important element of any microservices architecture. A number of technologies exist in this space, including Eureka, Apache Zookeeper + Curator, Kubernetes, etcd, and Consul. [Consul by Hashicorp](https://www.consul.io) is growing in popularity and has a number of attractive features, including broad platform support and a DNS interface. 

In general, registration of a service with a service discovery engine is a manual process. This sample shows how WebSphere Liberty Profile's extensibility can be used to automatically register all REST endpoints as Consul services. It provides a user feature which hooks into Liberty's annotation scanning function and publishes jax-rs endpoints with Consul.  

### Why not do this with reflection? 

It's possible to achieve something similar without the Liberty profile, using reflection. See, for example, [Dale Lane's blog post](http://dalelane.co.uk/blog/?p=1871) describing how to use reflection to produce a web page documenting endpoints. The limitation of this approach is that reflection will only see classes which have already been loaded. In a simple application, this would probably include the whole class space, but in an optimised application server, applications may only be loaded when they're first called. This makes reflectively scanning the class space unsuitable for service discovery.  

### Layout 

There are two user features, one which provides the basic consul api, and one which hooks annotation scanning to automatically register endpoints. 

The consul project wraps the consul jars and dependencies into an OSGi bundle, and the discovery.annotation project is the source for the annotation scanning. 

### Example server configuration 

To use and configure the feature, add the following to the server.xml of your Liberty server: 

	<!-- Enable features -->
	<featureManager>
		<feature>usr:consulDiscovery</feature>
	</featureManager>

	<consul.annotation.scanner server="my.consul.hostname" />

### Example usage 

The [catastrophe microservices sample](https://github.com/holly-cummins/catastrophe-microservices) shows how to configure a server to automatically register REST endpoints, and also how to do service lookups. For example, see one of the [server.xml](https://github.com/holly-cummins/catastrophe-scoring/blob/master/usr/servers/catastrophe-scoring/server.xml)s for the servers, for server configuration, and also the [ServiceFinder](https://github.com/holly-cummins/catastrophe-web/blob/master/src/main/java/catastrophe/discovery/ServiceFinder.java) class for lookup.

### Configuring the location of the Consul server

If you’re running in an environment like Bluemix, configuring services via environment variables is preferable to setting them up in the server.xml. If the CONSUL_SERVER environment variable is set, it will be preferred instead of the configured server. CONSUL_SERVER should be set to a host name or IP address. 
{'fraction_non_alphanumeric': 0.05387387387387387, 'fraction_numerical': 0.0014414414414414415, 'mean_word_length': 5.033695652173913, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5946745', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1269, 'n_words': 710}