Dubbo简单示例

步骤

1. 开启zookeeper注册中心
2. 编写接口
3. 编写服务提供者
4. 编写服务消费者
5. 启动服务提供者
6. 启动服务消费者

接口

  • 使用maven创建项目,打包方式为jar
<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.dobbo.provider</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
</project>
  • 编写简单接口
public interface GBDTService {
    public String ckeckInfo();
}

服务提供者

  • 使用maven创建项目,打包方式为jar,pom中引入接口
<dependency>
    <groupId>com.dobbo.provider</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
  • 编写接口实现类
public class GBDTServiceImpl implements GBDTService {
    public String ckeckInfo() {
        return "返回结果:0";
    }
}
  • 编写配置文件applicationContext.xml
<?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://code.alibabatech.com/schema/dubbo"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://code.alibabatech.com/schema/dubbo  
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
        "> 
    <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
    <dubbo:application name="gbdt-provider" owner="programmer" organization="dubbox"/>
    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
    <dubbo:registry protocol="zookeeper" address="192.168.31.125:2181,192.168.31.125:2182,192.168.31.125:2183"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--使用 dubbo 协议实现定义好的 api 接口-->
    <dubbo:service interface="com.dubbo.provider.service.GBDTService" ref="gbdtService" protocol="dubbo" />
    <!--具体实现该接口的 bean-->
    <bean id="gbdtService" class="com.dubbo.provider.impl.GBDTServiceImpl"/>
</beans>
  • 编写启动类
public class Provider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println(context.getDisplayName() + ": here");
        context.start();
        System.out.println("Provider");
        System.in.read();
    }
}

服务消费者

  • 使用maven创建项目,打包方式为jar,pom中引入接口
<dependency>
    <groupId>com.dobbo.provider</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

编写服务消费者配置文件applicationContext.xml

<?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://code.alibabatech.com/schema/dubbo"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://code.alibabatech.com/schema/dubbo  
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
        "> 
    <dubbo:application name="gbdt-consumer" owner="programmer" organization="dubbox"/>
    <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
    <dubbo:registry protocol="zookeeper" address="192.168.31.125:2181,192.168.31.125:2182,192.168.31.125:2183"/>
    <!--使用 dubbo 协议调用定义好的 api接口-->
    <dubbo:reference id="gbdtService" interface="com.dubbo.provider.service.GBDTService"/>
</beans>

验证结果

11.png

12.png

dubbo-admin后台查看服务

13.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容