Spring Cloud builds MicroServices architecture: Service Consumption (base)

Source: Internet
Author: User
Tags abstract definition

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):
<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:
spring.application.name=eureka-consumerserver.port=2101eureka.client.serviceUrl.defaultZone= http://localhost:1001/eureka/
    The
    • creates the application main class. Initializes the resttemplate , which is used to actually initiate a rest request. The @EnableDiscoveryClient annotation is used to add the current application to the service governance architecture.
@EnableDiscoveryClient @springbootapplication  Public class Application {@Bean public resttemplate resttemplate () {returnnew  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:
  @RestController  public  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.  

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 technical support for complete projects 1791743380

Spring Cloud builds MicroServices architecture: Service Consumption (base)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.