- linux系统搭建环境
安装tomcat和zookeeper注册中心,如果是在同一台机器,zookeeper端口号是2181,不用进行修改. - 服务提供者开发
创建业务接口
@Service要用阿里巴巴dubbo提供的注解,并在web.xml配置springlistener,
在spring配置文件中,配置如下
<dubbo:application name="dubboxdemo-service"/>
<dubbo:registry address="zookeeper://192.168.25.128:2181"/>
<dubbo:annotation package="cn.itcast.dubboxdemo.service" />
- 服务消费者开发
web.xml配置
<!-- 解决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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
</web-app>
- Controller类代码
@Controller
@RequestMapping("/user")
public class UserController {
@Reference
private UserService userService;
@RequestMapping("/showName")
@ResponseBody
public String showName(){
return userService.getName();
}
拷贝服务提供者业务服务包和接口
spring配置如下
<mvc:annotation-driven >
<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="dubboxdemo-web" />
<dubbo:registry address="zookeeper://192.168.25.132:2181"/>
<dubbo:annotation package="cn.itcast.dubboxdemo.controller" />
</beans>
浏览器输入http://localhost:8082/user/showName.do,查看测试结果
dubbox2.8.4需要jdk1.8支持