更新redis数据的操作集成到dubbo,其它地方调用

该博客展示了如何在Dubbo接口中定义及实现服务,包括获取、更新和删除Redis中的数据。接着,演示了在SpringBoot工程中如何引用并调用这些接口。最后,提供了在Struts2框架中配置Dubbo接口并进行调用的方法。主要涉及Spring Boot、Dubbo服务、Struts2和Redis操作。
摘要由CSDN通过智能技术生成

1.在dubbo的interface工程service层结构下写入接口

public interface LqSystemSetConfigDubboService {
    //获取redis中的value值
    SystemSetConfigDubbo getByKey(Long orgId);

    //更新redis数据
    boolean setRedis(Long orgId);

    //根据key键删除redis
    boolean deletedRedis(Long orgId);

}

2.在dubbo的provider工程service层下写入实现类

@Transactional 
@Component    
@org.springframework.stereotype.Service("lqSystemSetConfigDubboService")
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = LqSystemSetConfigDubboService.class)
public class LqSystemSetConfigDubboServiceImpl implements LqSystemSetConfigDubboService {
    private static Logger logger = LogManager.getLogger("service");
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    LqSystemSetConfigMapper lqSystemSetConfigMapper;

    @Override
    public SystemSetConfigDubbo getByKey(Long orgId) {
        log.info("----getByKey------"+orgId);
        if (orgId ==null){
            logger.error("数据参数错误"+orgId);
            return null;
        }
        Object object = RedisUtil.getValue(redisTemplate, CarownerhomeKeyName.systemSetConfigLq + orgId);
        if (object==null){
            SystemSetConfigDubbo systemSetConfigDubbo = lqSystemSetConfigMapper.selectByOrgId(orgId);
            if (systemSetConfigDubbo==null){
                logger.info("暂无数据");
                return null;
            }else {
                return systemSetConfigDubbo;
            }
        }else {
            SystemSetConfigRedis systemSetConfigRedis=(SystemSetConfigRedis)object;
            SystemSetConfigDubbo  systemSetConfigDubbo =new SystemSetConfigDubbo();
            BeanUtils.copyProperties(systemSetConfigRedis,systemSetConfigDubbo);
            return systemSetConfigDubbo;
        }
    }

    @Override
    public boolean setRedis(Long orgId) {
        if (orgId==null){
            logger.error("数据参数错误"+orgId);
            return false;
        }
        SystemSetConfigDubbo systemSetConfigDubbo = lqSystemSetConfigMapper.selectByOrgId(orgId);
            if (systemSetConfigDubbo==null){
                return false;
            }else {
                SystemSetConfigRedis systemSetConfigRedis=new SystemSetConfigRedis();
                BeanUtils.copyProperties(systemSetConfigDubbo,systemSetConfigRedis);
                boolean b = RedisUtil.setRedis(redisTemplate, CarownerhomeKeyName.systemSetConfigLq + orgId, systemSetConfigRedis);
            if (b){
                return true;
            }else {
                return false;
            }
        }
    }

    @Override
    public boolean deletedRedis(Long orgId) {
        if (orgId==null){
            logger.error("数据参数错误"+orgId);
            return false;
        }
        Object o = RedisUtil.deleteRedis(redisTemplate, CarownerhomeKeyName.systemSetConfigLq + orgId);
        if (o instanceof Boolean){
            return (Boolean)o;
        }else {
            return false;
        }
    }
}

3.springBoot工程调用

@RestController
@RequestMapping("/testLqSystemSetConfig")
public class LqSystemSetConfigController {
    @Reference
    LqSystemSetConfigDubboService lqSystemSetConfigDubboService;


    @GetMapping("/setRedis/{orgId}")
    public  boolean testSeRedis(@PathVariable Long orgId){
        boolean b = lqSystemSetConfigDubboService.setRedis(orgId);
        return b;
    }

    @GetMapping("/getRedis/{orgId}")
    public SystemSetConfigDubbo testGetRedis(@PathVariable Long orgId){
        SystemSetConfigDubbo systemSetConfigDubbo = lqSystemSetConfigDubboService.getByKey(orgId);
        return systemSetConfigDubbo;
    }

    @GetMapping("/deletedRedis")
    public boolean testDeletedRedis(@RequestParam(required = true) long orgId){
        boolean b = lqSystemSetConfigDubboService.deletedRedis(orgId);
        return b;
    }
}

4.struts2框架中,需要在xml文件中,配置dubbo接口

<?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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="carownerhomeCrm-customer" logger="slf4j" />

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!-- register:false,只订阅,不注册 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"  />

    <!-- dubbo接口 -->
  

    <dubbo:reference interface="com.zhiguan.carowner.springbootDubbo.service.system.LqSystemSetConfigDubboService"
                     id="LqSystemSetConfigDubboService"/>
</beans>

然后就可义调用了

public class LqSystemSetConfigAction extends BaseAction {
    private static final long serialVersionUID = 1L;
    private static Logger logger = (Logger) LogManager.getLogger("action");

    @Resource
    LqSystemSetConfigDubboService lqSystemSetConfigDubboService;
    @Resource
    SystemSetConfigService systemSetConfigService;


    public void testSetRedis(){
        logger.info("数据正常");
        String orgId = request.getParameter("orgId");
        if (orgId.isEmpty()){
            logger.error("参数错误");
            return;
        }
        boolean b = lqSystemSetConfigDubboService.setRedis(Long.valueOf(orgId));
        if (b){
            this.writeJson(b, true);
        }else {
            this.writeJson(b, false);
        }

    }
    public void testGetRedis(){
        String orgId = request.getParameter("orgId");
        if (orgId.isEmpty()){
            logger.error("参数错误");
            return;
        }
        SystemSetConfigDubbo systemSetConfigDubbo = lqSystemSetConfigDubboService.getByKey(Long.valueOf(orgId));
        if (systemSetConfigDubbo==null){
            SystemSetConfig setConfig = systemSetConfigService.selectOneByBak5(Long.parseLong(orgId));
            if (setConfig==null){
                this.writeJson("暂无数据", true);
            }else {
                this.writeJson(setConfig, true);
            }
        }
        this.writeJson(systemSetConfigDubbo, true);
    }

    public void testDeletedRedis(){
        String orgId = request.getParameter("orgId");
        if (orgId.isEmpty()){
            logger.error("参数错误");
            return;
        }
        boolean b = lqSystemSetConfigDubboService.deletedRedis(Long.valueOf(orgId));
        if (b){
            this.writeJson(b, true);
        }else {
            this.writeJson(b, false);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值