整体分为三个模块,分别是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
运行结果如下图