学习笔记(九)、Spring Boot整合 Dubbo操作使用

上节给出在传统Spring 项目中使用Dubbo + Zookeeper 搭建简单RPC程序,这次记录下Spring Boot中整合Dubbo使用

一、创建服务提供者boot项目,boot-rpc-provide:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  
    <modelVersion>4.0.0</modelVersion>

    <artifactId>boot-rpc-provide</artifactId>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <!--引入公共依赖-->
        <dependency>
            <groupId>rpcdemo</groupId>
            <artifactId>rpc-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

    </dependencies>
</project>

服务提供层实现:

package com.rpc.boot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.rpc.bean.User;
import com.rpc.service.UserService;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author WDG
 * @date 2019-2-14
 * 服务提供实现
 */
@Service //dubbo 注解,在服务注册实现类上标记
@Component // 该实例被spring 容器管理
public class UserServiceImpl implements UserService {

    public User getInfo(String userId) {

        System.out.println("获取到userId:"+userId);
        return  new User(userId,"上海",new Date(),"张三");
    }
}

 application.properties文件:

#服务提供者配置
dubbo.application.name=boot-rpc-provide
#使用zookeeper作为注册中心
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181

#分布式固定是dubbo,不需要改
dubbo.protocol.name=dubbo
#使用dubbo协议将服务暴露在20881端口
dubbo.protocol.port=20881

启动类:

package com.rpc.boot;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


/**
 * @author WDG
 * @date 2019-2-14
 */

@EnableDubbo 
@SpringBootApplication
public class RunApplication {
    public static void main(String[] args) {
        SpringApplication.run(RunApplication.class,args);
    }
}

注:主要加上 @EnableDubbo 注解,开启dubbo服务

二、创建服务消费者boot项目,boot-rpc-consumer:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
    <modelVersion>4.0.0</modelVersion>
    <artifactId>boot-rpc-consumer</artifactId>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <!--引入公共依赖-->
        <dependency>
            <groupId>rpcdemo</groupId>
            <artifactId>rpc-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>
</project>

application.properties配置文件:

server.port=8081
dubbo.registry.protocol=zookeeper
dubbo.application.name=boot-rpc-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo

服务消费service层:

package com.boot.rpc.consumer.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.rpc.bean.User;
import com.rpc.service.ConsumerService;
import com.rpc.service.UserService;
import org.springframework.stereotype.Service;

/**
 * @author WDG
 * @date 2019-2-14
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Reference //远程调用UserService 服务 使用dubbo的@Reference注解
    private UserService userService;
    public User getUser() {
        return userService.getInfo("qwer");
    }
}

controller:

package com.boot.rpc.consumer.controller;

import com.rpc.bean.User;
import com.rpc.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author WDG
 * @date 2019-2-14
 */
@RestController
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;

    @GetMapping("/getUser/{userId}")
    public User getUser(@PathVariable  String userId){
        return consumerService.getUser();
    }
}

 运行项目,启动在8081端口,dubbo-admin控制台截图:

 

中文官方文档:http://dubbo.apache.org/zh-cn/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值