标签:demo alt 安装 http conf 配置 lease 分享图片 guid
参考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
点击E:\项目\zookeeper-3.4.6\bin\zkServer.cmd
socket connection from /192.168.1.100:54836
重启tomcat、在编译后的文件中找到\WEB-INF文件夹下的dubbo.properties文件,然后进行配置,默认属性配置如下:
dubbo.registry.address=zookeeper://192.168.1.100:2181 dubbo.admin.root.password=root dubbo.admin.guest.password=guest
重启zk、tomcat、访问:http://192.168.1.100:8080/dubbo-admin-2.5.8 ,进入监控中心的管理界面(默认管理员账户密码为:root,root)
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>
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>
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>
先启动生产者、再启动消费者
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. 收到结果小明
dubbo系列二、dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台)
标签:demo alt 安装 http conf 配置 lease 分享图片 guid
原文地址:https://www.cnblogs.com/wangzhuxing/p/9723236.html