Using Loadbalancerclient
There are a number of abstraction interfaces in spring Cloud Commons that are related to service governance, including DiscoveryClient
, what we're about to introduce here LoadBalancerClient
. For the definition of these interfaces we have said in our previous introduction to service registration and discovery that the Spring cloud does this abstraction, a good decoupling of the service governance system, so that we can easily replace different service governance facilities.
From LoadBalancerClient
the name of the interface, we know that this is an abstract definition of a load balancer client, and let's look at how to consume the service using the Load Balancer Client interface provided by spring Cloud.
In the following example, we will use the Eureka-server built in the previous article as the Service registry, Eureka-client as the service provider.
- Let's start by creating a service consumer project, named:
eureka-consumer
. and to pom.xml
introduce dependencies in (this omits the configuration of the parent and dependencymanagement):
1234567891011121314 |
<dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> </dependencies> |
- Configuration
application.properties
, specify the address of the Eureka registry:
1234 |
Spring.application.name=eureka-consumerserver.port=2101 eureka.client.serviceurl.defaultzone=http://localhost:1001/eureka/ |
- Create the application main class. Initialized to
RestTemplate
actually initiate a rest request. @EnableDiscoveryClient
annotations are used to add current applications to the service governance system.
12345678910111213 |
@EnableDiscove Ryclient @SpringBootApplication public class application { @Bean public resttemplate resttemplate () { return new resttemplate (); } public static void main (string[] args) { span class= "keyword" >new springapplicationbuilder (Application.class). Web (true). Run (args); }} |
- Create an interface to consume the interface provided by Eureka-client:
12345678910111213141516 |
@RestController public < span class= "keyword" >class dccontroller { @Autowired Loadbalancerclient loadbalancerclient; @Autowired Resttemplate resttemplate; @GetMapping ( "/consumer") Span class= "function" >public String dc () { serviceinstance serviceinstance = loadbalancerclient.choose ( " Eureka-client "); String url = "/DC"; System.out.println (URL); return resttemplate.getforobject (URL, string.class); }} |
Can see here, we injected LoadBalancerClient
and, and in the implementation of the RestTemplate
/consumer
interface, the first through loadBalancerClient
the choose
function to load balanced selection of a eureka-client
service instance, the basic information of this service instance is stored in ServiceInstance
, The detailed address of the provider is then spliced through the information in these objects, and the object is then /dc
used RestTemplate
to implement the call to the service provider interface.
After you have finished writing your code, the reader can start Eureka-server, eureka-client, and Eureka-consumer. To track how the Eureka-consumer service is consuming the interface of the Eureka-client service /dc
.
From now on, I will record the process and the essence of the recently developed Springcloud micro-service cloud architecture, and help more friends who are interested in developing the Spring cloud framework, hoping to help more good scholars. Let's explore how the Spring cloud architecture is built and how it can be used in enterprise projects. SOURCE Source
Spring Cloud builds MicroServices architecture: Service Consumption (base)