Dubbo入门案例
程序员文章站
2022-06-02 15:22:00
...
Dubbo入门
简介:
架构:
以上内容摘自dubbo-user
入门案例:
安装ZooKeeper,这里将用ZooKeeper作为注册中心,统一管理服务。(安装步骤略)
安装dubbo-admin监控中心(非必须,注:可自行网上查找可用于JDK8的war包,要不然会因为环境问题无法再Tomcat中启动)
创建服务提供者工程Dubbo-P
添加依赖jar包(pom.xml)
<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>cn.guyouda</groupId>
<artifactId>Dubbo-P</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
创建服务接口:DemoService.java
package cn.guyouda.dubbo.example;
public interface DemoService {
String sayHello(String name);
}
服务提供者实现DemoService接口:DemoServiceImpl.java
package cn.guyouda.dubbo.example;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello," + name;
}
}
创建配置文件provider.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="hello-world-app" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20881" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="cn.guyouda.dubbo.example.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="cn.guyouda.dubbo.example.DemoServiceImpl" />
</beans>
启动服务:Provider.java
package cn.guyouda.dubbo.example;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new
String[] {"classpath:provider.xml"});
context.start();
System.out.println("Provider started");
System.in.read(); // 按任意键退出
}
}
创建消费者工程Dubbo-C(结构如下)
引入依赖pom.xml
<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>cn.guyouda</groupId>
<artifactId>SB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
</project>
创建服务接口DemoService.java(需和服务提供者接口保持一致)
package cn.guyouda.dubbo.example;
public interface DemoService {
String sayHello(String name);
}
<?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" /> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="cn.guyouda.dubbo.example.DemoService"/>
</beans>
启动Consumer.java
package cn.guyouda.dubbo.example;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new
String[] {"classpath:consumer.xml"});
context.start();
DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("world"); // 执行远程方法
System.out.println( hello ); // 显示调用结果
}
}
注意:
1、建议打开两个Eclipse分别运行两个工程,要不然运行Dubbo-C的时候Dubbo-P会停了,导致出错
2、新手运行Dubbo-P时极易遇见无法绑定,Dubbo端口20880占用问题:第一次启动就会绑定20880,再次启动就会出现无法绑定,可以关闭相应进程;建议重新启动的时候在provider.xml换一个端口20881,就不会出现之前的问题了。
3、一定要先启动Dubbo-P,确定服务提供者已经正常启动以后再启动Dubbo-C
下一篇: Spring的注解开发