DUBBO入门

DUBBO入门

官方文档:https://dubbo.gitbooks.io/dubbo-user-book/content/preface/

服务提供者#

项目结构:

pom文件:

Copy
<?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.xh.dubbo.demon</groupId> <artifactId>demon_provider</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>demon_api</module> <module>demon_pro</module> </modules> <name>demon_provider</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> </dependencies> </dependencyManagement> <build> ... </build> </project>

api#

接口:

Copy
package com.xh.dubbo.demon.service; /** * Created by root on 4/10/18. */ public interface SayHello { public String hello(String name); }

pom文件:
默认

实现#

实现类:

Copy
package com.xh.dubbo.demon.serviceImpl; import com.xh.dubbo.demon.service.SayHello; /** * Created by root on 4/10/18. */ public class SayHelloImpl implements SayHello { @Override public String hello(String name) { return "Hello " + name; } }

启动类:

Copy
package com.xh.dubbo.demon; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; /** * Hello world! */ public class App { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:provider.xml"}); context.start(); System.in.read(); // 按任意键退出 } }

配置文件:

Copy
<?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="hello-world-app"/> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="multicast://224.5.6.7:1234"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.xh.dubbo.demon.service.SayHello" ref="helloImpl"/> <!-- 和本地bean一样实现服务 --> <bean id="helloImpl" class="com.xh.dubbo.demon.serviceImpl.SayHelloImpl"/> </beans>

pom文件:

Copy
<?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"> <parent> <artifactId>demon_provider</artifactId> <groupId>com.xh.dubbo.demon</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>demon_pro</artifactId> <packaging>jar</packaging> <name>demon_pro</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.xh.dubbo.demon</groupId> <artifactId>demon_api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> </dependency> </dependencies> <build> ... </build> </project>

服务消费者#

项目结构:

调用服务:

Copy
package com.xh.dubbo.demon; import com.xh.dubbo.demon.service.SayHello; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Hello world! */ public class App { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:consumer.xml"}); context.start(); SayHello demoService = (SayHello) context.getBean("demoService"); // 获取远程服务代理 String hello = demoService.hello("nana "); // 执行远程方法 System.out.println(hello); // 显示调用结果 } }

配置文件:

Copy
<?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="consumer-of-helloworld-app"/> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry address="multicast://224.5.6.7:1234"/> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.xh.dubbo.demon.service.SayHello"/> </beans>

pom文件:

Copy
<?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.xh.dubbo.demon</groupId> <artifactId>demon_consumer</artifactId> <version>1.0-SNAPSHOT</version> <name>demon_consumer</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.xh.dubbo.demon</groupId> <artifactId>demon_api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> ... </build> </project>
posted @   懒企鹅  阅读(308)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
点击右上角即可分享
微信分享提示
CONTENTS