随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进,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
分别建立三个模块
<module>dubbo-api</module>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
</modules>
public interface IHelloWorldService {
public String sayHello(String name);
}
<artifactId>dubbo-apiartifactId>
<dependencies>
<dependency>
<groupId>com.bdonggroupId>
<artifactId>dubbo-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.5.3version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.9version>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
dependencies>
public class HelloWorldServiceImpl implements IHelloWorldService {
public String sayHello(String name) {
return "Hello World : "+ name;
}
}
<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: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:application name="hello-world-provider"/>
<dubbo:registry address="xxx.xxx.xxx.xxx:2181" protocol="zookeeper"/>
<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!