Springcloud Tutorials | 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:

<?xml version= "1.0" encoding= "UTF-8"?><Projectxmlns="http://maven.apache.org/POM/4.0.0"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" ><modelversion>4.0.0</Modelversion><Groupid>com.forezp</Groupid><Artifactid>service-ribbon</Artifactid><Version>0.0.1-snapshot</Version><Packaging>jar</Packaging><Name>service-ribbon</Name><Description>demo Project for Spring Boot</Description><Parent><Groupid>com.forezp</Groupid><Artifactid>sc-f-chapter2</Artifactid><Version>0.0.1-snapshot</Version></Parent><Dependencies><Dependency><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> </ dependency> <dependency> <groupid>org.springframework.cloud</ groupid> <artifactId> Spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </ dependencies></project>  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

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:

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/server:  port: 8764spring:  application:    name: service-ribbon
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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.

 @SpringBootAppli Cation @EnableEurekaClient  @EnableDiscoveryClient Span class= "Hljs-keyword" >public class serviceribbonapplication {public  Static void main (string[] args) {Springapplication.run ( Serviceribbonapplication.class, args); }  @Bean  @LoadBalanced resttemplate Resttemplate () { Span class= "Hljs-keyword" >return new resttemplate ();}}      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

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:

 @Service public class helloservice { @Autowired resttemplate resttemplate; public string hiservice (String name) { Return Resttemplate.getforobject ( "http://SERVICE-HI/hi?name=" +name,string.class);}}  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

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

 @RestController public class hellocontroler { @Autowired HelloService HelloService;  @GetMapping (value = public String hi (@RequestParam string name) {return Helloservice.hiservice (name); }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

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://blog.csdn.net/forezp/article/details/69788938

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

Original address: Http://blog.csdn.net/forezp. 81040946

Springcloud Tutorials | Article Two: Service Consumers (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.