2、Ribbon负载均衡
Ribbon在工作时分成两步第一步先选择 EurekaServer ,它优先选择在同一个区域内负载较少的server.
第二步再根据用户指定的策略,在从server取到的服务注册列表中选择一个地址。其中Ribbon提供了多种策略:比如轮询、随机和根据响应时间加权。
1)参考microservicecloud-provider-dept-8001,新建两个provider8002,8003,创建略
2)新建8002/8003数据库,各自微服务分别连各自的数据库,参考微服务概念(拥有独立的数据库)
pom文件
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jiatp.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-provider-dept-8002</artifactId>
<dependencies>
<dependency><!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
<groupId>com.jiatp.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</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>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--添加到eureka客户端 将provider注册进去 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--springboot2.x之Actuator应用监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
3)配置yml
server:
port: 8002
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.jiatp.springcloud.entity # 扫描此包下的entity ->所有entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件
spring:
application:
name: microservicecloud-dept # 很重要,对外暴露的微服务名字
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 数据源类型
driver-class-name: org.gjt.mm.mysql.Driver # 数据库驱动包
url: jdbc:mysql://localhost:3306/cloudDB02?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
username: root
password: 123456
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-idle: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间
eureka:
client: # 客户端注册进eureka服务列表内
serviceUrl:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: microservicecloud-dept8002 # 自定义服务名称信息
prefer-ip-address: true # 访问路径可以显示IP地址
info:
app.name: jiatp-microservicecloud
company.name: www.jiatp.club
build.artifactId: $project.artifactId$
build.version: $project.version$
8003微服务的yml同理
**注意:**端口、数据库url、分别对应不同的微服务,对外暴露的统一的服务实例名:
spring:
application:
name: microservicecloud-dept # 很重要,对外暴露的微服务名字
启动3个eureka集群,测试三个provider,再启动microservicecloud-consumer-dept-80
客户端通过Ribbo完成负载均衡并访问上一步的Dept微服务,运行http://localhost/consumer/dept/list,然后不短刷新观察:db_source:字段,各不相同,负载均衡实现
总结:Ribbon是一个软负载均衡的客户端组件,可以和其他所需请求的客户端结合使用,和eureka结合只是其中的一个实例。