【微服务架构】-Dubbo生态-Dubbo之Spring XML配置

...

  • 背景
    • 单一应用架构
    • 垂直应用架构
    • 分布式服务架构
    • 流动计算架构
  • Dubbo概念
  • Dubbo整合Spring XML配置
    • 搭建一个Dubbo项目流程
      • 创建三个项目
        • 创建maven父工程 dubbo-spring-xml
          • pom.xml配置
        • 创建API接口工程 dubbo-spring-xml-api
          • POM.XML
          • 实体Bean配置
          • 业务逻辑层Service
        • 创建服务提供者组件 dubbo-spring-xml-provider
          • pom.xml
          • 编写 service对外暴露接口实现类
          • application.xml配置
          • dubbo服务端provider.xml配置
          • web.xml
        • 创建服务消费者 dubbo-spring-xml-customer
          • pom.xml
          • 编写服务消费者 调用服务
          • 服务消费者配置 applicationContentxt.xml
          • 服务消费者dubbo配置 customer.xml
          • 服务消费者 web.xml
      • 运行测试

背景

随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。

单一应用架构

当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。此时,用于简化增删改查工作量的数据访问框架(ORM)是关键。

垂直应用架构

当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。此时,用于加速前端页面开发的Web框架(MVC)是关键。

分布式服务架构

当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。此时,用于提高业务复用及整合的分布式服务框架(RPC)是关键。

流动计算架构

当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。此时,用于提高机器利用率的资源调度和治理中心(SOA)是关键。

Dubbo概念

Dubbo 属于** 分布式服务架构**中的技术实现:
是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。 它的特性如下 面向接口代理的高性能RPC调用 智能负载均衡 服务自动注册与发现 高度可扩展能力 运行期流量调度 可视化的服务治理与运维

Dubbo整合Spring XML配置

搭建一个Dubbo项目流程

创建三个项目

【微服务架构】-Dubbo生态-Dubbo之Spring XML配置_第1张图片

创建maven父工程 dubbo-spring-xml

pom.xml配置
<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">
   <modelVersion>4.0.0modelVersion>
   <groupId>org.dkgroupId>
   <artifactId>dubbo-spring-xmlartifactId>
   <version>0.0.1-SNAPSHOTversion>
   <packaging>pompackaging>
   <modules>
      <module>dubbo-spring-xml-providermodule>
      <module>dubbo-spring-xml-customermodule>
      <module>dubbo-spring-xml-apimodule>
   modules>
   <properties>
      
      <java.version>1.7java.version>
      <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
      
      <jsp.version>2.2jsp.version>
      <jstl.version>1.2jstl.version>
      <servlet.version>3.1.0servlet.version>
      
      <spring-framework.version>4.3.10.RELEASEspring-framework.version>
      
      <logback.version>1.0.13logback.version>
      <slf4j.version>1.7.5slf4j.version>
   properties>
   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>javaxgroupId>
            <artifactId>javaee-web-apiartifactId>
            <version>7.0version>
            <scope>providedscope>
         dependency>

         
         <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring-framework.version}version>
         dependency>

         
         <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>${jstl.version}version>
         dependency>

         <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>${servlet.version}version>
            <scope>providedscope>
         dependency>

         <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>${jsp.version}version>
            <scope>providedscope>
         dependency>

         
         <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring-framework.version}version>
         dependency>

         
         <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>${slf4j.version}version>
         dependency>
         <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.16version>
         dependency>
         <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>jcl-over-slf4jartifactId>
            <version>${slf4j.version}version>
         dependency>
         <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>${logback.version}version>
         dependency>
         <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-coreartifactId>
            <version>${logback.version}version>
         dependency>
         <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-accessartifactId>
            <version>${logback.version}version>
         dependency>

         
         
         
         <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.8.6version>
         dependency>
         <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.8.6version>
         dependency>
         <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-annotationsartifactId>
            <version>2.8.6version>
         dependency>
         <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>2.8.6version>
         dependency>

         
         <dependency>
            <groupId>commons-iogroupId>
            <artifactId>commons-ioartifactId>
            <version>2.3version>
         dependency>
         <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <version>3.2version>
         dependency>
         <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring-framework.version}version>
            <scope>testscope>
         dependency>
         <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring-framework.version}version>
            <scope>testscope>
         dependency>

         <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjrtartifactId>
            <version>1.8.6version>
         dependency>
         
         <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjtoolsartifactId>
            <version>1.8.9version>
         dependency>
         <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.7.4version>
         dependency>

         <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
         dependency>

         
         <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.5.8version>
         dependency>
         
         <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>2.9.0version>
         dependency>
         <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.3.2version>
         dependency>
      dependencies>
   dependencyManagement>
   
   <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
        plugins>
   build>
