标签:xmla stat c项目 instance app 管理中心 main [] bin
参考http://www.cnblogs.com/ywlaker/p/6060005.html
参考http://www.cnblogs.com/ywlaker/p/6060146.html
创建三个项目
在dubbo-demo-client项目中创建接口DemoService.java
package org.dubbo.demo.client; public interface DemoService { public String test(String str); }
dubbo-demo-server项目修改pom.xml,添加以下依赖,要添加对dubbo-demo-client的依赖
<dependency> <groupId>com.xmyself</groupId> <artifactId>dubbo-demo-client</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency>
在dubbo-demo-server项目中创建类DemoServiceImpl.java,实现dubbo-demo-client声明的接口
package org.dubbo.demo.server; import org.dubbo.demo.client.DemoService; import org.springframework.stereotype.Service; @Service("demoService") public class DemoServiceImpl implements DemoService { public String test(String str) { return "hello world " + str; } }
创建目录
src/main/resources/META-INF/spring
在该目录下创建dubbo-server.xml,配置zookeeper地址和端口以及dubbo服务版本号
<?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:application name="dubbo-demo" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry protocol="zookeeper" address="172.17.210.124:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="org.dubbo.demo.client.DemoService" ref="demoService" version="0.0.1" /> <bean id="demoService" class="org.dubbo.demo.server.DemoServiceImpl" /> </beans>
创建Main.java
package org.dubbo.demo.server; public class Main { public static void main(String[] args) { /** * 启动容器,自动加载资源目录下的/META-INF/spring/*.xml初始化spring容器 */ com.alibaba.dubbo.container.Main.main(args); } }
启动该主类,即启动了dubbo服务。主类也可以用如下的方式来实现
public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/dubbo-server.xml"); context.start(); System.in.read();//阻塞 }
dubbo-demo-test项目首先配置好spring mvc,参考http://www.cnblogs.com/ywlaker/p/6046221.html,然后修改pom.xml添加依赖,要添加client的依赖
<dependency> <groupId>com.xmyself</groupId> <artifactId>dubbo-demo-client</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</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> <exclusions> <exclusion> <artifactId>org.apache.zookeeper</artifactId> <groupId>zookeeper</groupId> </exclusion> <exclusion> <artifactId>zookeeper</artifactId> <groupId>org.apache.zookeeper</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency>
在src/main/resources/spring目录下创建dubbo-client.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:application name="dubbo-demo" /> <!-- zookeeper注册中心 --> <dubbo:registry protocol="zookeeper" address="172.17.210.124:2181" /> <!-- 服务接口 --> <dubbo:reference id="demoService" interface="org.dubbo.demo.client.DemoService" version="0.0.1" /> </beans>
在spring初始化配置文件中加载dubbo-client.xml
<!-- 加载spring-dubbo-client.xml --> <import resource="classpath*:/spring/dubbo-client.xml" />
创建DemoController.java
package org.dubbo.demo.test.controller; import org.dubbo.demo.client.DemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class DemoController { @Autowired private DemoService demoService; @RequestMapping("/test") @ResponseBody public String test() { return demoService.test("dubbo-demo-test"); } }
启动项目,在浏览器地址栏输入
http://127.0.0.1:8080/dubbo-demo-test/test
标签:xmla stat c项目 instance app 管理中心 main [] bin
原文地址:http://www.cnblogs.com/ywlaker/p/6062281.html