Springcloud (sixth article) Springcloud Ribbon

Source: Internet
Author: User

Introduction to Spring Cloud ribbon

ribbonFor load balancing, and for soft load balancing, the core has three points:

    • Service discovery, discovering a list of dependent services
    • Service selection rules, how to select a valid service in multiple services
    • Service monitoring, detection of failed services, efficient elimination of failure services
Netflix Ribbon

A simple Demo

Configuration file:

# Max Number of retries on the same server (excluding the first try)Sample-client. Ribbon. Maxautoretries=1# Max Number of next servers to retry (excluding the first server)Sample-client. Ribbon. Maxautoretriesnextserver=1# Whether All operations can is retried for the This clientSample-client. Ribbon. Oktoretryonalloperations=true# Interval to refresh the server list from the sourceSample-client. Ribbon. Serverlistrefreshinterval= -# Connect Timeout used by Apache HttpClientSample-client. Ribbon. ConnectTimeout= the# Read Timeout used by Apache HttpClientSample-client. Ribbon. ReadTimeout= the# Initial List of servers, can be changed via Archaius dynamic @ runtimeSample-client. Ribbon. Listofservers=www. U51. Xin: theWww. Baidu. com: theWww. 163. com: theWww. Csdn. NET: the

Configuration format is clientName.spaceName.params
Core configuration sample-client.ribbon.listOfServers indicates a list of valid services


Packagecom. LKL. Springcloud. Ribbon;Importcom. Netflix. Client. Clientfactory;Importcom. Netflix. Client. HTTP. HttpRequest;Importcom. Netflix. Client. HTTP. HttpResponse;Importcom. Netflix. config. ConfigurationManager;Importcom. Netflix. LoadBalancer. Zoneawareloadbalancer;Importcom. Netflix. NIWS. Client. HTTP. Restclient;Import Java. NET. URI;/** * Created by Liaokailin on 16/5/6. * *public class Basictest {public static void main (string[] args) throws exception{ConfigurationManager. Loadpropertiesfromresources("Sample-client.properties");//1System. out. println(ConfigurationManager. Getconfiginstance(). GetProperty("Sample-client.ribbon.listofservers"));Restclient client = (restclient) clientfactory. Getnamedclient("Sample-client");//2HttpRequest request = HttpRequest. Newbuilder(). Uri(The New URI ("/")). Build();//3for (int i =0; i <; i++) {HttpResponse response = Client. Executewithloadbalancer(Request);//4System. out. println("Status code for"+ Response. Getrequesteduri() +"  :"+ Response. GetStatus());} zoneawareloadbalancer lb = (zoneawareloadbalancer) client. Getloadbalancer();System. out. println(LB. Getloadbalancerstats());ConfigurationManager. Getconfiginstance(). SetProperty("Sample-client.ribbon.listofservers","Www.qq.com:80,www.u51.xin");//5System. out. println("Changing servers ...");Thread. Sleep( the); for (int i =0; i <; i++) {HttpResponse response = Client. Executewithloadbalancer(Request);System. out. println("Status code for"+ Response. Getrequesteduri() +"  : "+ Response. GetStatus());} System. out. println(LB. Getloadbalancerstats());//6}}
    • 1. Loading configuration files via Archaius
    • 2. By ClientFactory obtaining the specified name of the client, the method obtains the LoadBalancer
    • 3. Build the request
    • 4. Execute LoadBalancer, you will find that the invoked service will switch the data in the listOfServers list
    • 5. Modify the list of available services
    • 6. Get LoadBalancer Status

ribbonThere are three core components

    • Rule-How to get a valid service from the list of services
    • Ping-The background run thread is used to determine if a service is available
    • ServerList-List of services
Spring Cloud Ribbon

Archaius Default Load Configuration config.properties

Ribbon configuration Information



application.propertiesConfigure the client name


corresponding Java class

Packagecom. LKL. Springcloud. Ribbon;Importcom. Netflix. LoadBalancer. Server;Importcom. Netflix. LoadBalancer. ServerList;Importcom. Netflix. LoadBalancer. Zoneawareloadbalancer;import org. Springframework. Beans. Factory. Annotation. Autowired;import org. Springframework. Boot. Springapplication;import org. Springframework. Boot. Autoconfigure. Springbootapplication;import org. Springframework. Boot. Builder. Springapplicationbuilder;import org. Springframework. Cloud. Netflix. Ribbon. Ribbonclients;import org. Springframework. Cloud. Netflix. Ribbon. Springclientfactory;import org. Springframework. Util. Collectionutils;import org. Springframework. Web. Bind. Annotation. Requestmapping;import org. Springframework. Web. Bind. Annotation. Restcontroller;Import Java. Util. List;/** * Created by Liaokailin on 16/5/6. * *@SpringBootApplication @ribbonclients@restcontrollerpublic class Ribbondefaultconfigapplication {@Autowired private Springclientfactory clientfactory;@RequestMapping ("/") public void Getserverlist () throws Exception {zoneawareloadbalancer<server> lb = (zoneawareloadbalancer <Server>) clientfactory. Getloadbalancer("MyClient");serverlist<server> serverlist = lb. Getserverlistimpl();list<server> serverdetaillist = serverlist. Getinitiallistofservers();if (! Collectionutils. IsEmpty(serverdetaillist)) {for (Server s:serverdetaillist) {System. out. println(s. GetHost() +","+ S. Getport());}} else {System. out. println("No service");}} public static void Main (string[] args) {springapplication app = new Springapplicationbuilder (Ribbondefau Ltconfigapplication. Class). Build();App. Run(args);}}

Using @RibbonClients annotations

In the getServerList method, get the first, ZoneAwareLoadBalancer then get serverlist, get to ServerList can choose a valid service to make the call.

If you need to modify the configuration information, you can pass @RibbonClients the defaultConfiguration configuration in, for example:

@RibbonClients(defaultConfiguration=MyDefaultRibbonConfig.class)Where MyDefaultRibbonConfig.class you can refer toRibbonClientConfiguration.java

OK ~ it ' s work! More about are here

Springcloud (sixth article) Springcloud Ribbon

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.