project>

创建API接口工程 dubbo-spring-xml-api

对外暴露RPC接口模块

POM.XML
<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">
  <modelVersion>4.0.0modelVersion>
  <parent>
    <groupId>org.dkgroupId>
    <artifactId>dubbo-spring-xmlartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>
  <artifactId>dubbo-spring-xml-apiartifactId>
   <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
        plugins>
   build>
project>
实体Bean配置
public class DefaultBean {
   private String str;
   private String methodName;
   private long timestamp;

   public String getStr() {
      return str;
   }

   public void setStr(String str) {
      this.str = str;
   }

   public String getMethodName() {
      return methodName;
   }

   public void setMethodName(String methodName) {
      this.methodName = methodName;
   }

   public long getTimestamp() {
      return timestamp;
   }
   public void setTimestamp(long timestamp) {
      this.timestamp = timestamp;
   }
   @Override
   public String toString() {
      return "DefaultBean [str=" + str + ", methodName=" + methodName + ", timestamp=" + timestamp + "]";
   }
}
业务逻辑层Service
public interface DefaultApiService {
   /**
    * 默认方法
    * @param str
    * @return
    */
   String defaultMethod(String str);
}


创建服务提供者组件 dubbo-spring-xml-provider

需要将dubbo-spring-xml-api 依赖引入dubbo-spring-xml-provider中。

pom.xml
<dependencies>
   <dependency>
      <groupId>javaxgroupId>
      <artifactId>javaee-web-apiartifactId>
      <scope>providedscope>
   dependency>

   
   <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
   dependency>

   
   <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>jstlartifactId>
   dependency>

   <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <scope>providedscope>
   dependency>

   <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <scope>providedscope>
   dependency>

   
   <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
   dependency>

   
   <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-apiartifactId>
   dependency>
   <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
   dependency>
   <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>jcl-over-slf4jartifactId>
   dependency>
   <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
   dependency>
   <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-coreartifactId>
   dependency>
   <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-accessartifactId>
   dependency>

   
   
   
   <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
   dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
   dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-annotationsartifactId>
   dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-coreartifactId>
   dependency>

   
   <dependency>
      <groupId>commons-iogroupId>
      <artifactId>commons-ioartifactId>
   dependency>

   <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <scope>testscope>
   dependency>
   <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <scope>testscope>
   dependency>

   <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjrtartifactId>
   dependency>
   
   <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjtoolsartifactId>
   dependency>
   <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjweaverartifactId>
   dependency>

   <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <scope>testscope>
   dependency>

   
   <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>dubboartifactId>
   dependency>
   
   <dependency>
      <groupId>redis.clientsgroupId>
      <artifactId>jedisartifactId>
   dependency>

   <dependency>
      <groupId>org.niuganggroupId>
      <artifactId>dubbo-spring-xml-apiartifactId>
      <version>0.0.1-SNAPSHOTversion>
   dependency>
dependencies>

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.pluginsgroupId>
         <artifactId>maven-compiler-pluginartifactId>
         <version>2.3.2version>
         <configuration>
            <source>${java.version}source>
            <target>${java.version}target>
         configuration>
      plugin>
      <plugin>
         <groupId>org.apache.maven.pluginsgroupId>
         <artifactId>maven-war-pluginartifactId>
         <version>2.3version>
         <configuration>
            <failOnMissingWebXml>falsefailOnMissingWebXml>
         configuration>
      plugin>
   plugins>
build>
编写 service对外暴露接口实现类
public class DefaultServiceImpl implements DefaultApiService {

