一、配置zk
1、安装zk 下载地址http://www.apache.org/dyn/closer.cgi/zookeeper/
下载ZK后解压,然后进行启动。此处的zk部署在192.168.0.190机器上,使用默认端口2181
2、启动zk
bin/zkServer.sh start
3、测试zk是否启动
bin/zkCli.sh -server 192.168.0.190:2181
出现
Welcome to ZooKeeper! 启动成功
二、新建服务端项目
1、新建项目 dubboProvide 配置服务端
pom.xml 配置如下
<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>dubboProvide</groupId><artifactId>dubbo-providerDemo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.4.10</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>0.4</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>3.2.8.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>3.2.8.RELEASE</version><scope>test</scope></dependency></dependencies></project>
spring-dubbo-provide.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:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"default-lazy-init="false" ><!-- 提供方应用信息,用于计算依赖关系 --><dubbo:application name="dubboProvide" /><!-- 使用zookeeper注册中心暴露服务地址 --><dubbo:registry address="zookeeper://192.168.0.190:2181" /><!-- 用dubbo协议在20880端口暴露服务 --><dubbo:protocol name="dubbo" port="20880" /><!-- 声明需要暴露的服务接口 --><dubbo:service interface="com.lin.provider.DemoService" ref="demoServiceImpl" /><!-- 和本地bean一样实现服务 --><bean id="demoServiceImpl" class="com.lin.provider.DemoServiceImpl" /></beans>
DemoServcie 如下:
package com.lin.provider;public interface DemoService {public String sayHello(String name);}
DemoServiceImpl 如下:
package com.lin.provider;public class DemoServiceImpl implements DemoService{@Overridepublic String sayHello(String name) {return "Hello Dubbo,Hello " + name;}}
服务端配置完毕,接下来启动项目,提供接口
Provider 如下:
package com.lin.provider;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-dubbo-provider.xml"});context.start();System.out.println("---开启端口-----");System.in.read(); // 按任意键退出}}
运行main方法,提供接口;此时服务端启动完毕。消费者就可以调用这个暴露的接口。
2、新建dubboCustom项目,实现消费者调用服务端。
pom.xml如上。
spring-dubbo-custoom.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:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"default-lazy-init="false" ><!-- 提供方应用信息,用于计算依赖关系 --><dubbo:application name="dubboCustom" /><!-- 使用zookeeper注册中心暴露服务地址 --><dubbo:registry address="zookeeper://192.168.0.190:2181" /><!-- 用dubbo协议在20880端口暴露服务 --><dubbo:protocol name="dubbo" port="20880" /><!-- 声明需要暴露的服务接口 --><dubbo:reference id="demoServiceImpl" interface="com.lin.provider.DemoService" /></beans>
消费者测试类,custom类如下:
package dubboCustom;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lin.provider.DemoService;public class Custom {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring-dubbo-custom.xml" });context.start();DemoService demoService = (DemoService) context.getBean("demoServiceImpl"); //String hello = demoService.sayHello("lin"); // ִSystem.out.println(hello); //System.in.read();}}
运行main方法,可以看到,控制台输出
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).log4j:WARN Please initialize the log4j system properly.Hello Dubbo,Hello tom
调用成功!。
256

被折叠的 条评论
为什么被折叠?



