Amateur Grass Springcloud Tutorial | Article Two: Service Consumers (Rest+ribbon) (Finchley version)

Source: Internet
Author: User

In the previous article, I spoke about the registration and discovery of services. In a microservices architecture, the business is split into a separate service, and the service-to-service communication is based on HTTP restful. Spring Cloud has two modes of service invocation, one ribbon+resttemplate and the other feign. In this article, we first explain the following based on Ribbon+rest.

I. INTRODUCTION of the Ribbon

The Ribbon is a client side load balancer which gives your a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you is using @FeignClient then this section also applies.

--From official website

The Ribbon is a load balancer client that can control some of the behavior of HTT and TCP very well. The feign is integrated with the Ribbon by default.

The ribbon has implemented these configuration beans by default:

    • Iclientconfig Ribbonclientconfig:defaultclientconfigimpl

    • IRule Ribbonrule:zoneavoidancerule

    • IPing ribbonping:noopping

    • ServerList Ribbonserverlist:configurationbasedserverlist

    • Serverlistfilter Ribbonserverlistfilter:zonepreferenceserverlistfilter

    • Iloadbalancer Ribbonloadbalancer:zoneawareloadbalancer

Ii. preparatory work

This article is based on the project of the previous article, start the Eureka-server project, start the Service-hi project, its port is 8762, change the port of the Service-hi configuration file to 8763, and start At this point you will find: Service-hi registered 2 instances in Eureka-server, which is equivalent to a small cluster.

How to launch multiple instances under idea, please refer to this article:
76408139

Visit localhost:8761:
How to start multiple instances of a project, please see this article: 76408139

Third, build a service consumer

Re-create a new Spring-boot project, named: Service-ribbon;
In its pom.xml inherits the parent Pom file and introduces the following dependencies:

1<?xml version= "1.0" encoding= "UTF-8"?>2<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >4<modelVersion>4.0.0</modelVersion>5  6<groupId>com.forezp</groupId>7<artifactId>service-ribbon</artifactId>8<version>0.0.1-SNAPSHOT</version>9<packaging>jar</packaging>Ten   One<name>service-ribbon</name> A<description>demo Project forSpring boot</description> -   -   the<parent> -<groupId>com.forezp</groupId> -<artifactId>sc-f-chapter2</artifactId> -<version>0.0.1-SNAPSHOT</version> +</parent> -   +<dependencies> A<dependency> at<groupId>org.springframework.cloud</groupId> -<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> -</dependency> -<dependency> -<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-web</artifactId> in</dependency> -<dependency> to<groupId>org.springframework.cloud</groupId> +<artifactId>spring-cloud-starter-netflix-ribbon</artifactId> -</dependency> the</dependencies> *   $  Panax Notoginseng   -</project>

In the project configuration file, the registry address for the specified service is http://localhost:8761/eureka/, the program name is Service-ribbon, and the program port is 8764. The configuration file application.yml is as follows:

1 Eureka: 2   Client:3    serviceurl:4       defaultzone:http://  localhost:8761/eureka/5server:6   port:87647Spring:  8  application:9     Name:service-ribbon

In the startup class of the project, the service center is registered through @enablediscoveryclient, and a bean:resttemplate is injected into the IOC of the program; And the @loadbalanced annotation shows that this restremplate turns on the load balancing function.

@SpringBootApplication @enableeurekaclient@enablediscoveryclientpublic class Serviceribbonapplication {public     static void Main (string[] args) {        springapplication.run (serviceribbonapplication.class, args);    }     @Bean    @LoadBalanced    resttemplate resttemplate () {        return new resttemplate ();}    }

Write a test class HelloService, through the resttemplate that was injected into the IOC container to consume the Service-hi service "/hi" interface, where we directly use the program name instead of the specific URL address, In the ribbon it chooses the specific service instance according to the service name, and, depending on the service instance, replaces the service name with a specific URL at the time of the request, with the following code:

1 @Service2  Public classHelloService {3  4 @Autowired5 resttemplate resttemplate;6  7      Publicstring Hiservice (string name) {8         returnResttemplate.getforobject ("Http://SERVICE-HI/hi?name=" +name,string.class);9     }Ten   One}

Write a controller, in the controller with the call HelloService method, the code is as follows:

1  2 @RestController3  Public classHellocontroler {4  5 @Autowired6 HelloService HelloService;7  8@GetMapping (value = "/hi")9      Publicstring Hi (@RequestParam string name) {Ten         returnHelloservice.hiservice (name); One     } A}

Multiple accesses to Http://localhost:8764/hi?name=forezp on the browser, the browser alternately displays:

Hi forezp,i am from port:8762

Hi forezp,i am from port:8763

This means that when we call the Resttemplate.getforobject ("Http://SERVICE-HI/hi?name=" +name,string.class) method, we have already done a load balancing service instance that accesses different ports.

Iv. Architecture at this time

    • One service registry, Eureka Server, Port 8761
    • Service-hi Project ran two instances, the ports are 8762,8763, respectively, registered with the Service registration center
    • Sercvice-ribbon Port 8764, registering with the Service registration center
    • When Sercvice-ribbon calls the Hi interface of Service-hi through Resttemplate, because of the load balancing with the Ribbon, the Hi interface of service-hi:8762 and 87,632 ports is called in turn;

SOURCE Download: Https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter2

V. References

This article references the following:

Http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

Thank you for your attention! Add QQ1 Group: 135430763,QQ2 Group: 454796847,QQ3 Group: 187424846. QQ Group into the group password: xttblog, want to add a group of friends, you can search: Xmtxtt, note: "Xttblog", add Assistant pull you into the group. Note errors do not agree with a friend application. Thanks again for your attention! Follow up with wonderful content will be sent to you the first time! Please send the original article to [email protected] email. Business cooperation can add assistants to communicate!

Amateur Grass Springcloud Tutorial | Second article: service Consumer (Rest+ribbon) (Finchley version)

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.