搜索

HMdubbo8【dubbo高级:多版本、负载均衡】


发布时间: 2022-11-24 18:00:03    浏览次数:9 次

1 多版本

image-20221020112842710

image-20221020112856748

image-20221020112908609

  • 灰度发布:当出现新功能时,会让一部分用户先使用新功能,用户反馈没问题时,再将所有用户迁移到新功能。

  • dubbo 中使用version 属性来设置和调用同一个接口的不同版本


image-20221020114132503

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);
    }
}

image-20221020114255726

image-20221020114027371

image-20221020114004902

image-20221020114308729

image-20221020114050989

2 负载均衡

dubbo-admin启动方法详见HMdubbo5【dubbo高级:dubbo-admin】 - yub4by - 博客园 (cnblogs.com)

负载均衡策略(4种):

  • random :是默认策略,按权重随机,权重默认值100。按权重设置随机概率。
  • roundRobin :按权重轮询。
  • leastActive:最少活跃调用数,相同活跃数的随机。
  • consistentHash:一致性 Hash,相同参数的请求总是发到同一提供者。

1&2:

image-20221020134221233

3:

image-20221020140331100

4:

image-20221020140402706


策略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>

一号机器:

image-20221020140835835

image-20221020140903726

image-20221020141015265

PS:默认是22222,不是55555;web微服务使用了33333,此前配置过

二号机器:

image-20221020141053266

image-20221020141108549

image-20221020141146719

image-20221020141216593

image-20221020141247141

三号机器:

image-20221020141349035

image-20221020141407756

image-20221020141457572

image-20221020141604956

image-20221020141641449

image-20221020141721997

image-20221020141755139

进行多次访问测试,发现1,2,3出现的概率基本趋向于1:2:1

免责声明 HMdubbo8【dubbo高级:多版本、负载均衡】,资源类别:文本, 浏览次数:9 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 06:00:03。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/yppah/p/16809734.html