Dubbo学习第一章-XML配置

整体分为三个模块,分别是API、Provider、Consumer

dubbo建议将服务接口、服务模型、服务异常等均放在 API 包中,因为服务模型和异常也是 API 的一部分,这样做也符合分包原则:重用发布等价原则(REP),共同重用原则(CRP)。

运行前提:java、zookeeper

一、下载zookeeper:http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/

1、将\zookeeper-3.4.14\conf下的zoo_sample.cfg修改为zoo.cfg

2、点击\zookeeper-3.4.14\bin\zkServer.cmd即可运行zookeeper

具体的zookeeper安装可参考其他博客或者:https://blog.csdn.net/weixin_37715446/article/details/78642052

目录结构

二、API 就只有两个接口

public interface Userservice {
    String getUserName(String name);
}
public interface UserServiceConsumer {
    String getAllUser(String name);
}

三、Provider:

pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>

    <groupId>com.zsw.dubbo</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.6.0</version>
        </dependency>
        <!-- 引用API包-->
        <dependency>
            <groupId>com.zsw.dubbo</groupId>
            <artifactId>dubbointerface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

新建实现类:

import orderservice.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserImpl implements UserService {
    public String getUserAddress(String name) {
        return name + "====获取到地址";
    }
}

新建provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--当前服务名,全局唯一-->
    <dubbo:application name="dubbo-service-provider"></dubbo:application>
    <!--指定注册中心的位置-->
    <dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry>
    <!--指定通信规则-->
    <dubbo:protocol name="dubbo" port="20080"></dubbo:protocol>
    <dubbo:service interface="orderservice.UserService" ref="UserImpl"></dubbo:service>
    <bean id="UserImpl" class="com.zsw.provider.impl.UserImpl"></bean>
</beans>

新建运行类:

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); // 按任意键退出
    }
}

四、Consumer:

pom:

<?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>

    <groupId>com.zsw.dubbo</groupId>
    <artifactId>orderservice-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.6.0</version>
        </dependency>
         <!-- 引用API包-->
        <dependency>
            <groupId>com.zsw.dubbo</groupId>
            <artifactId>dubbointerface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

新建实现类

import orderservice.OrderServie;
import orderservice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderImpl implements OrderServie {
    //在这里调用服务提供提供的服务
    @Autowired
    UserService userService;

    public String initOrder(String name) {
        return userService.getUserAddress(name);
    }
}

新建运行类:

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); // 按任意键退出
    }
}

新建consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!-- xmlns:context="http://www.springframework.org/schema/context"-->
    <!--当前服务名,全局唯一-->
    <dubbo:application name="dubbo-consumer"></dubbo:application>
    <!--指定注册中心的位置-->
    <dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry>
    <!--声明要调用的接口欧-->
    <dubbo:reference interface="orderservice.UserService" id="UserService"></dubbo:reference>
    <!--扫描该包,-->
    <context:component-scan base-package="com.zsw.dubbo.orderservice.impl"></context:component-scan>
</beans>

五、运行和debug

1、必须先运行provider,不然直接运行consumer会导致注入异常。

运行成功可在dubbo-admin查看

具体dubbo-admin的安装地址:https://github.com/apache/dubbo-admin

 

运行结果如下图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rorschach01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值