Using Loadbalancerclient
There are a number of abstraction interfaces in spring Cloud Commons that are related to service governance, including Discoveryclient, the loadbalancerclient we are about to introduce here. 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 the naming of the Loadbalancerclient 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 introduce dependencies in Pom.xml (the configuration of parent and dependencymanagement is omitted here):
<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>
Configure Application.properties, specify the address of the Eureka registry:
spring.application.name=eureka-consumerserver.port=2101eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
Create the application main class. Initializes the resttemplate, which is used to actually initiate a rest request. @EnableDiscoveryClient annotations are used to add the current application to the service governance architecture.
@EnableDiscoveryClient@SpringBootApplicationpublic class Application {@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {new SpringApplicationBuilder(Application.class).web(true).run(args);}}
Create an interface to consume the interface provided by Eureka-client
@RestControllerpublic class DcController {@AutowiredLoadBalancerClient loadBalancerClient;@AutowiredRestTemplate restTemplate;@GetMapping("/consumer")public String dc() {ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client");String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc";System.out.println(url);return restTemplate.getForObject(url, String.class);}}
As you can see here, we inject loadbalancerclient and resttemplate, and in the implementation of the/consumer interface, First, a Eureka-client service instance is selected through the Choose function of Loadbalancerclient, which is stored in serviceinstance, and the information in these objects is then spliced out to access/ The detailed address of the DC interface, and finally uses the Resttemplate object 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 consumes the/DC interface of the Eureka-client service.
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.
Spring Cloud builds MicroServices architecture: Service Consumption (base)