How to do it...

Take a look at the following steps to set up service discovery:

  1. Replace the spring-cloud-commons and spring-cloud-context modules with spring-cloud-starter-consul-all by modifying the build.gradle file located in the root of our project with the following content:
... 
 
dependencies { 
    ... 
    compile("io.dropwizard.metrics:metrics-graphite:3.1.0") 
    compile("org.springframework.boot:spring-boot-devtools") 
    //compile("org.springframework.cloud:spring-cloud-context") 
    //compile("org.springframework.cloud:spring-cloud-commons") 
    compile("org.springframework.cloud:spring-cloud-starter-consul-all") 
    runtime("com.h2database:h2") 
    ... 
} 
... 
  1. With Consul dependencies added, we will proceed with enabling our application to automatically register with the local agent upon startup by modifying the BookPubApplication.java file located under the src/main/java/com/example/bookpub directory from the root of our project with the following content:
... 
@EnableScheduling 
@EnableDbCounting 
@EnableDiscoveryClient 
public class BookPubApplication { 
    ... 
}
  1. Given that Consul was successfully installed using the steps described in the Setting up Consul recipe in Chapter 6, Application Packaging and Deployment, we should be able to start it by running consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul and our Terminal window should display the following output:
==> Starting Consul agent... 
==> Starting Consul agent RPC... 
==> Consul agent running! 
           Version: 'v1.0.2' 
... 
  1. After the Consul agent is up and running successfully, we will proceed by starting our application by running ./gradlew clean bootRun
  2. As we watch the startup logs scroll by, there are a couple of interesting entries that indicate the application is interacting with the agent, so watch for the following content in the logs:
... 
2017-12-26 --- b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='consul', propertySources=[ConsulPropertySource [name='config/BookPub-ch9/'], ConsulPropertySource [name='config/application/']]] 
... 
2017-12-26 --- o.s.c.consul.discovery.ConsulLifecycle   : Registering service with consul: NewService{id='BookPub-ch9-8080', name='BookPub-ch9', tags=[], address='<your_machine_name>', port=8080, check=Check{script='null', interval=10s, ttl=null, http=http://<your_machine_name>:8081/health, tcp=null, timeout=null}} 
2017-12-26 --- o.s.c.consul.discovery.ConsulLifecycle   : Registering service with consul: NewService{id='BookPub-ch9-8080-management', name='BookPub-ch9-management', tags=[management], address='://<your_machine_name>', port=8081, check=Check{script='null', interval=10s, ttl=null, http=http://chic02qv045g8wn:8081/health, tcp=null, timeout=null}} 
...
  1. Just to verify that our application has registered and is in communication with the local Consul agent, let's open http://localhost:8081/actuator/consul in the browser to see the Consul agent information, as shown in the following screenshot:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.118.95