记录使用,帮助理解
源代码点击这里
服务方
服务方的目录结构
- 依赖
spring的一些依赖,内置tomcat+dubbo+zookeeper的依赖
<!-- dubbo组件 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.10</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
</dependency>
<!-- zookeeper客户端 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF8</encoding>
</configuration>
</plugin>
<!-- 给maven项目 内置一个tomcat,之后 可以直接运行 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8802</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
- 配置
- web.xml
因为是war包所以需要再webapp下建立WEB-INF/web.xml
<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" id="WebApp_ID" version="2.5">
<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>
</web-app>
- applicationContext.xml
<!-- 配置dubbo的应用名称 -->
<dubbo:application name="students-server" />
<!-- 配置注册中心地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.2.129:2181" />
<!-- 配置dubbo扫描包 :将@Service所在包 放入 dubbo扫描中,供后续 dubbo在rpc时使用-->
<dubbo:annotation package="org.students.server.impl" />
<!-- 将@Service所在包 放入springIOC容器中,供后续 依赖注入时使用 -->
<context:component-scan base-package="org.students.server.impl"></context:component-scan>
- java文件
service接口和实现类(注解使用阿里提供的@service)
package org.students.server.impl;
import org.students.server.StudentServer;
import com.alibaba.dubbo.config.annotation.Service;
@Service//阿里提供的
public class StudentServerImpl implements StudentServer{
public String server(String name) {
return "server:" +name;
}
}
消费方
- 依赖:跟上面服务方一样
- 配置
- web.xml
<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" id="WebApp_ID" version="2.5">
<!-- 解决post乱码 -->
<filter>
<filter-name>CharacterEncodingfilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>foreEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<!-- 只拦截 .action结尾的请求 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
- springmvc.xml
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<mvc:annotation-driven>
<!-- 将Controller中的内容 直接打印到 浏览器中 -->
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置dubbo的应用名称 -->
<dubbo:application name="students-consumer"/>
<!-- 配置注册中心地址 <dubbo:consumer timeout="20000" check="false"/>-->
<dubbo:registry address="zookeeper://192.168.2.129:2181" />
<!-- 配置dubbo扫描包 -->
<dubbo:annotation package="org.controller"/>
<!-- 将控制器@Controller所在包 加入IOC容器 -->
<context:component-scan base-package="org.controller"></context:component-scan>
</beans>
- java类
service的接口 和controller
service的接口:跟服务方的service接口一样,要在controller中远程调用服务端中StudentServer 然后自动装配。
package org.students.server;
public interface StudentServer {
public String server(String name);//zs
}
package org.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.students.server.StudentServer;
import com.alibaba.dubbo.config.annotation.Reference;
@RestController
@RequestMapping("controller")
public class StudentController {
@Reference//调用远程服务中的StudentServer
StudentServer stuServer ;
@RequestMapping("rpcSerer")
public String rpcSerer() {
String result = stuServer.server("zs") ;
return result ;//将结果显示在控制台
}
}
测试
消费方tomcat端口号为8803
访问http://localhost:8803/controller/rpcSerer.action