springboot+dubbo(消费者)

启动zookeeper。

创建一个springboot的项目。

这边引入dubbo相关的jar包。

<dependency>
   <groupId>io.dubbo.springboot</groupId>
   <artifactId>spring-boot-starter-dubbo</artifactId>
   <version>1.0.0</version>
</dependency>

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>dubbo</artifactId>
   <version>2.5.6</version>
</dependency>

这边同样将springboot的父引用改为1.5.0版本:

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <!--<version>2.0.0.RELEASE</version>-->
   <version>1.5.0.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

配置springboot中的application.propertis:

spring.dubbo.application.name=consumer-1
#zk地址
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo协议
spring.dubbo.protocol.name=dubbo
#duboo端口号
#spring.dubbo.protocol.port=20880

#这是要扫描使用的dubbo接口所在包位置
spring.dubbo.scan=com.example.demo.service 

创建需要调用的DubboTest的接口:

package com.example.demo.service;

/**
 * 时间: 2018/3/22.
 *
 * @author zwf
 */
public interface DubboTest {
    public String test();
}

  同时创建调用dubbo服务的接口以及实体类:

package com.example.demo.service;

/**
 * 时间: 2018/3/22.
 *
 * @author zwf
 */

public interface TestService {
    public String test();
}
实体类:
package com.example.demo.service.impl;


import com.alibaba.dubbo.config.annotation.Reference;
import com.example.demo.service.DubboTest;

import com.example.demo.service.TestService;


import org.springframework.stereotype.Service;

/**
 * 时间: 2018/3/22.
 *
 * @author zwf
 */
@Service
public class TestServiceImpl implements TestService {

    @Reference
    DubboTest dubboTest;
//
    public String test(){

        return  dubboTest.test();
    }
}
 
 

这边这么创建的原因是Springboot在处理reference这个注解的时候是通过懒加载的形式,所以DubbetTest这个接口要在引入的它所在包的下一层。深入不做探究了。

创建调用测试的TestService接口的controller:

package com.example.demo.controller;

import com.example.demo.service.TestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 时间: 2018/3/22.
 *
 * @author zwf
 */
@RestController
public class TestController {

    @Resource
    TestService test;

    @RequestMapping("/test.json")
    public String test1(){
        return  test.test();
    }
}
 
 

然后启动springboot之后,dubbo消费者的调用就产生了。可以在zookeeper上看见相应的信息。

通过controller,就能看见相应的操作了。

搭建完成消费者和生产者,就考虑了调用的事务怎么解决。

猜你喜欢

转载自blog.csdn.net/qq_36633149/article/details/79666100