dubbo 使用 學習四(springmvc+spring+dubbo+zookeeper本地偽集群)

通過之前的兩篇我們能在本地搭建單一和集群兩種方式的dubbo服務(wù)邮绿,這篇我們來看 springmvc+spring+mybatis+dubbo+zookeeper 的項目的搭建……

不管是什么樣的項目中使用dubbo 和zookeeper做分布式的服務(wù)阅酪,我們要做的就是將服務(wù)提供者注冊到zookeeper中而服務(wù)消費者來消費服務(wù)提供者提供的服務(wù)糖耸,所以我們要做的就是安裝zookeeper 和服務(wù)注冊到zookeeper中稻轨,服務(wù)消費者從zookeeper中獲取服務(wù)提供者的信息獲取并消費服務(wù),廢話少說下面我們來看看代碼……

我這個demo配置的是單個的所以安裝zookeeper之后只需要修改DataDir 的路徑其他的都不用修改而且安裝也很簡單所以這里就不啰嗦了坊夫,自己找度娘……

一厉熟、服務(wù)提供者

1抹估、整體目錄結(jié)構(gòu):

通過這個目錄結(jié)構(gòu)我們看到的就是一個平時的web項目……

2正压、這里需要的jar包除了平時springmvc+sping+mybatis 的jar包還有zookeeper 和dubbo的相關(guān)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">

我們發(fā)現(xiàn)這里一點都沒變化迂尝,全都是一樣哈哈哈哈

日志的配置和數(shù)據(jù)源的配置就不貼出來了脱茉,這兩個還是怎么配就怎么配沒什么變化。到這里我們需要的jar包都好了雹舀,配置文件也都好了芦劣,我們來看看具體的代碼

6、服務(wù)接口和服務(wù)接口實現(xiàn)類

[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層原來怎么寫現(xiàn)在還是怎么寫

7说榆、控制層的代碼虚吟,其實這個控制層的代碼和這里說的dubbo zookeeper是沒有關(guān)系的所以我們寫不寫都可以寸认,但是這里想說明一點就是提供的服務(wù)的接口和實現(xiàn)類在本項目中控制層也是可以直接調(diào)用的

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

}

}

到這里完了,是不是發(fā)現(xiàn)和平時的springmvc+spring+mybatis 項目是一樣的串慰,只不過在spring的配置文件中多加了dubbo 的配置偏塞。確實dubbo的使用配置確實很方便。

我們簡單總結(jié)一下流程:

(1)邦鲫、導(dǎo)入dubbo zookeeper相關(guān)jar包灸叼,如果使用的是mavne直接按照上面的方式配置,注意有可能會出現(xiàn)spring 包沖突的情況……

(2)庆捺、spring配置文件中加入dubbo 相關(guān)的配置古今,這部分配置和在java工程的配置都是一樣,就是將指定的服務(wù)注冊到zookeeper這個注冊中心并把這個服務(wù)相關(guān)的端口暴露出去

發(fā)現(xiàn)沒說來說去就多了這兩項……

二滔以、服務(wù)消費者

1捉腥、整體目錄結(jié)構(gòu):

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

這里注意你画,服務(wù)消費者的和服務(wù)提供者一樣抵碟,這樣配置后會出現(xiàn)spring的jar包沖突,所以要注意坏匪!

還有要將服務(wù)提供者打成jar包導(dǎo)入服務(wù)消費者拟逮,因為我們之后會看到服務(wù)消費者調(diào)用服務(wù)提供者的服務(wù)方法時會用到……

3、web.xml 配置和平時的配置沒什么區(qū)別

[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敦迄,有區(qū)別的就是這個文件中配置了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颅崩、服務(wù)消費者控制層的代碼

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

}

}

我們通過代碼看到這里自動注入了服務(wù)提供者的UserService 接口而,而之后的方法中調(diào)用了接口實現(xiàn)類的方法say(String str)蕊苗,這樣就簡單的實現(xiàn)了服務(wù)的調(diào)用沿后。

7、服務(wù)提供者朽砰、服務(wù)消費者啟動后消費服務(wù)端的服務(wù)結(jié)果……

(1)尖滚、服務(wù)提供者服務(wù)接口實現(xiàn)類

(2)、發(fā)送請求

(3)瞧柔、服務(wù)消費者消費結(jié)果

ok 到這里就結(jié)束了……

這里給大家看一個spring jar包沖突的那個jar包:

這個jar包就是每一run pom.xml 是會自動加入的一個jar包漆弄,記得刪除這個jar包不然會出現(xiàn)jar包沖突異常……

三造锅、總結(jié)

1撼唾、通過前面的兩篇和這一篇我們了解了怎么搭建dubbo zookeeper的項目,從第一篇文章的第一個列子到這個列子我們發(fā)現(xiàn)使用dubbo提供服務(wù)確實很簡單哥蔚,因為我們通過學習發(fā)現(xiàn)唯一不同的就是安裝個zookeeper和在spring的配置文件中配置dubbo(當然還有不用spring配置的倒谷,但文檔不推薦使用)蛛蒙,還有一個就是將服務(wù)提供者的服務(wù)接口打成jar包導(dǎo)入到服務(wù)消費者,簡單的demo 和初學就到這里渤愁,之后再學習牵祟,希望能了解更多dubbo的高級特性和大家分享!

愿意了解或者源碼的朋友直接求求交流分享技術(shù):2042849237

更多詳細源碼參考來源:http://minglisoft.cn/technology

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末抖格,一起剝皮案震驚了整個濱河市诺苹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌雹拄,老刑警劉巖收奔,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異滓玖,居然都是意外死亡筹淫,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門呢撞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人饰剥,你說我怎么就攤上這事殊霞。” “怎么了汰蓉?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵绷蹲,是天一觀的道長。 經(jīng)常有香客問我顾孽,道長祝钢,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任若厚,我火速辦了婚禮拦英,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘测秸。我一直安慰自己疤估,他們只是感情好,可當我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布霎冯。 她就那樣靜靜地躺著铃拇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪沈撞。 梳的紋絲不亂的頭發(fā)上慷荔,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天,我揣著相機與錄音缠俺,去河邊找鬼显晶。 笑死贷岸,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的吧碾。 我是一名探鬼主播凰盔,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼倦春!你這毒婦竟也來了户敬?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤睁本,失蹤者是張志新(化名)和其女友劉穎尿庐,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體呢堰,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡抄瑟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了枉疼。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片皮假。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖骂维,靈堂內(nèi)的尸體忽然破棺而出惹资,到底是詐尸還是另有隱情,我是刑警寧澤航闺,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布褪测,位于F島的核電站,受9級特大地震影響潦刃,放射性物質(zhì)發(fā)生泄漏侮措。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一乖杠、第九天 我趴在偏房一處隱蔽的房頂上張望分扎。 院中可真熱鬧,春花似錦胧洒、人聲如沸笆包。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽庵佣。三九已至,卻和暖如春汛兜,著一層夾襖步出監(jiān)牢的瞬間巴粪,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留肛根,地道東北人辫塌。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓,卻偏偏與公主長得像派哲,于是被迫代替她去往敵國和親臼氨。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容