How to do it...

  1. We will start by adding Netflix Feign module dependencies to our project. Let's modify our build.gradle file located in the root of our project with the following content:
dependencies { 
    ... 
    compile("org.springframework.cloud:spring-
cloud-starter-consul-all") compile("org.springframework.cloud:spring-
cloud-starter-openfeign") runtime("com.h2database:h2") ... }
  1. With the dependency added, our next step is to create a Java API interface describing how we want to define our interaction with the BookPub service. Let's create an api package under the src/main/java/com/example/bookpub directory from the root of our project.
  2. Inside the newly-created api package, let's create our API class file named BookPubClient.java with the following content:
package com.example.bookpub.api; 
 
import com.example.bookpub.entity.Book; 
import org.springframework.cloud.netflix.feign.FeignClient; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
 
@FeignClient("http://BookPub-ch9") 
public interface BookPubClient { 
    @RequestMapping(value = "/books/{isbn}",  
                    method = RequestMethod.GET) 
    public Book findBookByIsbn(@PathVariable("isbn") String isbn); 
}
  1. After we have defined the API, it is time to tell our application that we want to enable Feign support. We will do that by making a change to the BookPubApplication.java file located under the src/main/java/com/example/bookpub directory from the root of our project with the following content:
... 
@EnableDiscoveryClient 
@EnableFeignClients 
public class BookPubApplication {...} 
  1. Finally, let's create a client controller to invoke BookPubClient by making a new file named ClientController.java under the src/main/java/com/example/bookpub/controllers directory from the root of our project with the following content:
... 
@RestController 
@RequestMapping("/client") 
public class ClientController { 
 
    @Autowired 
    private BookPubClient client; 
 
    @RequestMapping(value = "/book/{isbn}",  
                    method = RequestMethod.GET) 
    public Book getBook(@PathVariable String isbn) { 
        return client.findBookByIsbn(isbn); 
    } 
} 
  1. With everything set and done, let's start the application by executing the ./gradlew clean bootRun command.
Make sure that the Consul agent is also running in the background, otherwise service registration will fail.
  1. Once the application is up and running, let's open http://localhost:8080/client/book/978-1-78528-415-1 in the browser to see the Consul agent information, as shown in the following screenshot:
  2. If we look at the application console logs, we will also see entries indicating that our Feign client is initialized and functioning. You should see something similar to this:
2017-12-26 --- c.n.u.concurrent.ShutdownEnabledTimer : Shutdown hook installed for: NFLoadBalancer-PingTimer-BookPub-ch9
2017-12-26 --- c.netflix.loadbalancer.BaseLoadBalancer : Client:BookPub-ch9 instantiated a LoadBalancer:DynamicServerListLoadBalancer:{NFLoadBalancer:name=BookPub-ch9,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2017-12-26 --- c.n.l.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater
2017-12-26 --- c.netflix.config.ChainedDynamicProperty : Flipping property: BookPub-ch9.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2017-12-26 --- c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client BookPub-ch9 initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=BookPub-ch9,current list of Servers=[192.168.1.194:8080],Load balancer stats=Zone stats: {unknown=[Zone:unknown; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:192.168.1.194:8080; Zone:UNKNOWN; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Wed Dec 31 18:00:00 CST 1969; First connection made: Wed Dec 31 18:00:00 CST 1969; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:ConsulServerList{serviceId='BookPub-ch9', tag=null}
  1. One last thing that we should do is to get our tests to work with all the newly added frameworks. Because Spring Cloud does not add itself to the test life cycle, we should explicitly disable any reliance on beans created by Spring Cloud libraries during tests. To do so let's add to our application.properties file located under the src/test/resources directory from the root of the project of the following properties:
spring.cloud.bus.enabled=false 
spring.cloud.consul.enabled=false 
spring.cloud.consul.discovery.enabled=false 
eureka.client.enabled=false 
autoconfigure.exclude=com.example.bookpub.
MonitoringConfiguration.class
  1. We also need to add a Mock dependency on BookPubClient into the JpaAuthorRepositoryTests.java and WebMvcBookControllerTests.java files located under the src/test/java/com/example/bookpub directory from the root of the project with the following content:
@MockBean 
private BookPubClient client; 
..................Content has been hidden....................

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