dubbo 使用 学习四(springmvc+spring+dubbo+zookeeper本地伪集群)

通过之前的两篇我们能在本地搭建单一和集群两种方式的dubbo服务,这篇我们来看 springmvc+spring+mybatis+dubbo+zookeeper 的项目的搭建……

不管是什么样的项目中使用dubbo 和zookeeper做分布式的服务,我们要做的就是将服务提供者注册到zookeeper中而服务消费者来消费服务提供者提供的服务,所以我们要做的就是安装zookeeper 和服务注册到zookeeper中,服务消费者从zookeeper中获取服务提供者的信息获取并消费服务,废话少说下面我们来看看代码……

我这个demo配置的是单个的所以安装zookeeper之后只需要修改DataDir 的路径其他的都不用修改而且安装也很简单所以这里就不啰嗦了,自己找度娘……

一、服务提供者

1、整体目录结构:

通过这个目录结构我们看到的就是一个平时的web项目……

2、这里需要的jar包除了平时springmvc+sping+mybatis 的jar包还有zookeeper 和dubbo的相关jar包,下面看看pom.xml 配置文件

这里说明一下,这样配置后有一个springjar包冲突,是哪个冲突了是不是不好找,其实也不是,你先把dubbo zookeeper的配置去了,看看有哪些jar包在加上看看多的那个spring 直接删除就可以……

[html]view plaincopyprint?

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.test

dubboser3

0.0.1-SNAPSHOT

jar

dubboser3

http://maven.apache.org

UTF-8



org.springframework

spring-core

4.2.0.RELEASE

org.springframework

spring-webmvc

4.2.0.RELEASE

org.springframework

spring-tx

4.2.0.RELEASE

org.springframework

spring-jdbc

4.2.0.RELEASE

org.springframework

spring-test

4.2.0.RELEASE

org.aspectj

aspectjweaver

1.8.6


org.mybatis

mybatis

3.3.0


org.mybatis

mybatis-spring

1.2.3


mysql

mysql-connector-java

5.1.36


com.alibaba

druid

1.0.15

org.codehaus.jackson

jackson-mapper-asl

1.9.12


commons-fileupload

commons-fileupload

1.3.1


javax.servlet

javax.servlet-api

3.1.0


com.fasterxml.jackson.core

jackson-core

2.6.1

com.fasterxml.jackson.core

jackson-databind

2.6.1


net.sf.json-lib

json-lib

2.4

jdk15


org.slf4j

slf4j-log4j12

1.7.2


org.apache.taglibs

taglibs-standard-spec

1.2.1

org.apache.taglibs

taglibs-standard-impl

1.2.1


commons-codec

commons-codec

1.4


commons-logging

commons-logging

1.1.1

commons-httpclient

commons-httpclient

3.0.1


junit

junit

4.12

test


com.alibaba

dubbo

2.5.3


com.github.sgroschupf

zkclient

0.1


org.apache.zookeeper

zookeeper

3.4.5


org.apache.maven.plugins

maven-war-plugin

2.1.1


WebRoot\WEB-INF\web.xml


WebRoot

org.apache.maven.plugins

maven-compiler-plugin

1.7

1.7

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.test

dubboser3

0.0.1-SNAPSHOT

jar

dubboser3

http://maven.apache.org

UTF-8



org.springframework

spring-core

4.2.0.RELEASE

org.springframework

spring-webmvc

4.2.0.RELEASE

org.springframework

spring-tx

4.2.0.RELEASE

org.springframework

spring-jdbc

4.2.0.RELEASE

org.springframework

spring-test

4.2.0.RELEASE

org.aspectj

aspectjweaver

1.8.6


org.mybatis

mybatis

3.3.0


org.mybatis

mybatis-spring

1.2.3


mysql

mysql-connector-java

5.1.36


com.alibaba

druid

1.0.15

org.codehaus.jackson

jackson-mapper-asl

1.9.12


commons-fileupload

commons-fileupload

1.3.1


javax.servlet

javax.servlet-api

3.1.0


com.fasterxml.jackson.core

jackson-core

2.6.1

com.fasterxml.jackson.core

jackson-databind

2.6.1


net.sf.json-lib

json-lib

2.4

jdk15


org.slf4j

slf4j-log4j12

