分佈式服務-DUBBOX(六):集成服務消費者

一、建立demo-consumer工程

整個工程跟demo-web相似,只是將controller中調用Service改成dubbo服務。java

1)pom.xmlweb

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>com.company</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-consumer</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>com.company</groupId>
            <artifactId>demo-base</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.company</groupId>
            <artifactId>demo-api</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

</project>

2)UserControllerredis

package com.company.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.company.api.dto.UserDTO;
import com.company.api.iapi.UserApi;
import com.company.base.exception.DemoException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("user")
public class UserController {
    @Reference
    private UserApi userApi;

    @RequestMapping("qryUserById")
    @ResponseBody
    public UserDTO qryUserById(Long id) throws DemoException{
        if(id == null){
            throw new DemoException("id爲空");
        }

        return userApi.qryById(id);
    }

}

3)dubbo.propertiesspring

##
# Copyright 1999-2011 Alibaba Group.
#  
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#  
#      http://www.apache.org/licenses/LICENSE-2.0
#  
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
dubbo.container=log4j,spring
dubbo.application.name=demo-consumer
dubbo.application.owner=
#dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
#dubbo.monitor.protocol=registry
dubbo.log4j.file=logs/dubbo-demo-consumer.log
dubbo.log4j.level=WARN

dubbo.reference.check=false
dubbo.reference.version=wsy

4)spring-consumer.xmlexpress

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-lazy-init="false">

    <!--<dubbo:reference interface="com.talkweb.service.UserService" id="userService"/>-->
    <dubbo:annotation package="com.company.consumer.controller"/>

</beans>

5)spring-mvc.xmlapache

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        ">

        <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8"/>
        </bean>

        <!--spring-mvc組件默認配置+啓動註解等等-->
        <mvc:annotation-driven>
            <mvc:message-converters>
                <!--StringHttpMessageConverter中文亂碼-->
                <ref bean="stringHttpMessageConverter"/>
            </mvc:message-converters>
        </mvc:annotation-driven>

        <!--spring ioc中掃描-講註解Controller掃入-->
        <!--<context:component-scan base-package="com.company.consumer.controller">-->
            <!--<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
        <!--</context:component-scan>-->

        <context:component-scan base-package="com.company"/>

        <!--引入dubbo消費者配置-->
        <import resource="classpath:spring-consumer.xml"/>

</beans>

6)web.xmlapi

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <!--字符過濾器配置-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <description>spring mvc servlet</description>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:spring-mvc.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>

        <!--Servlet3.0配置文件上傳參數-->
        <multipart-config>
            <!--<location>D:\\upload</location>-->
            <!--<max-file-size>1024</max-file-size>-->
            <!--<max-request-size>1024</max-request-size>-->
            <!--<file-size-threshold>1</file-size-threshold>-->
        </multipart-config>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


</web-app>

 

二、運行結果

將demo-consumer部署到tomcat上。spring-mvc

http://localhost:8086/demo-consumer/user/qryUserById.do?id=1,運行結果下圖。tomcat

相關文章
相關標籤/搜索