标签:
项目组采用分布式服务,线上有几十个应用,RPC调用完全依靠Dubbo。平时开发一直都是用其他人搭好的dubbo环境,最近自己抽空独立的搭建dubbo小demo,一个服务端,一个客户端。
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <v.dubbo.ext>1.3.0-SNAPSHOT</v.dubbo.ext> <v.spring>3.1.2.RELEASE</v.spring> <v.plugin.jar>2.3.1</v.plugin.jar> </properties> <dependencies> <dependency> <groupId>com.jd.dubbo.ext</groupId> <artifactId>dubbo-ext-spi</artifactId> <version>${v.dubbo.ext}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${v.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${v.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${v.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${v.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${v.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${v.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${v.spring}</version> </dependency> </dependencies>这些都是开发dubbo服务端必须要用的依赖包。
package org.dubbo.server.api; public interface DemoService { public String sayHello(String name); }
package org.dubbo.server.service; import org.dubbo.server.api.DemoService; import org.springframework.stereotype.Service; @Service("demoService") public class DemoServiceImpl implements DemoService{ public String sayHello(String name) { return "Hello " + name; } }同时服务端应该作为一个独立的应用部署起来,处理客户端的请求。
package org.dubbo.server.service; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboServer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}); context.start(); System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟 } }
<?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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName"> <context:component-scan base-package="org.dubbo.server.service.**" /> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="hehe_consumer" organization="risk.im.jd.com" owner="lvsheng" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry id="registry" address="10.28.163.15:6060" /> <dubbo:protocol id="protocol" name="dubbo" port="25000" heartbeat="0" threadpool="cached" threads="512" /> <dubbo:provider id="im.riskctrl.dubbo.provider" timeout="5000" retries="5" loadbalance="roundrobin" cluster="failover" registry="registry"> </dubbo:provider> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService --> <dubbo:service interface="org.dubbo.server.api.DemoService" ref="demoService" provider="im.riskctrl.dubbo.provider" > </dubbo:service> </beans>
package com.jd.lvsheng.dubbo.client.test; import org.dubbo.server.api.DemoService; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Service; @Service("dubboConsumer") public class DubboConsumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); context.start(); DemoService demoService = context.getBean("demoService", DemoService.class); System.out.println(demoService.sayHello("aaa")); System.in.read(); } }客户端的spring配置如下:
<?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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="dubbo_consumer" organization="risk.im.com" owner="lvsheng" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry id="registry" address="10.28.163.15:6060" /> <dubbo:protocol id="protocol" name="dubbo" port="25001" heartbeat="0" threadpool="cached" threads="512" /> <dubbo:provider id="im.riskctrl.dubbo.provider" timeout="5000" retries="5" loadbalance="roundrobin" cluster="failover" registry="registry"> </dubbo:provider> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService --> <dubbo:reference interface="org.dubbo.server.api.DemoService" id="demoService" registry="registry"> </dubbo:reference> </beans>整个工程是可以运行的。
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/bruce128/article/details/47024609