1.7.2


org.apache.taglibs

taglibs-standard-spec

1.2.1

org.apache.taglibs

taglibs-standard-impl

1.2.1


commons-codec

commons-codec

1.4


commons-logging

commons-logging

1.1.1

commons-httpclient

commons-httpclient

3.0.1


junit

junit

4.12

test


com.alibaba

dubbo

2.5.3


com.github.sgroschupf

zkclient

0.1


org.apache.zookeeper

zookeeper

3.4.5


org.apache.maven.plugins

maven-war-plugin

2.1.1


WebRoot\WEB-INF\web.xml


WebRoot

org.apache.maven.plugins

maven-compiler-plugin

1.7

1.7

3、web.xml 配置文件,这里没什么不同

[html]view plaincopyprint?


dubbocli3

org.springframework.web.context.ContextLoaderListener


org.springframework.web.util.Log4jConfigListener

contextConfigLocation

classpath:config/springmvc-servlet.xml,

classpath:config/ApplicationContext.xml


log4jConfigLocation

classpath:log4j.properties

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceEncoding

true

encodingFilter

*.do

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:config/springmvc-servlet.xml

1

springmvc

*.do

DruidStatView

com.alibaba.druid.support.http.StatViewServlet

DruidStatView

/druid/*

DruidWebStatFilter

com.alibaba.druid.support.http.WebStatFilter

exclusions

weburi.json,.html,.js,.gif,.jpg,.png,.css,.ico,/druid/*

sessionStatMaxCount

10

principalSessionName

FRONT_USER

DruidWebStatFilter

*.do

DruidWebStatFilter

*.jsp

index.html

index.htm

index.jsp

default.html


dubbocli3

org.springframework.web.context.ContextLoaderListener


org.springframework.web.util.Log4jConfigListener

contextConfigLocation

classpath:config/springmvc-servlet.xml,

classpath:config/ApplicationContext.xml


log4jConfigLocation

classpath:log4j.properties

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceEncoding

true

encodingFilter

*.do

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:config/springmvc-servlet.xml

1

springmvc

*.do

DruidStatView

com.alibaba.druid.support.http.StatViewServlet

DruidStatView

/druid/*

DruidWebStatFilter

com.alibaba.druid.support.http.WebStatFilter

exclusions

weburi.json,.html,.js,.gif,.jpg,.png,.css,.ico,/druid/*

sessionStatMaxCount

10

principalSessionName

FRONT_USER

DruidWebStatFilter

*.do

DruidWebStatFilter

*.jsp

index.html

index.htm

index.jsp

default.html

4、看看spring的配置,ApplicationContext.xml配置文件中的配置

[html]view plaincopyprint?


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:task="http://www.springframework.org/schema/task"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd

">



${druid.driverClassName}

${druid.url}

${druid.username}

${druid.password}

${druid.initialSize}

${druid.minIdle}

${druid.maxActive}

${druid.maxWait}

${druid.timeBetweenEvictionRunsMillis}

${druid.minEvictableIdleTimeMillis}

${druid.testWhileIdle}

${druid.testOnBorrow}

${druid.testOnReturn}

${druid.poolPreparedStatements}

${druid.maxPoolPreparedStatementPerConnectionSize}



class="org.springframework.jdbc.datasource.DataSourceTransactionManager">












ref="demoService"/>



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:task="http://www.springframework.org/schema/task"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd

">



${druid.driverClassName}

${druid.url}

${druid.username}

${druid.password}

${druid.initialSize}

${druid.minIdle}

${druid.maxActive}

${druid.maxWait}

${druid.timeBetweenEvictionRunsMillis}

${druid.minEvictableIdleTimeMillis}

${druid.testWhileIdle}

${druid.testOnBorrow}

${druid.testOnReturn}

${druid.poolPreparedStatements}

${druid.maxPoolPreparedStatementPerConnectionSize}



class="org.springframework.jdbc.datasource.DataSourceTransactionManager">












ref="demoService" />


我们看到了这个配置文件相比平时的springmvc+spring+mybatis 项目的配置文件就多了个dubbo的这部分配置,所以从这里看出dubbo zookeeper的使用还是挺方便的……

5、springmvc 的配置文件springmvc-servlet.xml

[html]view plaincopyprint?


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"

xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">




class="org.springframework.web.servlet.view.InternalResourceViewResolver">


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">




class="org.springframework.web.servlet.view.InternalResourceViewResolver">

我们发现这里一点都没变化,全都是一样哈哈哈哈

日志的配置和数据源的配置就不贴出来了,这两个还是怎么配就怎么配没什么变化。到这里我们需要的jar包都好了,配置文件也都好了,我们来看看具体的代码

6、服务接口和服务接口实现类

[java]view plaincopyprint?

packagecom.test.dubboser3.service;

publicinterfaceUserService {

publicString say(String str);

}

package com.test.dubboser3.service;

public interface UserService {

public String say(String str);

}

[java]view plaincopyprint?

packagecom.test.dubboser3.service;

importorg.springframework.stereotype.Service;

@Service("userServiceImp")

publicclassUserServiceImpimplementsUserService {

@Override

publicString say(String str) {

// TODO Auto-generated method stub

return"---------"+str+"----------";

}

}

package com.test.dubboser3.service;

import org.springframework.stereotype.Service;

@Service("userServiceImp")

public class UserServiceImp implements UserService {

@Override

public String say(String str) {

// TODO Auto-generated method stub

return "---------"+str+"----------";

}

}

我们看到service层原来怎么写现在还是怎么写

7、控制层的代码,其实这个控制层的代码和这里说的dubbo zookeeper是没有关系的所以我们写不写都可以,但是这里想说明一点就是提供的服务的接口和实现类在本项目中控制层也是可以直接调用的

[java]view plaincopyprint?

packagecom.test.dubboser3.controller;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importcom.test.dubboser3.service.UserService;

@Controller

@RequestMapping(value="/user")

publicclassUserController {

@Autowired

privateUserService userServiceImp;

@RequestMapping(value="/info.do")

publicString  info(){

String str=userServiceImp.say("hello");

//String str="12312";

System.out.println(str);

return"index";

}

}

package com.test.dubboser3.controller;

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

import org.springframework.stereotype.Controller;

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

import com.test.dubboser3.service.UserService;

@Controller

@RequestMapping(value="/user")

public class UserController {

@Autowired

private UserService userServiceImp;

@RequestMapping(value="/info.do")

public String  info(){

String str=userServiceImp.say("hello");

//String str="12312";

System.out.println(str);

return "index";

}

}

到这里完了,是不是发现和平时的springmvc+spring+mybatis 项目是一样的,只不过在spring的配置文件中多加了dubbo 的配置。确实dubbo的使用配置确实很方便。

我们简单总结一下流程:

(1)、导入dubbo zookeeper相关jar包,如果使用的是mavne直接按照上面的方式配置,注意有可能会出现spring 包冲突的情况……

(2)、spring配置文件中加入dubbo 相关的配置,这部分配置和在java工程的配置都是一样,就是将指定的服务注册到zookeeper这个注册中心并把这个服务相关的端口暴露出去

发现没说来说去就多了这两项……

二、服务消费者

1、整体目录结构:

2、项目需要的jar包即这里使用了maven 所以pom.xml配置文件如下:

[html]view plaincopyprint?

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.test

dubbocli3

0.0.1-SNAPSHOT

jar

dubbocli3

http://maven.apache.org

UTF-8



org.springframework

spring-core

4.2.0.RELEASE

org.springframework

spring-webmvc

4.2.0.RELEASE

org.springframework

spring-tx

4.2.0.RELEASE

org.springframework

spring-jdbc

4.2.0.RELEASE

org.springframework

spring-test

4.2.0.RELEASE

org.aspectj

aspectjweaver

1.8.6


org.mybatis

mybatis

3.3.0


org.mybatis

mybatis-spring

1.2.3


mysql

mysql-connector-java

5.1.36


com.alibaba

druid

1.0.15

org.codehaus.jackson

jackson-mapper-asl

1.9.12


commons-fileupload

commons-fileupload

1.3.1


javax.servlet

javax.servlet-api

3.1.0


com.fasterxml.jackson.core

jackson-core

2.6.1

com.fasterxml.jackson.core

jackson-databind

2.6.1


net.sf.json-lib

json-lib

2.4

jdk15


org.slf4j

slf4j-log4j12

1.7.2


org.apache.taglibs

taglibs-standard-spec

1.2.1

org.apache.taglibs

taglibs-standard-impl

1.2.1


commons-codec

commons-codec

1.4


commons-logging

commons-logging

1.1.1

commons-httpclient

commons-httpclient

3.0.1


junit

junit

4.12

test


com.alibaba

dubbo

2.5.3


com.github.sgroschupf

zkclient

0.1


org.apache.zookeeper

zookeeper

3.4.5


org.apache.maven.plugins

maven-war-plugin

2.1.1


WebRoot\WEB-INF\web.xml


WebRoot

org.apache.maven.plugins

maven-compiler-plugin

1.7

1.7

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.test

dubbocli3

0.0.1-SNAPSHOT

jar

dubbocli3

http://maven.apache.org

UTF-8



org.springframework

spring-core

4.2.0.RELEASE

org.springframework

spring-webmvc

4.2.0.RELEASE

org.springframework

spring-tx

4.2.0.RELEASE

org.springframework

spring-jdbc

4.2.0.RELEASE

org.springframework

spring-test

4.2.0.RELEASE

org.aspectj

aspectjweaver

1.8.6


org.mybatis

mybatis

3.3.0


org.mybatis

mybatis-spring

1.2.3


mysql

mysql-connector-java

5.1.36


com.alibaba

druid

1.0.15

org.codehaus.jackson

jackson-mapper-asl

1.9.12


commons-fileupload

commons-fileupload

1.3.1


javax.servlet

javax.servlet-api

3.1.0


com.fasterxml.jackson.core

jackson-core

2.6.1

com.fasterxml.jackson.core

jackson-databind

2.6.1


net.sf.json-lib

json-lib

2.4

jdk15


org.slf4j

slf4j-log4j12

1.7.2


org.apache.taglibs

taglibs-standard-spec

1.2.1

org.apache.taglibs

taglibs-standard-impl

1.2.1


commons-codec

commons-codec

1.4


commons-logging

commons-logging

1.1.1

commons-httpclient

commons-httpclient

3.0.1


junit

junit

4.12

test


com.alibaba

dubbo

2.5.3


com.github.sgroschupf

zkclient

0.1


org.apache.zookeeper

zookeeper

3.4.5


org.apache.maven.plugins

maven-war-plugin

2.1.1


WebRoot\WEB-INF\web.xml


WebRoot

org.apache.maven.plugins

maven-compiler-plugin

1.7

1.7

这里注意,服务消费者的和服务提供者一样,这样配置后会出现spring的jar包冲突,所以要注意!

还有要将服务提供者打成jar包导入服务消费者,因为我们之后会看到服务消费者调用服务提供者的服务方法时会用到……

3、web.xml 配置和平时的配置没什么区别

[html]view plaincopyprint?


dubbocli3

org.springframework.web.context.ContextLoaderListener


org.springframework.web.util.Log4jConfigListener

contextConfigLocation

classpath:config/springmvc-servlet.xml,

classpath:config/ApplicationContext.xml


log4jConfigLocation

classpath:log4j.properties

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceEncoding

true

encodingFilter

*.do

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:config/springmvc-servlet.xml

1

springmvc

*.do

DruidStatView

com.alibaba.druid.support.http.StatViewServlet

DruidStatView

/druid/*

DruidWebStatFilter

com.alibaba.druid.support.http.WebStatFilter

exclusions

weburi.json,.html,.js,.gif,.jpg,.png,.css,.ico,/druid/*

sessionStatMaxCount

10

principalSessionName

FRONT_USER

DruidWebStatFilter

*.do

DruidWebStatFilter

*.jsp

index.html

index.htm

index.jsp

default.html


dubbocli3

org.springframework.web.context.ContextLoaderListener


org.springframework.web.util.Log4jConfigListener

contextConfigLocation

classpath:config/springmvc-servlet.xml,

classpath:config/ApplicationContext.xml


log4jConfigLocation

classpath:log4j.properties

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceEncoding

true

encodingFilter

*.do

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:config/springmvc-servlet.xml

1

springmvc

*.do

DruidStatView

com.alibaba.druid.support.http.StatViewServlet

DruidStatView

/druid/*

DruidWebStatFilter

com.alibaba.druid.support.http.WebStatFilter

exclusions

weburi.json,.html,.js,.gif,.jpg,.png,.css,.ico,/druid/*

sessionStatMaxCount

10

principalSessionName

FRONT_USER

DruidWebStatFilter

*.do

DruidWebStatFilter

*.jsp

index.html

index.htm

index.jsp

default.html

4、spring的配置文件ApplicationContext.xml,有区别的就是这个文件中配置了dubbo

[html]view plaincopyprint?


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:task="http://www.springframework.org/schema/task"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd

">



${druid.driverClassName}

${druid.url}

${druid.username}

${druid.password}

${druid.initialSize}

${druid.minIdle}

${druid.maxActive}

${druid.maxWait}

${druid.timeBetweenEvictionRunsMillis}

${druid.minEvictableIdleTimeMillis}

${druid.testWhileIdle}

${druid.testOnBorrow}

${druid.testOnReturn}

${druid.poolPreparedStatements}

${druid.maxPoolPreparedStatementPerConnectionSize}











xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:task="http://www.springframework.org/schema/task"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd

">



${druid.driverClassName}

${druid.url}

${druid.username}

${druid.password}

${druid.initialSize}

${druid.minIdle}

${druid.maxActive}

${druid.maxWait}

${druid.timeBetweenEvictionRunsMillis}

${druid.minEvictableIdleTimeMillis}

${druid.testWhileIdle}

${druid.testOnBorrow}

${druid.testOnReturn}

${druid.poolPreparedStatements}

${druid.maxPoolPreparedStatementPerConnectionSize}










spring 的配置文件中只多了dubbo的这些配置,其他的还是一样的

5、springmvc 的配置文件springmvc-servlet.xml 没什么变化都是一样的

[html]view plaincopyprint?


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"

xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">




class="org.springframework.web.servlet.view.InternalResourceViewResolver">


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">




class="org.springframework.web.servlet.view.InternalResourceViewResolver">

6、服务消费者控制层的代码

[java]view plaincopyprint?

packagecom.test.dubbocli3.controller;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importcom.test.dubboser3.service.UserService;

/**

*@author WHD

*data 2016年10月19日

*/

