动力节点首页 全国咨询热线:400-8080-105

绑定手机号,登录
手机号

验证码

微信登录
手机号登录
手机号

验证码

微信登录与注册
微信扫码登录与注册

扫码关注微信公众号完成登录与注册
手机号登录
首页 > 文章

Dubbo的使用方法

07-12 11:21 661浏览
举报 T字号
  • 大字
  • 中字
  • 小字

Dubbo搭建

导入Maven依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
</dependency>
<!--连接注册中心 zkclient-->
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.8</version>
</dependency>
<!--连接注册中心 curator-->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>2.7.1</version>
</dependency>

基于配置文件版本

服务提供者

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://dubbo.apache.org/schema/dubbo
        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--配置SpringBean-->
    <bean id="qqUserService" class="com.baizhi.qq.service.impl.QQUserService"/>
    <bean id="wxUserService" class="com.baizhi.wx.service.impl.WXUserService"/>
    <!-- 提供方应用信息,用于计算依赖关系-->
    <dubbo:application name="UserCenterProvider"/>
    <!--dubbo 注册中心 client可选值 zkclient、curator -->
    <dubbo:registry address="zookeeper://192.168.128.128:2181" client="curator" />
    <!--配置dubbo服务端实现-->
    <dubbo:protocol name="dubbo" port="20880" server="netty"/>
    <!--暴露服务接口-->
    <dubbo:service interface="com.baizhi.service.IUserService" ref="qqUserService" group="g1"/>
    <dubbo:service interface="com.baizhi.service.IUserService" ref="wxUserService" group="g2" />
</beans>

服务消费者

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://dubbo.apache.org/schema/dubbo
        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="DubboQQConsumer"/>
    <!--dubbo 注册中心 client可选值 zkclient、curator -->
    <dubbo:registry address="zookeeper://192.168.128.128:2181" client="curator" />
    <!--引用服务 基于接口创建代理bean -->
    <dubbo:reference id="qqUserService" proxy="jdk" group="g2" interface="com.baizhi.service.IUserService" />
</beans>

注解版本配置

@Configuration
@DubboComponentScan(basePackages = {
        "com.baizhi.qq.service.impl",
        "com.baizhi.wx.service.impl"}
        )
public class DubboConfiguration {
    //等价 <dubbo:application name="provider-test"/>
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("provider-test");
        return applicationConfig;
    }
    //等价 <dubbo:registry address="zookeeper://192.168.128.128:2181" client="curator" />
    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://192.168.128.128:2181");
        registryConfig.setClient("curator");
        return registryConfig;
    }
    @Bean
    public  ProtocolConfig protocolConfig(){
        ProtocolConfig protocolConfig=new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(20880);
        protocolConfig.setServer("netty");
        return  protocolConfig;
    }
}

服务暴露

@Service(
        group = "g1"
)
public class QQUserService implements IUserService {
    public User queryUserById(Integer id) {
        return new User(id,"QQ用户1",new Date());
    }
    public List<User> queryAll() {
        return null;
    }
    public void deleteUserById(Integer id) {
    }
    public void UpdateUser(User user) {
    }
}
---    
import com.alibaba.dubbo.config.annotation.Service;
import com.baizhi.entity.User;
import com.baizhi.service.IUserService;
import java.util.Date;
import java.util.List;
@Service(group = "g2")
public class WXUserService implements IUserService {
    public User queryUserById(Integer id) {
        return new User(id,"WX用户1",new Date());
    }
    public List<User> queryAll() {
        return null;
    }
    public void deleteUserById(Integer id) {
    }
    public void UpdateUser(User user) {
    }
}

消费端

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.baizhi.service.IUserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@DubboComponentScan(basePackages = "com.baizhi.service")
public class DubboConfiguration {
    @Reference(group = "g2" ,proxy = "jdk")
    private IUserService userService;
    @Bean
    public IUserService qqUserService(){
      return userService;
    }
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("provider-test");
        return applicationConfig;
    }
    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://192.168.128.128:2181");
        registryConfig.setClient("curator");
        return registryConfig;
    }
}

动力节点在线课程涵盖零基础入门,高级进阶,在职提升三大主力内容,覆盖Java从入门到就业提升的全体系学习内容。全部Java视频教程免费观看,相关学习资料免费下载!对于火爆技术,每周一定时更新!如果想了解更多相关技术,可以到动力节点在线免费观看Dubbo框架视频教程学习哦!

0人推荐
共同学习,写下你的评论
0条评论
代码小兵652
程序员代码小兵652

113篇文章贡献392215字

相关课程 更多>

作者相关文章更多>

推荐相关文章更多>

Java面试题及答案整理

提枪策马乘胜追击04-21 20:01

Spring常见面试题

代码小兵92504-17 16:07

Java零基础实战项目——五子棋

代码小兵98804-25 13:57

Java string类详解

杨晶珍05-11 14:54

6道经典算法面试题

杨晶珍05-12 16:39

发评论

举报

0/150

取消