Ribbon是一个基于Http和TCP的负载均衡工具。
一、环境准备
在上一篇博客的基础上,开启 Eureka–Server 与 Eureka–Client项目。Eureka服务注册与服务发现
Eureka–Client 项目开启后,将配置文件中的端口从8762改为8763,再启动一次。这好比启动了两个服务器,模拟一个分布式项目,同一个项目分别部署在8762端口与8763端口。
IDEA开启多实例方式如下:
1.1在IDEA上点击Application右边的下三角,弹出选项后,点击Edit Configuration
1.2打开配置后,选择需要开启的Application,打钩Allow running in parallel,再保存。
启动后,访问http://localhost:8761/,在注册中心中应该就能看到同一个服务器名,有两个对应的端口。
二、建一个服务消费者
右击项目根目录,New–Module–选择左侧的Maven,点击next–输入新模块的名字ribbon。
1.1导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>main</artifactId>
<groupId>com.safesoft</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ribbon</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies>
</project>
2.2编写application.yml配置文件
#当前项目要被部署的端口
#8761是注册中心 8762是服务提供者一号 8763是服务提供者二号
server:
port: 8764
#当前ribbon项目注册到下面配置的注册中心中
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#当前项目名
spring:
application:
name: service-ribbon
2.3编写启动类
@EnableEurekaClient表明这是一个服务提供者
@EnableDiscoveryClient表示向服务中心注册
@LoadBalanced表明RestTemplate工具类将会负载均衡
package com.safesoft.ribbon;
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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ApplicationRibbon {
public static void main(String[] args) {
SpringApplication.run( ApplicationRibbon.class, args );
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
2.4编写服务接口
HelloController与HelloService,在Service中,访问其它服务器的接口。
HelloController.class
package com.safesoft.ribbon.web;
import com.safesoft.ribbon.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("hello")
public String hello(String name){
//调用Service接口,并返回JSON数据
return helloService.obtainOtherServiceData(name);
}
}
HelloService.class
package com.safesoft.ribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloService {
/**
* 将IOC容器中的负载均衡RestTemplate工具注入进来
*/
@Autowired
RestTemplate restTemplate;
public String obtainOtherServiceData(String name) {
//尝试调用其它微服务接口,访问的是SERVICE-CLIENT这个服务器的/hi 接口
return restTemplate.getForObject("http://SERVICE-CLIENT/hello?name=" + name, String.class);
}
}
2.5启动ribbon项目
在浏览器上输入:http://localhost:8764/hello?name=pc,第一次刷新结果如下:
第二次刷新结果如下:
多次刷新发现:发送给8764端口的请求,被分发到SERVICE-CLIENT项目。且分别交替发给部署在8762与8763的服务器。
三、源码下载和目录结构
1.1项目源码
2.1项目整体目录
参考文章
https://blog.csdn.net/yanluandai1985/article/details/86628111
https://blog.csdn.net/forezp/article/details/76408139
https://www.jianshu.com/p/59295c91dde7
https://blog.csdn.net/xiaojin21cen/article/details/86704954