Spring Cloud Consumer Services Ribbon (step on the pit and crawl forward)

Source: Internet
Author: User

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 methods of service invocation,

One is ribbon+resttemplate, the other is feign.

Ribbon, which mainly provides customer-side software load balancing algorithms.

The Ribbon client component provides a complete set of configuration options, such as connection timeouts, retries, retry algorithms, and so on. The ribbon incorporates pluggable, customizable load balancing components. Here are some of the load balancing strategies that are used:
-Simple Poll Load balancing
-weighted response Time load balancing
-Zone-aware polling load balancing
-Random load Balancing

The Ribbon also includes the following features:
-Easy integration with service discovery components like Netflix's Eureka
-Use Archaius to complete runtime configuration
-use JMX to expose operational metrics and publish using servo
-Multi-pluggable serialization options

Change the port of the Service-hi configuration file to 8762 and start, and you will find that Service-hi registered 2 instances in Eureka-server, which is equivalent to a small cluster. Visit localhost:8761:

But you'll find that idea doesn't start two times the same Main method class

That's all you have to do.

Build a service consumer

Re-create a new Spring-boot project, named: Service-ribbon;

 
 <  dependency  >  <  groupid  >  org.springframework.cloud</  GroupId  >  <  artifactid  >  Spring-cloud-starter-netflix-eureka-server</ artifactid  >  </ dependency  >  

The above node seems to include the Spring-cloud-starter-eureka dependency, so just add two nodes

Maybe it's life, okay?

Add two nodes to the Pom.xml

<Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-ribbon</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-web</Artifactid>        </Dependency>

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

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.

Package Cn.zhiwei;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.cloud.client.discovery.enablediscoveryclient;import Org.springframework.context.annotation.bean;import Org.springframework.web.client.resttemplate;import org.springframework.cloud.client.loadbalancer.LoadBalanced, @SpringBootApplication @enablediscoveryclientpublic Class Serviceribbonapplication {public static void main (string[] args) {Springapplication.run ( Serviceribbonapplication.class, args);} @Bean @loadbalanced//must give this note, otherwise it will not recognize the path 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:

Package Cn.zhiwei.service;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import org.springframework.web.client.resttemplate;/** * Created by Administrator on 2018/4/6. */@Service//Plus this representation is managed by the spring container public class HelloService {    @Autowired    resttemplate resttemplate;    public string Hiservice (string name) {        return Resttemplate.getforobject ("Http://SERVICE-HI/hi?name=" +name, String.class);}    }

  

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

Package Cn.zhiwei.controller;import Cn.zhiwei.service.helloservice;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import org.springframework.web.bind.annotation.restcontroller;/** * Created by Administrator on 2018/4/6. */@RestControllerpublic class Hellocontroler {    @Autowired    helloservice helloservice;    @RequestMapping (value = "/hi") Public    String hi (@RequestParam String name) {        return Helloservice.hiservice ( name);}    }

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

Hi Brother Wai, I am from Port:8762hi Brother Wai, I am from port:8763

Project Structure diagram

Spring Cloud Consumer Services Ribbon (crawl forward)

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.