通過之前的兩篇我們能在本地搭建單一和集群兩種方式的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