dubbo+zk做远程调用的示例

前言

最近跟进一个传智健康的项目,他的技术栈使用的是ssm,注册中心是zookeeper,远程调用使用的是dubbo。技术栈比较老套,但还是想去了解一下!

核心代码

控制层

注意@Reference注解的包,是dubbo的!这里他通过maven,把interface放到pom里了!

package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 检查项管理
 *
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/12/16 22:19
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController
{
    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem)
    {
        try
        {
            checkItemService.add(checkItem);
        } catch (Exception e)
        {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }
}

服务层

package com.itheima.service;

import com.itheima.pojo.CheckItem;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/12/16 22:24
 */
public interface CheckItemService
{
    void add(CheckItem checkItem);
}

提供者

这里的提供者是接口的实现方,我这里只阐述核心代码,具体的会放到码云里,代码也很简单!

package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.CheckItemDao;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 加了事务注解之后,@Service注解就需要明确实现的是哪个接口
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/12/16 22:39
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService
{
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem)
    {
        checkItemDao.add(checkItem);
    }
}

这里实现类里面的@Service注解不再是传统的那个,注意包是dubbo下面的。第二个点是如果开始事务的话,那么要明确自己实现的是哪个接口!

链接:https://gitee.com/thirtyleo/itcast_health

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值