消費者サービスの開発Dubbox

開発段階:

(1 )Mavenの作成プロジェクト(WAR )dubboxdemo-ウェブ "dubboxdemoサービス"プロジェクトとのpom.xml依存関係で導入し、。違いは、Tomcatプラグインのポート8082に実行することです。

(2 )WebアプリケーションにWEB-INFの下にディレクトリを作成するディレクトリ、とweb.xmlを作成します

<?xmlのバージョン= "1.0"エンコード= "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"

    バージョン= "2.5" >  

   <! - 文字化けはポストを解決します - >

    <フィルタ>

        <フィルタ名> CharacterEncodingFilter </フィルタ名> <フィルタクラス> org.springframework.web.filter.CharacterEncodingFilter </フィルタクラス>

        <init-param>の

            <param-name>のエンコーディング</ PARAM名>

            <PARAM値> UTF -8 </ PARAM値>

        </ initの-param>の

        <init-param>の 

            <param-name>のforceEncoding </ PARAM名> 

            <param-value>の真</ PARAM値> 

        </ initの-param>の 

    </フィルタ>

    <のfilter-mapping>

        <フィルタ名> CharacterEncodingFilter </フィルタ名>

        <url-pattern> / * </のurl-pattern>

    </フィルタマッピング>       

  <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,查看浏览器输出结果

おすすめ

転載: www.cnblogs.com/cn-chy-com/p/11118749.html