1.在使用dubbo开发中我们有使用到注解的配置方式,相对来说比较简单,所以写下注解的配置方式方便更多的人。
2.使用的jar有dubbo-2.5.3,zkClient-0.3,zookeeper-3.4.5。
3.dubbo生产者的配置信息
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
=
"http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
"
>
name
=
"dubbo_provider"
/>
address
=
"zookeeper://127.0.0.1:2181"
/>
name
=
"dubbo"
port
=
"20880"
/>
package
=
"com.dubbo.provide"
/>
base-package
=
"com.dubbo."
/>
service类的配置
package com.dubbo.provide.user.service.impl;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.provide.user.service.IUserService;
@Component
@Service
public class UserService implements IUserService {
@Override
public String getUser() {
return "调用成功,good";
}
}
@
Component是spring bean的注解,@Service是dubbo的注解,这两个注解的意思就是一个在配置文件里面声明了bean,一个暴露的服务接口。
配置消费者(客户端)
我这边使用的是springmvc框架,注意:这边接口的名称以及包路径,必须和提供者暴露的接口一致,可以直接将服务端的接口导出成jar,然后在客户端引入使用。
平时加配置文件的习惯都是按不同的功能来配置的,但是刚试了一下将dubbo单独配置,发现在注解扫描的时候扫描不到,有可能是跟spring的注解扫描发生了冲突,最后我将dubbo的配置集成到了springMvc-servlet.xml配置中问题就解决了
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
p:ignoreDefaultModelOnRedirect="true" >
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConvert
er"/>
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
dubbo的配置我是加在了springMvc配置的最前面。
controller的配置
package com.frame.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.provide.user.service.IUserService;
@Controller
@RequestMapping(value="/user/*")
public class UserController {
@Reference
IUserService userService;//调用Dubbo暴露的接口
@RequestMapping(value="getUser.htm")
public @ResponseBody String getUser(){
return userService.getUser();
}
}
使用@Reference来注入服务端暴露的接口。