@Controller

@RequestMapping(value="/user")

publicclassUserController {

@Autowired

privateUserService demoServicemy;

@RequestMapping(value="/info.do")

publicString  info(){

String str=demoServicemy.say("hello");

System.out.println("consumer: "+str);

return"index";

}

}

package com.test.dubbocli3.controller;

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

import org.springframework.stereotype.Controller;

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

import com.test.dubboser3.service.UserService;

/**

*@author WHD

*data 2016年10月19日

*/

@Controller

@RequestMapping(value="/user")

public class UserController {

@Autowired

private UserService demoServicemy;

@RequestMapping(value="/info.do")

public String  info(){

String str=demoServicemy.say("hello");

System.out.println("consumer: "+str);

return "index";

}

}

我们通过代码看到这里自动注入了服务提供者的UserService 接口而,而之后的方法中调用了接口实现类的方法say(String str),这样就简单的实现了服务的调用。

7、服务提供者、服务消费者启动后消费服务端的服务结果……

(1)、服务提供者服务接口实现类

(2)、发送请求

(3)、服务消费者消费结果

ok 到这里就结束了……

这里给大家看一个spring jar包冲突的那个jar包:

这个jar包就是每一run pom.xml 是会自动加入的一个jar包,记得删除这个jar包不然会出现jar包冲突异常……

三、总结

1、通过前面的两篇和这一篇我们了解了怎么搭建dubbo zookeeper的项目,从第一篇文章的第一个列子到这个列子我们发现使用dubbo提供服务确实很简单,因为我们通过学习发现唯一不同的就是安装个zookeeper和在spring的配置文件中配置dubbo(当然还有不用spring配置的,但文档不推荐使用),还有一个就是将服务提供者的服务接口打成jar包导入到服务消费者,简单的demo 和初学就到这里,之后再学习,希望能了解更多dubbo的高级特性和大家分享!

愿意了解或者源码的朋友直接求求交流分享技术:2042849237

更多详细源码参考来源:http://minglisoft.cn/technology

推荐阅读更多精彩内容