dubbo的入门
服务提供者(porvider)
一.导入相关依赖
二.创建applicationContext-service.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="dubboxdemo-service"/>
//zookeeper地址
<dubbo:registry address="zookeeper://192.168.0.100:2181"/>
//配置协议和端口
<dubbo:protocol name="duboo" port="20880"/>
//匹配相关服务
<dubbo:annotation package="com.jiu.provider.impl" />
</beans>
三.web.xml
<!-- 加载spring容器的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
四.提供server服务方法
import com.alibaba.dubbo.config.annotation.Service;
@Service (此注解书dubbo相关包下的)
public class UserProviderImpl implements UserProvider {
@Override
public String providerTest() {
return "ni hao a con" ;
}
}
服务消费者(concumer)
一.导入相关依赖
二.创建applicationContext-service.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="dubboxdemo-concumer"/>
//zookeeper地址
<dubbo:registry address="zookeeper://192.168.0.100:2181"/>
//扫描contorller
<dubbo:annotation package="com.jiu.controller" />
//不需要先启动提供者
<dubbo:consumer check="false"/>
</beans>
三.web。xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-web.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*.do</url-pattern>
</servlet-mapping>
四。编写controller接口
@Controller
@RequestMapping("/user")
publicclass UserController {
@Reference
private UserProvider useProvider;
@RequestMapping("/showName")
@ResponseBody
public String showName(){
useProvider.providerTest(); } }

浙公网安备 33010602011771号