dubbo系列二、dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台)
一、zookeeper配置中心安装
1、下载安装包,zookeeper-3.4.6.tar.gz
2、解压安装包,修改配置文件
参考zookeeper-3.4.6/conf/zoo_sample.cfg文件,同步录下建立zoo.cfg,配置如下:
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=E:\项目\zookeeper-3.4.6\data # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1
3、启动zk
点击E:\项目\zookeeper-3.4.6\bin\zkServer.cmd
socket connection from /192.168.1.100:54836
二、dubboadmin监控中心的安装配置(非必须)
1、下载tomcat安装运行
2、下载dubbo-admin-2.5.8.war到tomcat7 \ webapps目录下
3、修改dubbo.properties
重启tomcat、在编译后的文件中找到\WEB-INF文件夹下的dubbo.properties文件,然后进行配置,默认属性配置如下:
dubbo.registry.address=zookeeper://192.168.1.100:2181 dubbo.admin.root.password=root dubbo.admin.guest.password=guest
4、验证dubbo-admin
重启zk、tomcat、访问:http://192.168.1.100:8080/dubbo-admin-2.5.8 ,进入监控中心的管理界面(默认管理员账户密码为:root,root)
三、dubbo代码示例
1、公共接口service
package com.dubbo.demo.api; public interface DemoRpcService { /** * 测试方法 * @return */ String getUserName(String uid); }
pom.xml
<?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>dubbo.demo</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </project>
2、生产者代码
package com.dubbo.demo; import com.dubbo.demo.api.DemoRpcService; public class DemoRpcServiceImpl implements DemoRpcService { public String getUserName(String uid) { System.out.println("接收入参:"+uid); return "小明"; } }
启动代码:
public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml"); context.start(); // 阻塞当前进程,否则程序会直接停止 System.in.read(); }
dubbo-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应用程序命名--> <dubbo:application name="dubbo-demo-provider"/> <!--dubbo注册地址--> <dubbo:registry address="zookeeper://192.168.1.100:2181"/> <!--dubbo协议地址--> <dubbo:protocol name="dubbo" port="20880"/> <!--接口声明--> <dubbo:service interface="com.dubbo.demo.api.DemoRpcService" ref="demoRpcService"/> <bean id="demoRpcService" class="com.dubbo.demo.DemoRpcServiceImpl"/> </beans>
pom.xml
<?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>dubbo.demo</groupId> <artifactId>dubbo-provider</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <!-- zookeeper客户端 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <!-- api接口 --> <dependency> <groupId>dubbo.demo</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
3、消费者代码
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
context.start();
String useId = "123456";
DemoRpcService demoService = (DemoRpcService) context.getBean("demoRpcService");
System.out.println("收到结果"+demoService.getUserName(useId));
// 阻塞当前进程,否则程序会直接停止
System.in.read();
}
dubbo-consumer.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应用程序命名--> <dubbo:application name="dubbo-demo-provider"/> <!--dubbo注册地址--> <dubbo:registry address="zookeeper://192.168.1.100:2181"/> <!--接口引用--> <dubbo:reference interface="com.dubbo.demo.api.DemoRpcService" id="demoRpcService"/> </beans>
pom.xml
<?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>dubbo.demo</groupId> <artifactId>dubbo-consumer</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <!-- zookeeper客户端 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <!-- api接口 --> <dependency> <groupId>dubbo.demo</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build> </project>
4、运行测试
先启动生产者、再启动消费者
Connected to the target VM, address: '127.0.0.1:52472', transport: 'socket' log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 接收入参:123456
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 收到结果小明
5、演示代码下载地址
GitHub:https://github.com/Star-Lordxing/dubbo-demo