   //模拟数据
   public static List<DefaultBean> list = new ArrayList<DefaultBean>();
   static {

      DefaultBean defaultBean = new DefaultBean();
      defaultBean.setStr("1");
      defaultBean.setMethodName("defaultMethod");
      defaultBean.setTimestamp(System.currentTimeMillis());
      list.add(defaultBean);
      DefaultBean defaultBean2 = new DefaultBean();
      defaultBean2.setStr("2");
      defaultBean2.setMethodName("defaultMethod2");
      defaultBean2.setTimestamp(System.currentTimeMillis());
      list.add(defaultBean2);
      DefaultBean defaultBean3 = new DefaultBean();
      defaultBean3.setStr("3");
      defaultBean3.setMethodName("defaultMethod3");
      defaultBean3.setTimestamp(System.currentTimeMillis());
      list.add(defaultBean);
      DefaultBean defaultBean4 = new DefaultBean();
      defaultBean4.setStr("4");
      defaultBean4.setMethodName("defaultMethod4");
      defaultBean4.setTimestamp(System.currentTimeMillis());
      list.add(defaultBean4);
      DefaultBean defaultBean5 = new DefaultBean();
      defaultBean5.setStr("5");
      defaultBean5.setMethodName("defaultMethod5");
      defaultBean5.setTimestamp(System.currentTimeMillis());
      list.add(defaultBean5);
   }

   @Override
   public String defaultMethod(String str) {
      Random random = new Random();
      int nextInt = random.nextInt(6);
      String res = null;
      if (nextInt > 0) {
         DefaultBean defaultBean = list.get(nextInt - 1);
         res = defaultBean.getStr() + "#" + defaultBean.getMethodName() + "#" + defaultBean.getTimestamp()
               + "if-defaultMethod:" + str + "^^^^^^^^^^^^";
      } else {
         DefaultBean defaultBean = list.get(nextInt);
         res = defaultBean.getStr() + "#" + defaultBean.getMethodName() + "#" + defaultBean.getTimestamp()
               + "else-defaultMethod:" + str + "^^^^^^^^^^^^";
      }
      return res;
   }
application.xml配置

<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: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/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <import resource="provider.xml" />
   
   
   <context:component-scan base-package="org.niugang" />
   
   
   <mvc:annotation-driven />
   
beans>
dubbo服务端provider.xml配置

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

   

   
   <dubbo:application name="hello-world-app" />

   
   <dubbo:registry address="redis://localhost:6379"
      timeout="30000">
      
      
      <dubbo:parameter key="max.idle" value="10" />
      <dubbo:parameter key="min.idle" value="5" />
      <dubbo:parameter key="max.active" value="20" />
      <dubbo:parameter key="max.total" value="100" />
   dubbo:registry>

   
   <dubbo:protocol name="dubbo" port="20880" accesslog="true" />

   
   <bean id="defaultService" class="org.niugang.service.DefaultServiceImpl" />
   
   
   
   <dubbo:service interface="org.niugang.service.DefaultApiService"
      ref="defaultService" version="1.0.0" />

   
   <dubbo:monitor protocol="registry">dubbo:monitor>
   
   
beans>
web.xml

<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   
   <context-param>
      <param-name>contextConfigLocationparam-name>
      
      <param-value>classpath:applicationContext.xmlparam-value>
   context-param>
   
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener
      listener-class>
   listener>
   
   <servlet>
      <servlet-name>dispatcherservlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet
      servlet-class>
      <init-param>
         <param-name>contextConfigLocationparam-name>
         <param-value>classpath:applicationContext.xmlparam-value>
      init-param>
      <load-on-startup>1load-on-startup>
   servlet>
   <servlet-mapping>
      <servlet-name>dispatcherservlet-name>
      <url-pattern>/url-pattern>
   servlet-mapping>
   
   <filter>
      <filter-name>characterEncodingFilterfilter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
      <init-param>
         <param-name>encodingparam-name>
         <param-value>UTF-8param-value>
      init-param>
      <init-param>
         <param-name>forceEncodingparam-name>
         <param-value>trueparam-value>
      init-param>
   filter>
   
