HMdubbo7【dubbo高级:地址缓存、超时重试】

1 地址缓存

image-20221017215743377

image-20221017215808042

image-20221017215817617

image-20221017215827116

2 超时

2.1

image-20221017222339261

image-20221017222309477

image-20221017222357503

image-20221017222426231

2.2

image-20221020104508562

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 //将该类提供的方法(服务)对外发布,将访问地址(ip,端口,路径)注册到注册中心中(zk)
@Service(timeout = 3000, retries = 0) // 设置当前服务3秒超时(默认1秒),重试0次
public class UserServiceImpl implements UserService {

    @Override
    public String sayHello() {
        return "hello dubbo! hihihi";
    }

    @Override
    public User findUserById(int id) {
        // 查询User对象
        User user = new User(1, "zhangsan", "123");
        // 模拟数据库查询很慢,查了5秒-->超时
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        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//远程注入
    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);
    }*/
    int i = 1;
    @RequestMapping("/findUser")
    public User findUser(int id){
        // =============================================
        // 用于看服务超时效果
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(i++);
                    try {
                        Thread.sleep(1000); //1秒钟输出一次
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        // 预计控制台输出到3或4时,下面方法调用报错
        // =============================================
        return userService.findUserById(id);
    }
}

启动两个服务,访问测试

image-20221020104049531

image-20221020104002455


image-20221020104635699

重启两个服务,访问测试

image-20221020104726776


image-20221020104816971

image-20221020104919543

重启两个服务,访问测试(3秒生效?1秒生效?综合生效?)

image-20221020105105998

显然是1秒生效了

但是建议超时时间配置在服务提供者方(@Service(timeout = 3000, retries = 0)),不建议配置在服务消费者方(@Reference(timeout = 1000)

3 重试

3.1

image-20221020105655160

超时之后才会重试,正常不会重试

3.2

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 //将该类提供的方法(服务)对外发布,将访问地址(ip,端口,路径)注册到注册中心中(zk)
//@Service(timeout = 3000, retries = 0) // 设置当前服务3秒超时(默认1秒),重试0次
//@Service(retries = 0) // 设置当前服务3秒1秒,重试0次
@Service(timeout = 3000, retries = 2) // 设置当前服务3秒超时(默认1秒),重试2次(默认值)(即三次,包含第一次)
public class UserServiceImpl implements UserService {

    @Override
    public String sayHello() {
        return "hello dubbo! hihihi";
    }

    int i = 1;
    @Override
    public User findUserById(int id) {
        System.out.println("服务被调用了:" + i++);
        // 查询User对象
        User user = new User(1, "zhangsan", "123");
        // 模拟数据库查询很慢,查了5秒-->超时
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return user;
    }

    /*@Override
    public User findUserById(int id) {
        // 查询User对象
        User user = new User(1, "zhangsan", "123");
        return user;
    }*/
}

重启微服务,访问测试
image-20221020111619306

posted @   yub4by  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示