Spring Cloud builds service consumption base for MicroServices architecture

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):
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. @EnableDiscoveryClientannotations 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.

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 service consumption base for MicroServices architecture

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.