web.xml
监听spring配置文件
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!--作为服务的配置-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo.xml</param-value>
</context-param>
<!--作为controler入口的配置-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
dubbo.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"
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">
<context:property-placeholder location="classpath:dubbo.properties" />
<dubbo:application name="provider"/><!--服务名称-->
<dubbo:registry address="zookeeper://192.168.58.167:2181"/><!--配置注册中心地址-->
<dubbo:protocol name="dubbo" port="${port}"/><!--注册中心提供者,不提供服务只消费不用配,集群也要端口不同 name: 请求协议 port:提供者的请求端口,不能重复 -->
<dubbo:annotation package="包"/><!--包扫描,@Service作为提供者,@Controller作为消费者-->
</beans>
Service
一定要有接口,可以单独作为一个模块
public interface Service {
Object funtion(Object object);
}
实现类要加上@Service注解
@Service(timeout = )//debug时设置
public class ServiceImpl implements Service {
@Override
public Object funtion(Object object) {
return null;
}
传递的实体类要实现Serializable接口
Controller
模块要引用服务接口模块
@Controller
public class Controller {
//用Reference引用服务接口,测试时可以check=false不测试服务
@Reference(check = true, loadbalance = "random/roundrobin/leastactive")
private Service service;