一、理解负载均衡
负载均衡个人理解:是指,将要处理的数据发送给中介,中介通过算法分摊到多个可以处理该数据的服务上去处理。
一般分为硬件负载均衡和软件负载均衡。硬件负载均衡主要通过在服务器节点之间安装专门用于负载均衡的设备,比如 F5 等;软件负载均衡则是通过在服务器上安装一些具有均衡负载功能或模块的软件来完成请求分发工作, 比如Nginx 等。
二、搭建Spring Cloud Ribbon负载均衡
1.搭建服务注册中心,具体步骤请参考:https://blog.csdn.net/notMoonHeart/article/details/84949475
2.注册服务,创建两个Spring Boot工程(具体pom.xml文件和配置文件信息内容参见步骤1链接里的服务端内容),并写Controller类,如图所示:
代码分别如下:
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value="/hello")
public String index(){
return "hello server1";
}
}
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value="/hello")
public String index(){
return "hello server2";
}
}
注意:这里返回不同,主要是为了后续更直观的看到赋值均衡的效果。
3.搭建客户端
①创建Sprint Boot工程,命名为hello-client,并设置pom.xml文件,代码如下:
4.0.0
hello-client
hello-client
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.cloud
spring-cloud-starter-eureka
1.4.0.RELEASE
org.springframework.cloud
spring-cloud-starter-ribbon
1.3.5.RELEASE
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
org.springframework.boot
spring-boot-maven-plugin
②创建客户端发送数据类,命名为ClientController.java,代码如下:
package com.example.client.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ClientController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value="/ribbon-client",method=RequestMethod.GET)
public String helloTemplate() {
return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
}
}
③更改启动类,加入注册服务注解,并创建实例开启负载均衡,代码如下:
package com.example.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class HelloClientApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(HelloClientApplication.class, args);
}
}
④设置配置文件application.properties,详细信息如下:
server.port=9100
spring.application.name=ribbon-client
eureka.client.service-url.defaultZone=http://localhost:8088/eureka/eureka/
4.按顺序启动工程,
①首先启动服务注册中心,然后依次启动两个server工程,启动完成,访问http://localhost:8088/eureka/效果如下:
说明两个服务以及在服务注册中心注册成功了。
②启动客户端工程,即hello-client工程,访问http://localhost:9100/ribbon-client,效果图如下:
然后不断刷新页面,返回结果会在hello server1和hello server2之间切换,这下是否对负载均衡有一个初步的认识呢?