随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进,Dubbo应运而生。
架构
上图为dubbo结合注册中心的架构(也有单点模式),服务提供方(Provider)将所有的服务注册到注册中心(Registry),服务消费方(Consumer)通过访问注册中心取得可用的服务列表,然后调用服务。
后台管理部署
源码下载
https://github.com/alibaba/dubbo
控制台部署
Dubbo是一个多模块的maven项目,其中dubbo-admin是控制台。
- 修改dubbo-admin模块下的dubbo.properties的配置,如
dubbo.registry.address=zookeeper://xxx.xxx.xxx.xxx:2181;zookeeper://xxx.xxx.xxx.xxx:2182;zookeeper://xxx.xxx.xxx.xxx:2183
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
- 部署到web容器,启动即可
实现简单dubbo服务
分别建立三个模块
<modules>
<module>dubbo-api</module>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
</modules>
dubbo-api
- 定义接口
public interface IHelloWorldService {
public String sayHello(String name);
}
- pom配置
<artifactId>dubbo-api</artifactId>
dubbo-provider
- pom配置
<dependencies>
<dependency>
<groupId>com.bdong</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- http://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
- 实现接口
public class HelloWorldServiceImpl implements IHelloWorldService {
public String sayHello(String name) {
return "Hello World : "+ 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="hello-world-provider"/>
<!--注册中心-->
<dubbo:registry address="xxx.xxx.xxx.xxx:2181" protocol="zookeeper"/>
<!--<!–不使用注册中心–>-->
<!--<dubbo:registry address="N/A"/>-->
<!--协议-->
<dubbo:protocol name="dubbo" port="20881"/>
<!--发布服务-->
<dubbo:service interface="com.bdong.dubbo.api.IHelloWorldService" ref="demoService" protocol="dubbo"/>
<bean id="demoService" class="com.bdong.dubbo.provider.HelloWorldServiceImpl"/>
</beans>
- 启动服务
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
context.start();
//保持服务可用
System.in.read();
}
dubbo-consumer
- pom配置同dubbo-provider
- 配置文件
<!--服务消费者名称-->
<dubbo:application name="hello-world-provider"/>
<!--注册中心-->
<dubbo:registry address="xxx.xxx.xxx.xxx:2181" protocol="zookeeper"/>
<!--<!–不使用注册中心–>-->
<!--<dubbo:registry address="N/A"/>-->
<!--协议-->
<dubbo:protocol name="dubbo" port="20881"/>
<!--引用服务-->
<dubbo:reference interface="com.bdong.dubbo.api.IHelloWorldService" id="demoService" protocol="dubbo"/>
- 调用
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});
IHelloWorldService helloWorldService = (IHelloWorldService) context.getBean("demoService");
System.out.println(helloWorldService.sayHello("man!"));
}
- 结果
Hello World : man!