zoukankan      html  css  js  c++  java
  • Dubbox服务的消费方开发

    开发步骤:

    (1)创建Maven工程(WAR)dubboxdemo-web ,在pom.xml引入依赖 ,同“dubboxdemo-service”工程。区别就是把tomcat插件的运行端口改为8082 。

    (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:applicationContext-web.xml</param-value>

        </init-param>

      </servlet> 

      <servlet-mapping>

        <servlet-name>springmvc</servlet-name>

        <url-pattern>*.do</url-pattern>

      </servlet-mapping>

    </web-app>

    (3)拷贝业务接口

    将“dubboxdemo-service”工程的cn.itcast.dubboxdemo.service 包以及下面的接口拷贝至此工程。

    (4)编写Controller

    package cn.itcast.dubboxdemo.controller;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.ResponseBody;

    import cn.itcast.dubbodemo.service.UserService;

    @Controller

    @RequestMapping("/user")

    public class UserController {

        @Reference

        private UserService userService; 

        @RequestMapping("/showName")

        @ResponseBody

        public String showName(){

            return userService.getName();

        }      

    }

    (5)编写spring配置文件

    在src/main/resources下创建applicationContext-web.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">

                <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>

    (6)测试运行

    tomcat7:run

    在浏览器输入http://localhost:8082/user/showName.do,查看浏览器输出结果

  • 相关阅读:
    Android学习第一课
    窗体 dialog 弹出时动画效果
    poj2105 IP Address(简单题)
    #定位系统性能瓶颈# strace &amp; ltrace
    POJ2142——The Balance
    排序算法系列之八大排序算法性能比較-从实验结果分析
    DuFile网赚网盘
    一次dns缓存引发的惨案
    IT大咖说
    HTTPS与SNI扩展,一个IP绑定多个SSL证书 | VeriSign|SSL证书|数字签名证书|服务器证书|微软代码签名证书|Symantec
  • 原文地址:https://www.cnblogs.com/cn-chy-com/p/11118749.html
Copyright © 2011-2022 走看看