Dubbo服务的发展和作用:
- 首先,看下一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越多之后的常规方案演进历程。
- 其次,当服务越来越多之后,我们需要做哪些服务治理?
- 最后,是dubbo的架构图
- Multicast注册中心
- Zookeeper注册中心
- Redis注册中心
- Simple注册中心
dubbo的简单环境搭建
1、安装zookeeper
zookeeper下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/,下载完成解压即可运行。注意,zookeeper目录下的conf文件夹中并没有zookeeper的配置文件,不过有一个zoo_sample.cfg文件,将其命令为zoo.cfg即可使用默认配置。
点击zkServer.cmd启动zookeeper:
2、安装dubbo-admin.war
安装dubbo-admin.war这一步是非必需的,不过使用dubbo-admin可以方便查看dubbo的运行状态和数据,便于管理。下载完成后放入到tomcat的webapps目录下,启动tomcat访问dubbo即可,登录之后就可以看到如下页面(登录用户名和密码在dubbo.properties文件中查看):
3、dubbo-server端配置
新建dubbo-server工程,下载所需的jar包,编写提供服务的接口和实现类,然后通过zookeeper注册中心暴露服务。
(3.1) 在maven文件中添加所需jar包的依赖:
<dependency> <groupId>com.alibabagroupId> <artifactId>dubboartifactId> <version>2.5.2version> dependency> <dependency> <groupId>org.apache.zookeepergroupId> <artifactId>zookeeperartifactId> <version>3.4.6version> dependency> <dependency> <groupId>com.github.sgroschupfgroupId> <artifactId>zkclientartifactId> <version>0.1version> dependency> <dependency> <groupId>commons-logginggroupId> <artifactId>commons-loggingartifactId> <version>1.2version> dependency> <dependency> <groupId>log4jgroupId> <artifactId>log4jartifactId> <version>1.2.17version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>4.1.4.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>4.1.4.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-beansartifactId> <version>4.1.4.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-expressionartifactId> <version>4.1.4.RELEASEversion> dependency>
(3.2) 工程spring文件(applicationContext.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:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <bean id="demoService" class="com.luoxn28.dubbo.impl.DemoServiceImpl"/> <dubbo:application name="demo-provider"/> <dubbo:registry address="zookeeper://192.168.1.100:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="com.luoxn28.dubbo.DemoService" ref="demoService"/> beans>
(3.3) 对外服务接口及其实现类:
/** * DemoService */ public interface DemoService { String sayHello(String name); } /** * DemoServiceImpl */ public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hi " + name + ", I am dubbo service"; } }
(3.4) 启动类:
/** * ServiceStart */ public class ServiceStart { public static void main(String[] args) { // 加载applicationContext.xml ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("start dubbo"); while (true) { Thread.yield(); } } }
整个dubbo-server工程结构如下:
4、dubbo-client端配置
新建dubbo-client工程,下载所需的jar包,编写获取服务的接口,然后通过zookeeper注册中心暴露的服务来获取。
(4.1) 在maven文件中添加所需jar包的依赖:
com.alibaba dubbo 2.5.2 org.apache.zookeeper zookeeper 3.4.6 com.github.sgroschupf zkclient 0.1 commons-logging commons-logging 1.2 log4j log4j 1.2.17 org.springframework spring-core 4.1.4.RELEASE org.springframework spring-context 4.1.4.RELEASE org.springframework spring-beans 4.1.4.RELEASE org.springframework spring-expression 4.1.4.RELEASE
(4.2) 工程spring文件(applicationContext.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:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="demo-client"/> <dubbo:registry address="zookeeper://192.168.1.100:2181" /> <dubbo:reference id="demoService" interface="com.luoxn28.dubbo.DemoService" check="false"/> beans>
(4.3) 待获取服务的接口:
public interface DemoService { String sayHello(String name); }
(4.4) 启动类
/** * ClientStart */ public class ClientStart { public static void main(String[] args) { System.out.println("hello dubbo"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); DemoService service = (DemoService) context.getBean("demoService"); String response = service.sayHello("luoxn28"); System.out.println(response); } }
整个dubbo-server工程结构如下:
运行ClientStart类之后,输出如下:
注意:
首先运行zookeeper,然后启动dubbo-server程序,最后运行dubbo-client程序。dubbo-client中的服务接口路径需要和dubbo-server的一致,也就是说一个类在dubbo-server中的url和在dubbo-client中的url要一样,否则运行会出现Forbid consumer异常。
参考:
1、windows下 zookeeper dubbo 安装+配置+demo 详细图文教程
2、从头开始搭建一个dubbo+zookeeper平台
3、dubbo-demo示例代码