1. 程式人生 > >springboot--application.yml整合dubbo

springboot--application.yml整合dubbo

Service層: 1.pom裡面引jar包

		<!--Dubbo start -->
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>${zkclient.version}</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba.spring.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>
		<!-- Dubbo end -->

2.application.yml配置檔案 這是最簡單的配置了,更多詳細配置可以隨著需要新增

  dubbo:
    server: true
    registry:
      address: zookeeper://192.168.25.133:2181
    protocol:
      name: dubbo
      port: 20899
    scan: com.lbonline
    provider:
      filter: catTransaction

web層 1.pom檔案

        <!-- Spring Boot Dubbo start -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!-- Spring Boot Dubbo end-->

2.application.yml配置檔案

dubbo:
    registry:
       address: zookeeper://192.168.25.133:2181
    protocol:
      name: dubbo
      port: 20899
    scan: com.lbonline
    consumer:
      filter: catTransaction
      check: false

配置好了接下來一個呼叫的小例子,用的是scan掃描接下來註解得需要注意了

import com.alibaba.dubbo.config.annotation.Reference;
/**
 * @Auther: lijianmin
 * @Date: 2018/8/11 15:02
 * @Description:
 */
@Api(value = "評論資訊拉取", tags = "評論資訊拉取")
@RestController
@RequestMapping("/api/comment/")
@Slf4j
public class CommentController {

    @Reference(version ="1.0.0")
    CommentServer commentServer;
    @ApiOperation(value = "查詢課程評論")
    @GetMapping("/schedule")
    public ResultVo<Comment> findComment(@RequestParam("scheduleId") Integer scheduleId, @RequestParam("beginTime") String beginTime){
        Comment comment = commentServer.findCommentBysIdBtime(scheduleId, beginTime);
        return ResultVoUtil.success(comment);
    }
}

需要注意的是@reference的包import com.alibaba.dubbo.config.annotation.Reference;

import com.alibaba.dubbo.config.annotation.Service;
/**
 * @Auther: lijianmin
 * @Date: 2018/8/11 15:02
 * @Description:
 */
@Service(version = "1.0.0", interfaceClass = CommentServer.class, timeout = 3000)
@Component
@Slf4j
public class CommentServerImpl implements CommentServer {
    @Autowired
    CommentMapper commentMapper;
    @Override
    public Comment findComment(int classId) {
        return commentMapper.findComment(classId);
    }

    /**
     * 根據課程id和課程開始時間查詢評價
     * @param scheduleId
     * @param beginTiome
     * @return
     */
    @Override
    public Comment findCommentBysIdBtime(int scheduleId, String beginTiome) {
        return commentMapper.findCommentBysIdBtime(scheduleId,beginTiome);
    }
}

需要注意的是這個@service的註解的引用包import com.alibaba.dubbo.config.annotation.Service;

最簡單的一個整合就到了,詳細的可以檢視官網