   <filter-mapping>
      <filter-name>characterEncodingFilterfilter-name>
      <url-pattern>/*url-pattern>
   filter-mapping>
web-app>

创建服务消费者 dubbo-spring-xml-customer

需要引入dubbo-spring-xml-api依赖

pom.xml
<dependencies>
   <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <scope>testscope>
   dependency>
   <dependency>
      <groupId>javaxgroupId>
      <artifactId>javaee-web-apiartifactId>
      <scope>providedscope>
   dependency>

   
   <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
   dependency>

   
   <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>jstlartifactId>
   dependency>

   <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <scope>providedscope>
   dependency>

   <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <scope>providedscope>
   dependency>

   
   <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
   dependency>

   
   <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-apiartifactId>
   dependency>
   <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
   dependency>
   <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>jcl-over-slf4jartifactId>
   dependency>
   <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
   dependency>
   <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-coreartifactId>
   dependency>
   <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-accessartifactId>
   dependency>

   
   
   
   <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
   dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
   dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-annotationsartifactId>
   dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-coreartifactId>
   dependency>

   
   <dependency>
      <groupId>commons-iogroupId>
      <artifactId>commons-ioartifactId>
   dependency>

   <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <scope>testscope>
   dependency>
   <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <scope>testscope>
   dependency>

   <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjrtartifactId>
   dependency>
   
   <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjtoolsartifactId>
   dependency>
   <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjweaverartifactId>
   dependency>
   
   <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>dubboartifactId>
   dependency>
   
   <dependency>
      <groupId>redis.clientsgroupId>
      <artifactId>jedisartifactId>
   dependency>
   
   <dependency>
      <groupId>org.niuganggroupId>
      <artifactId>dubbo-spring-xml-apiartifactId>
      <version>0.0.1-SNAPSHOTversion>
   dependency>
   <dependency>
      <groupId>org.apache.commonsgroupId>
      <artifactId>commons-lang3artifactId>
   dependency>
   <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
   dependency>
dependencies>

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.pluginsgroupId>
         <artifactId>maven-compiler-pluginartifactId>
         <version>2.3.2version>
         <configuration>
            <source>${java.version}source>
            <target>${java.version}target>
         configuration>
      plugin>
      <plugin>
         <groupId>org.apache.maven.pluginsgroupId>
         <artifactId>maven-war-pluginartifactId>
         <version>2.3version>
         <configuration>
            <failOnMissingWebXml>falsefailOnMissingWebXml>
         configuration>
      plugin>
   plugins>
   <finalName>dubbo-spring-xml-customerfinalName>
build>
编写服务消费者 调用服务
@RestController
public class DemoConsumerController {
   /**
    * 引入服务提供者
    */
   @Autowired
   private DefaultApiService defaultService;
   @RequestMapping("/sayHello")
   public String sayHello(@RequestParam String name) {
      return defaultService.defaultMethod(name);
   }
}
服务消费者配置 applicationContentxt.xml

<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:mvc="http://www.springframework.org/schema/mvc"
      xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <import resource="classpath:consumer.xml" />
   
   <context:component-scan base-package="org.niugang" />
   
   <mvc:annotation-driven />
beans>
服务消费者dubbo配置 customer.xml

<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">
    
   
   <dubbo:application name="hello-world-customer" />

   <dubbo:registry address="redis://localhost:6379"
      timeout="30000" check="false" />

   
   
   
   <dubbo:reference id="defaultService" interface="org.niugang.service.DefaultApiService"  version="1.0.0"/>

   
   <dubbo:monitor protocol="registry">dubbo:monitor>
   
   <dubbo:consumer check="false">dubbo:consumer>
beans>

服务消费者 web.xml

<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   
   <context-param>
      <param-name>contextConfigLocationparam-name>
      
      <param-value>classpath*:applicationContext.xmlparam-value>
   context-param>
   
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener
      listener-class>
   listener>
   
   <servlet>
      <servlet-name>dispatcherservlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet
      servlet-class>
      <init-param>
         <param-name>contextConfigLocationparam-name>
         <param-value>classpath*:applicationContext.xmlparam-value>
      init-param>
      <load-on-startup>1load-on-startup>
   servlet>
   <servlet-mapping>
      <servlet-name>dispatcherservlet-name>
      <url-pattern>/url-pattern>
   servlet-mapping>
   
   <filter>
      <filter-name>characterEncodingFilterfilter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
      <init-param>
         <param-name>encodingparam-name>
         <param-value>UTF-8param-value>
      init-param>
      <init-param>
         <param-name>forceEncodingparam-name>
         <param-value>trueparam-value>
      init-param>
   filter>
   
   <filter-mapping>
      <filter-name>characterEncodingFilterfilter-name>
      <url-pattern>/url-pattern>
   filter-mapping>
web-app>

运行测试

以上配置完成。将服务提供者,服务消费分别部署到不同的tomcat中。
启动服务提供者,启动服务消费者。
配置Tomcat后运行:

【微服务架构】-Dubbo生态-Dubbo之Spring XML配置_第2张图片
【微服务架构】-Dubbo生态-Dubbo之Spring XML配置_第3张图片
【微服务架构】-Dubbo生态-Dubbo之Spring XML配置_第4张图片
录入请求地址:http://localhost:8081/sayHello?name=dk
【微服务架构】-Dubbo生态-Dubbo之Spring XML配置_第5张图片

你可能感兴趣的:(微服务)