1.建立兩個provider:springcloud-provider-dept-8002
2. 配置跟8001一樣
整合 Ribbon
由上述可知,Ribbon 是需要內建在消費端的
是以在消費端 : springcloud-03-consumer-dept-8082 進行修改
在 POM 檔案中添加 Ribbon、Eureka 依賴
<!--ribbon-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<!--Eureka 服務提供者-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
編寫 application.yml
# Eureka
eureka:
client:
# 不向注冊中心注冊自己
register-with-eureka: false
# 配置 可連接配接的注冊中心
service-url:
defaultZone: http://127.0.0.1:7001/eureka/,http://127.0.0.2:7002/eureka/
在主啟動類上添加 Eureka 啟動項 : @EnableEurekaClient
在之前把 RestTemplate 注冊到 Bean 的配置方法上添加一個注解
配置 負載均衡實作 RestTemplate : @LoadBalanced
修改控制層
/**提供者 URL 的字首
*
* 不使用 Ribbon 時 ,這裡就是第一中寫法
*
* 使用 Ribbon 實作負載均衡 時,這裡就不能寫死為一個位址,
* 而需要通過注冊中心的服務名來通路
* 服務名:在 提供者 YML 檔案中配置的 spring:application:name: 的值
* 或者 監控頁面的 Application 字段值
*/
//private static final String REST_URL_PREFIX = "http://localhost:8081";
private static final String REST_URL_PREFIX = "http://SpringCloud-02-provider";
啟動叢集、提供者、消費者,進行測試
進入 消費者頁面 送出請求,得到正确結果
2.2 添加提供者,觀察負載均衡
添加一個資料庫 : spring_cloud_02
建立一個 服務提供者 : springcloud-02-provider-dept-8083
把另一個 提供者 的檔案都複制過來,再做修改
修改 application.yml (端口号、資料庫、描述資訊)
【注意】多個提供者的服務名必須一緻
server:
port: 8083
mybatis:
type-aliases-package: com.demo.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
spring:
application:
name: SpringCloud-02-provider
datasource:
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring_cloud_02?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
type: com.alibaba.druid.pool.DruidDataSource
logging:
level:
com.demo.mapper: DEBUG
# Eureka,配置服務注冊到哪裡
eureka:
client:
service-url:
# 配置監控頁面的位址,這是在 Eureka Server 中配置的
defaultZone: http://127.0.0.1:7001/eureka/,http://127.0.0.2:7002/eureka/
instance:
# 修改 Eureka 監控頁面上的預設描述資訊
instance-id: springcloud-provider-dept-8083
修改主啟動類,添加注解
//在配置過這個注解後,服務啟動會自動注冊到 Eureka Server
@EnableEurekaClient
//開啟服務發現
@EnableDiscoveryClient
2.3 修改負載均衡政策
負載均衡有一個核心的結構 : IRule
進入該接口
實作類
修改為已經實作的政策
因為是在消費者端做負載均衡,是以在消費者中修改
把已經實作的政策注冊的 Bean 中即可
修改 springcloud-03-consumer-dept-8082 的 BeanConfig 檔案
@Bean
public IRule myRule(){
// 先使用已經實作的政策——随機
return new RandomRule();
}
重建消費者,重新整理請求,會發現不再輪詢,會在已有的提供者之間随機選擇
修改配置 Ribbon 的方式
建立一個 MyRule 類,并且把上面那個 myRule 方法挪過去
【注意】:這個 MyRule 類 不能在主應用程式的上下文(也就是 主啟動類的同級目錄中),是以需要單獨的建立一個包
@Configuration
public class MyRule {
/**
* 修改預設的負載均衡政策
*/
@Bean
public IRule customize(){
// 先使用已經實作的政策——随機
return new RandomRule();
}
}
在主啟動類上加上 Ribbon 的注解:
@RibbonClient
@SpringBootApplication
@EnableEurekaClient
// configuration:标注 Rule 的配置類 ; name:标注需要配置的服務名
@RibbonClient(configuration = MyRule.class,name = "SpringCloud-02-provider")
public class Springcloud03ConsumerDept8082Application {
public static void main(String[] args) {
SpringApplication.run(Springcloud03ConsumerDept8082Application.class, args);
}
}
自定義政策(簡單示例)
可以點開剛剛看的那個 RandomRule 的源代碼,複制過來修改一下
修改要求:每個提供者通路五次
編寫 DiyRule()
public class DiyRule extends AbstractLoadBalancerRule {
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
} else {
Server server = null;
while(server == null) {
if (Thread.interrupted()) {
return null;
}
// 獲得可獲得(活着的)的服務
List<Server> upList = lb.getReachableServers();
// 獲得所有的服務
List<Server> allList = lb.getAllServers();
int serverCount = allList.size();
if (serverCount == 0) {
return null;
}
//==上面是寫死的======中間是修改部分==================
System.out.println("自定義的 Rule");
System.out.println(upList.size());
// 通路某一個提供者的次數
int times = 0;
// 提供者的下标
int index = 0;
// 從活着的服務中随機擷取一個
server = (Server)upList.get(index);
if (times < 4){
times++;
}else {
times = 1;
index = (index + 1) % upList.size();
}
//==下面是寫死的======中間是修改部分===================
if (server == null) {
Thread.yield();
} else {
if (server.isAlive()) {
return server;
}
server = null;
Thread.yield();
}
}
return server;
}
}
@Override
public void initWithNiwsConfig(IClientConfig iClientConfig) {
}
@Override
public Server choose(Object o) {
return null;
}
}
MyRule 類
@Configuration
public class CustomizedRule {
/**
* 修改預設的負載均衡政策
*/
@Bean
public IRule customize(){
// 先使用已經實作的政策——随機
return new DiyRule();
}
}
在主啟動類上添加
// configuration:标注 Rule 的配置類 ; name:标注需要配置的服務名
@RibbonClient(name = "SPRINGCLOUD-PROVIDER", configuration = CustomizedRule.class)