"MicroServices" four: Easy fix springcloud microservices-load Balancing Ribbon

Source: Internet
Author: User
Tags connection pooling server port

Load balancing is an essential name for any high-availability, high-load system. In a large-scale distributed computing system, a service in a single case, it is difficult to deal with a variety of unexpected situations. Therefore, load balancing is a solution that allows the system to distribute traffic in the event of a performance bottleneck or in some of these states. In the Springcloud system, many of Netflix's best products have been added, one of which is the Ribbon for server-side load balancing.

This series of posts directory

"MicroServices" three: Easy to fix springcloud micro-service Catalog
This series is a serial article and it is strongly recommended that you read the previous few before reading this article.

About Us Load Balancing introduction

Load Balancing : The English name is load Balance, built on the existing network structure, it provides a cheap and effective way to expand network equipment and server bandwidth, increase throughput, enhance network data processing capacity, improve network flexibility and availability. This means that it can be shared across multiple operating units, such as Web servers, FTP servers, enterprise critical application servers, and other mission-critical servers, to work together to accomplish tasks.
The benefits of load balancing are obvious:

Ribbon Introduction

The Ribbon is an open source Netflix software for client-side soft load balancing. Spring Cloud encapsulates the ribbon to better use Spring Boot's automated configuration concept.

Introduction to Spring Cloud Ribbon

The Spring Cloud Ribbon is a set of client-side load balancing tools implemented on the Netflix ribbon. It is a client load balancer based on HTTP and TCP. It can set the server-side list to poll access to achieve a balanced load by configuring Ribbonserverlist in the client.

Start takeoff

Before taking off, let me explain that the related sub-projects have been built in the previous articles of this project including: Registry, Configuration Center. You can continue to use this article.

Create two servers

You need to create two identical servers to distribute the client according to different mechanisms to achieve a load-balancing effect. We have agreed on two sub-project names:
cloud-hyh-service-1 port number: 8071
cloud-hyh-service-2 port number: 8072
For service name settings:cloud-service , other businesses are the same and can be replicated. "The port number is not the same"

Pom.xml file Configuration
<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-test</artifactId>        <scope>Test</scope>    </dependency></dependencies><build>    <plugins>        <plugin>            <groupId>Org.springframework.boot</groupId>            <artifactId>Spring-boot-maven-plugin</artifactId>        </plugin>    </plugins></build>
Server one parameter configuration
#服务注册中心配置eureka:  client:    service-url:      defaultZone: http://localhost:8081/eureka/  instance:    appname: cloud-service    lease-renewal-interval-in-seconds: 1server:  port: 8071spring:  application:    name: cloud-service
Server two-parameter configuration
#服务注册中心配置eureka:  client:    service-url:      defaultZone: http://localhost:8081/eureka/  instance:    appname: cloud-serviceserver:  port: 8072spring:  application:    name: cloud-service

Description : Basically the same as configuration one, just configure the port number to 8072

Server Portal Configuration Application.yml
/** * @Description :  * @Author hanyahong * @Date 2017/12/7- 17:35 */@SpringBootApplication@EnableDiscoveryClientpublicclass ServiceTwoApplication {    publicstaticvoidmain(String[] args) {        SpringApplication.run(ServiceTwoApplication.class, args);    }}
New Test API Class
/*** @Description: Test ribbontest API* @Author Hanyahong* @Date 2017/12/7-17:40 */@RestController@RequestMapping(Value ="/ribbon") Public classRibbontestapi {/*** Get blog name API     *     * @returnRelated Information     */    @RequestMapping(Value ="Name", method = Requestmethod.GET) PublicStringGetmyblognameapi() {return "The road is just beginning to-www.hanyahong.com-beijing"+"This server port number: 8071"; }} Note: Two servers, in addition to the server port number returned8071 8072Different, the others are the same, just to see the effect.
To create a test client

Create a subproject, cloud-hyh-ribbon-client, used primarily to test ribbon client load.

Pom file Configuration

Include the following dependencies in the Pom file:

<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-test</artifactId>        <scope>Test</scope>    </dependency></dependencies><build>     <plugins>         <plugin>             <groupId>Org.springframework.boot</groupId>             <artifactId>Spring-boot-maven-plugin</artifactId>         </plugin>     </plugins> </build>
Configuration file application Configuration
eureka:  client:    service-url:      defaultZone: http://localhost:8081/eureka/  instance:    appname: ribbon-clientserver:  port: 8092spring:  application:    name: ribbon-client
Configuring Subproject Startup Classes
/*** @Description: Start class, demo load balancer Server* @Author Hanyahong* @Date 2017/12/7-17:00 */@SpringBootApplication@EnableDiscoveryClient Public classribbonserviceapplication { Public Static void Main(string[] args) {springapplication.Run(Ribbonserviceapplication.class, args); }/*** Spring provides clients to access the rest service* @return     */    @Bean    @LoadBalancedResttemplateresttemplate() {return New resttemplate(); }}

Description

resttemplate is the client that spring provides to access the rest service. Resttemplate provides a variety of convenient ways to access remote HTTP services, which can greatly improve the efficiency of client writing. Calling Resttemplate's default constructor, the Resttemplate object creates HTTP requests at the bottom by using implementations under the Java.net package, and can specify different HTTP request methods by using Clienthttprequestfactory.
Clienthttprequestfactory interface mainly provides two kinds of implementation, one is Simpleclienthttprequestfactory, The underlying HTTP request connection is created using the J2SE provided by the java.net package, and the other way is to use the Httpcomponentsclienthttprequestfactory method to access the remote HTTP service using HttpClient, using the H Ttpclient can configure information such as connection pooling and certificates.

* * @LoadBalanced * * Note added to Resttemplate, this annotation automatically constructs the implementation class of the Loadbalancerclient interface and registers it with the spring container.

Creating an Interface API
/*** @Description: Testing the client Load Balancer interface API* @Author Hanyahong* @Date 2017/12/7-18:01 */@RestController@RequestMapping(Value ="/test") Public classTestribbonapi {/*** Inject Resttemplate     */    @AutowiredResttemplate resttemplate;@RequestMapping(Value ="/blog/name", method = Requestmethod.GET) PublicStringTestgetnameofblog() {String url="Http://CLOUD-SERVICE/ribbon/name";returnResttemplate.Getforobject(url,string.class); }}

Note: The URL in this code is set to the service name of the server mentioned above.

Start a program to test

Fully configured, the server is fully configured, including a registry, a configuration center, two identically configured servers, and a test server for testing client load balancing.
It will be seen in the registry after successful startup.

By accessing the client address: Http://localhost:8092/test/name can be accessed. The effect is as follows:

Refresh one time:

At this point all configurations are successful. The test results were also successful.

This article source

GitHub Source: Https://github.com/hanyahong/spring-cloud-microservice

"MicroServices" four: Easy fix springcloud microservices-load Balancing 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.