HMdubbo8【dubbo高级:多版本、负载均衡】
1 多版本
-
灰度发布:当出现新功能时,会让一部分用户先使用新功能,用户反馈没问题时,再将所有用户迁移到新功能。
-
dubbo 中使用version 属性来设置和调用同一个接口的不同版本
package com.yppah.service.impl;
import com.yppah.pojo.User;
import com.yppah.service.UserService;
import org.apache.dubbo.config.annotation.Service;
//import org.springframework.stereotype.Service;
/**
* @Author: haifei
* @Date: 2022/10/9 0:07
*/
//@Service //定义bean:将该类的对象创建出来,放到spring的IOC容器中
@Service(version = "v1.0") //将该类提供的方法(服务)对外发布,将访问地址(ip,端口,路径)注册到注册中心中(zk)
public class UserServiceImpl implements UserService {
@Override
public String sayHello() {
return "hello dubbo! hihihi";
}
@Override
public User findUserById(int id) {
System.out.println("old version");
User user = new User(1, "zhangsan", "123");
return user;
}
/*@Override
public User findUserById(int id) {
// 查询User对象
User user = new User(1, "zhangsan", "123");
return user;
}*/
}
package com.yppah.service.impl;
import com.yppah.pojo.User;
import com.yppah.service.UserService;
import org.apache.dubbo.config.annotation.Service;
//import org.springframework.stereotype.Service;
/**
* @Author: haifei
* @Date: 2022/10/9 0:07
*/
//@Service //定义bean:将该类的对象创建出来,放到spring的IOC容器中
@Service(version = "v2.0") //将该类提供的方法(服务)对外发布,将访问地址(ip,端口,路径)注册到注册中心中(zk)
public class UserServiceImpl2 implements UserService {
@Override
public String sayHello() {
return "hello dubbo! hihihi";
}
@Override
public User findUserById(int id) {
System.out.println("new version");
User user = new User(1, "zhangsan", "123");
return user;
}
/*@Override
public User findUserById(int id) {
// 查询User对象
User user = new User(1, "zhangsan", "123");
return user;
}*/
}
package com.yppah.controller;
//import com.yppah.service.UserService;
import com.yppah.pojo.User;
import com.yppah.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: haifei
* @Date: 2022/10/9 0:30
*/
@RestController
@RequestMapping("/user")
public class UserController {
//@Autowired//本地注入
/*
1. 从注册中心zk获取useService的访问url
2. 进行远程调用RPC
3. 将结果封装为一个代理对象,并给变量赋值
*/
@Reference(version = "v1.0")//远程注入
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello(){
return userService.sayHello();
}
/**
* 根据id查询用户信息
* @param id
* @return
*/
/*@RequestMapping("/findUser")
public User findUser(int id){
return userService.findUserById(id);
}*/
@RequestMapping("/findUser")
public User findUser(int id){
return userService.findUserById(id);
}
}
2 负载均衡
dubbo-admin启动方法详见HMdubbo5【dubbo高级:dubbo-admin】 - yub4by - 博客园 (cnblogs.com)
负载均衡策略(4种):
- random :是默认策略,按权重随机,权重默认值100。按权重设置随机概率。
- roundRobin :按权重轮询。
- leastActive:最少活跃调用数,相同活跃数的随机。
- consistentHash:一致性 Hash,相同参数的请求总是发到同一提供者。
1&2:
3:
4:
策略1-权重随机-演示
package com.yppah.service.impl;
import com.yppah.pojo.User;
import com.yppah.service.UserService;
import org.apache.dubbo.config.annotation.Service;
/**
* @Author: haifei
* @Date: 2022/10/9 0:07
*/
@Service(weight = 100)//200、100
public class UserServiceImpl implements UserService {
@Override
public String sayHello() {
return "111111111";//2222222、33333333
}
@Override
public User findUserById(int id) {
System.out.println("old version");
User user = new User(1, "zhangsan", "123");
return user;
}
}
package com.yppah.controller;
import com.yppah.pojo.User;
import com.yppah.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: haifei
* @Date: 2022/10/9 0:30
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Reference(loadbalance = "random")//远程注入
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello(){
return userService.sayHello();
}
/**
* 根据id查询用户信息
* @param id
* @return
*/
@RequestMapping("/findUser")
public User findUser(int id){
return userService.findUserById(id);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫包-->
<!--<context:component-scan base-package="com.yppah.service" />-->
<!--dubbo配置-->
<!--1. 配置项目名称(唯一)-->
<dubbo:application name="dubbo-service" />
<!--2. 配置注册中心地址-->
<dubbo:registry address="zookeeper://192.168.1.8:2181" />
<!--3. 配置dubbo扫包-->
<dubbo:annotation package="com.yppah.service.impl" />
<!--本地端口配置-->
<!--<dubbo:protocol port="20880" />-->
<!-- 元数据配置 -->
<dubbo:metadata-report address="zookeeper://192.168.1.8:2181" />
</beans>
一号机器:
PS:默认是22222,不是55555;web微服务使用了33333,此前配置过
二号机器:
三号机器:
进行多次访问测试,发现1,2,3出现的概率基本趋向于1:2:1