說到分佈式,你們可能很快就能想到阿里巴巴的dubbo,zoookeeper集羣,目前spring-cloud也是比較熱門的,spring-cloud是基於spinrg-boot之上進行協做的,java
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<
modelVersion
>
4.0.0
</
modelVersion
>
<
groupId
>
com.dubbo.test
</
groupId
>
<
artifactId
>
dubbo_provider
</
artifactId
>
<
version
>
0.0.1-SNAPSHOT
</
version
>
<
packaging
>
war
</
packaging
>
<
dependencies
>
<!-- 此處爲provider工程 依賴 dubbo-interface工程 的依賴配置 -->
<
dependency
>
<
groupId
>
com.dubbo.test.interface
</
groupId
>
<
artifactId
>
dubbo_service_interface
</
artifactId
>
<
version
>
0.0.1-SNAPSHOT
</
version
>
</
dependency
>
<!-- 配置Spring -->
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-aop
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-aspects
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-beans
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-context
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-context-support
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-core
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-expression
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-instrument
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-instrument-tomcat
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-messaging
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-orm
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-oxm
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-test
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-tx
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-web
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-webmvc
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-webmvc-portlet
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-websocket
</
artifactId
>
<
version
>
4.3.1.RELEASE
</
version
>
</
dependency
>
<!-- end -->
<!-- dubbo -->
<
dependency
>
<
groupId
>
org.apache.zookeeper
</
groupId
>
<
artifactId
>
zookeeper
</
artifactId
>
<
version
>
3.4.8
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
com.alibaba
</
groupId
>
<
artifactId
>
dubbo
</
artifactId
>
<
version
>
2.5.3
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
com.101tec
</
groupId
>
<
artifactId
>
zkclient
</
artifactId
>
<
version
>
0.4
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.javassist
</
groupId
>
<
artifactId
>
javassist
</
artifactId
>
<
version
>
3.18.1-GA
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.slf4j
</
groupId
>
<
artifactId
>
slf4j-api
</
artifactId
>
<
version
>
1.6.1
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.slf4j
</
groupId
>
<
artifactId
>
slf4j-log4j12
</
artifactId
>
<
version
>
1.6.1
</
version
>
<
scope
>
test
</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>
log4j
</
groupId
>
<
artifactId
>
log4j
</
artifactId
>
<
version
>
1.2.16
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
commons-io
</
groupId
>
<
artifactId
>
commons-io
</
artifactId
>
<
version
>
2.5
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
commons-logging
</
groupId
>
<
artifactId
>
commons-logging
</
artifactId
>
<
version
>
1.2
</
version
>
</
dependency
>
<!-- end -->
</
dependencies
>
<!-- 項目發佈倉庫 -->
<!--
<
distributionManagement
>
<
repository
>
<
id
>
releases
</
id
>
<
name
>
Nexus Release Repository
</
name
>
<
url
>
http://localhost:8081/nexus/content/repositories/releases/
</
url
>
</
repository
>
<
snapshotRepository
>
<
id
>
snapshots
</
id
>
<
name
>
Nexus Snapshot Repository
</
name
>
<
url
>
http://localhost:8081/nexus/content/repositories/snapshots/
</
url
>
</
snapshotRepository
>
</
distributionManagement
>-->
<!--注意:我這裏把項目的發佈倉庫註釋了,由於這裏涉及到maven中settings.xml文件的配置以及nexus,只須要知足上面依賴的jar包存在就ok-->
</
project
>
接下來建立一個包,名爲
com.dubbo.test.provider,而後建立一個TestServiceImpl實現TestService接口,而且實現其接口類的方法。內容以下:
package com.dubbo.test.provider;
import com.dubbo.test.inter.service.TestService;
public class TestServiceImpl implements TestService{
/*
* @see com.dubbo.test.inter.service.TestService#getName(java.lang.String)
*/
public String getName(String name) {
return "hello " + name + ", success!!";
}
}
接着就是比較重要的一步,那就是spring整合dubbo的配置,主要配置接口的暴露,由於jar包都已經依賴進來了。建立spring-dubbo-provider.xml配置文件,文件內容以下:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
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:dubbo
=
"http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation
=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
>
<!-- 提供方應用信息,用於計算依賴關係 -->
<
dubbo:application
name
=
"dubbo-service"
/>
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<
dubbo:registry
address
=
"zookeeper://localhost:2181"
/>
<!-- 用dubbo協議在20880端口暴露服務 -->
<
dubbo:protocol
name
=
"dubbo"
port
=
"20880"
/>
<!-- 聲明須要暴露的服務接口 -->
<
dubbo:service
interface
=
"com.dubbo.test.inter.service.TestService"
ref
=
"testService"
/>
<
bean
id
=
"testService"
class
=
"com.dubbo.test.provider.TestServiceImpl"
/>
</
beans
>
到目前,dubbo的遠程接口提供者已經實現了,接下來建立一個測試類,命名爲:DubboTest,內容以下:
package
com.test;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
DubboTest {
public
static
void
main(String[]
args
) {
ClassPathXmlApplicationContext
context
=
new
ClassPathXmlApplicationContext(
"spring-dubbo-provider.xml"
);
context
.start();
System.
out
.println(
"服務啓動成功!!!"
+
context
.getBean(
"testService"
));
}
}
dubbo-provider測試方式: ①首先啓動zookeeper服務 ②而後在運行DubboTest中的main方法,main方法啓動後,TestService類中的接口就被暴露了 不然是確定不能連上zookeeper的。
三、前兩步已經把interface和provider建立好了,而且測試經過了,接下來咱們來實現它的消費者。
建立一個maven工程名爲: dubbo-consumer,該工程主要是遠程調用dubbo-provider工程中暴露的接口來實現咱們須要的業務開發。
首先pom文件的配置,這裏我就很少說了,跟dubbo-provider工程中的pom.xml文件內容如出一轍。
接下來就是spring整合dubbo,主要配置怎樣使用到dubbo-provider工程中暴露的接口。建立一個spring-dubbo-consumer.xml文件,內容以下:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
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:dubbo
=
"http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation
=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
>
<!-- 提供方應用信息,用於計算依賴關係 -->
<
dubbo:application
name
=
"dubbo_provider"
/>
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<
dubbo:registry
address
=
"zookeeper://localhost:2181"
/>
<
dubbo:consumer
timeout
=
"5000"
/>
<
dubbo:reference
id
=
"testService"
interface
=
"com.dubbo.test.inter.service.TestService"
/>
</
beans
>
最後就是測試了,建立一個DubboTest類,實現以下:
package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dubbo.test.inter.service.TestService;
public class DubboTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubbo-consumer.xml");
context.start();
TestService testService = context.getBean(TestService.class);
System.out.println(testService.getName("xxx"));
System.out.println("dubbo success!!!!!");
}
}
到目前三個工程都已近完成了,接下來就是終極測試:
重點:
①啓動zookeeper服務
②啓動dubbo-provider工程中的main方法,暴露接口
③啓動dubbo-consumer工程中的main方法,調用接口實現本身業務。
後續將會 繼續把集成的知識點 和 其餘的知識點補全(在web端查看暴露的服務而且設置)