消费者开发步骤:
(1)创建Maven工程dubbox-customer,在pom.xml引入依赖 ,同“dubboxservice”工程。
(2)在webapps目录下WEB-INF 目录,并修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 解决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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
(3)拷贝业务接口
将“dubboxservice”工程的org.hlx.service 包下面的接口拷贝至此工程。
(4)编写Controller
package com.hlx.demo.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import org.hlx.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @Author HLX
* @Date 2020/1/4 15:11
*/
@Controller
@RequestMapping("/user")
public class UserController {
//远程调用接口(阿里巴巴)引用哦
@Reference
private UserService userService;
@RequestMapping("/show") //映射路径
@ResponseBody //返回值直接输出
public String showName(){ //如果没有 @ResponseBody 返回是跳转的页面了哦!
return userService.getUserName();
}
}
(5)编写spring配置文件
在src/java/resources下创建springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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">
<mvc:annotation-driven >
<mvc:message-converters register-defaults="false">
<!--springmvc转换器返回值为字符串-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--消费方的名称-->
<dubbo:application name="dubbox-web"/>
<!--zookeeper服务IP及端口号-->
<dubbo:registry address="zookeeper://192.168.25.128:2181"/>
<!--扫描控制类-->
<dubbo:annotation package="com.hlx.demo.controller"/>
</beans>
(6)测试运行
先启动zookeeper服务哦!

再启动服务端 http://localhost:8081
最后启动消费端 http://localhost:8082/user/show.do

来源:CSDN
作者:凌冰_
链接:https://blog.csdn.net/hlx20080808/article/details/103835441
