码迷,mamicode.com
首页 > 其他好文 > 详细

dubbo的简单使用

时间:2016-11-28 08:46:10      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:ace   code   frame   void   name   string   service   注册   birt   

技术分享

整个过程大致是这样的

1.注册中心使用zookeeper,地址为192.168.192.128:2181!

技术分享

2.首先服务方

所在的服务器是127.0.0.1:8081

服务方提供的接口:

public interface ITestTbService {

    void insertTestTb(TestTb testTb);
}

接口的实现类

@Service("testTbService")
@Transactional
public class TestTbServiceImpl implements ITestTbService {

    @Resource
    private TestTbMapper testTbMapper;
    
    @Override
    public void insertTestTb(TestTb testTb) {
        testTbMapper.insertSelective(testTb);
        //throw new RuntimeException();
    }
}

dubbo-provider.xml

<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-4.0.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="service-provider"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="192.168.198.128:2181" protocol="zookeeper"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol port="20880" name="dubbo"/>
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.winner.service.ITestTbService" ref="testTbService"/>

</beans>

3.服务消费方

所在的服务器是127.0.0.1:8080

<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-4.0.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用信息,用于计算依赖关系 -->
    <dubbo:application name="service-consumer"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="192.168.198.128:2181" protocol="zookeeper"/>
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:reference interface="com.winner.service.ITestTbService" id="testTbService"/>
    
</beans>

在消费服务器端:

@Controller
public class CenterController {

    @Resource
    private ITestTbService testTbService;

    @RequestMapping("/test/index.do")
    public String index() {
        TestTb testTb = new TestTb();
        testTb.setName("zhangsan");
        testTb.setBirthday(new Date());
        testTbService.insertTestTb(testTb);
        return "index";
    }
}

可以看到,我们直接用@Resource注入进来这个接口的实现,注意,这个接口的实现是在另外一台服务器上哟(此处只是用不同的端口模拟)!

注意事项:

Dubbo消费方及提供方传递的参数必须实现序列化接口!

public class TestTb implements Serializable

 

dubbo的简单使用

标签:ace   code   frame   void   name   string   service   注册   birt   

原文地址:http://www.cnblogs.com/winner-0715/p/6107987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!