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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,884评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,347评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,435评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,509评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,611评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,837评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,987评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,730评论 0 267
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,194评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,525评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,664评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,334评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,944评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,764评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,997评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,389评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,554评论 2 349

推荐阅读更多精彩内容