Ribbon是客户端的负载均衡机制,它有几种负载均衡机制。默认是轮询,我们也可以自定义规则。通过合理的分配网络请求来减小服务器的压力。项目都是注册到eureka服务器上。通过ribbon去调用其他服务。
项目搭建:
1. 导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<!-- springcloud依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
2. 写配置文件
# 项目访问路径前缀
server:
context-path: /demo
# 设置服务名称,服务会以这个名字注册到eureka服务器上
spring:
application:
name: demo
# 设置eureka服务器的注册地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3. 开发服务接口
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping("/data")
public String testData(HttpServletRequest req){
String url = req.getRequestURL().toString();
return "提供服务的是: "+url;
}
}
4. 启动主函数,测试。
import java.util.Scanner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//申明eureka客户端
@EnableEurekaClient
public class Main {
public static void main(String[] args) {
//1. 启动main方法,在控制台输入端口号
Scanner scan = new Scanner(System.in);
String port = scan.nextLine();
//项目以输入的端口号启动
new SpringApplicationBuilder(Main.class).properties("server.port="+port).run(args);
//2. 启动两次main方法,分别以8080 8081启动,模拟集群的环境 浏览器访问接口测试下。
}
}
二:搭建Ribbon项目
1. 导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<!-- springcloud依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- ribbon依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
2. 配置application.yml文件
# 指定默认端口
server:
port: 8083
# 设置服务名称,服务会以这个名字注册到eureka服务器上
spring:
application:
name: ribbon
# 设置eureka服务器的注册地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3. 编写controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class Controller {
@Autowired
private RestTemplate restTpl;
@RequestMapping("/testRibbon")
public String testRibbon() {
String data = restTpl.getForObject("http://demo/demo/data", String.class);
//String data = restTpl.getForObject("http://127.0.0.1:8081/demo/data", String.class);
//User user = restTpl.getForObject("http://demo/demo/user/{id}", User.class,id); return data;
}
}
4. 编写主函数入口, 配置RestTemplate
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
//申明eureka客户端
@EnableEurekaClient
public class RibbonMain {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonMain.class, args);
}
}
测试:我们通过 ribbon来调用其他服务数据,可以看到它默认是走轮询策略。