學習dubbo+zookeeper+spring中遇到的問題和解決辦法
阿新 • • 發佈:2019-02-20
我使用的macOS。
第一次建立MAVEN專案
在使用eclipse建立maven專案時,遇到了不能轉換Dynamic Web Module version 的問題 ,建立maven專案時預設是2.3,不讓轉成3.0(我使用的是jdk1.8)在網上試了很多辦法最後通過這篇部落格的辦法解決的http://blog.csdn.net/ahau10/article/details/51232406下載配置zookeeper
我的下載地址https://pan.baidu.com/s/1dEBdFUx 下載好後解壓 ,進入zookeeper-3.10.4/conf/zoo_sample.cfgtickTime:為心跳時間,為了確保連線存在的,以毫秒為單位,最小超時時間為兩個心跳時間
下載dubbo-admin.war 方便檢視管理
我的是jdk1.8的環境 這個我試過好用 https://pan.baidu.com/s/1miDN5WW 下載好後放到tomcat 的webapps下面 然後啟動tomcat 會自動解壓 之後就可以通過瀏覽器使用了賬號密碼都是root
建立的自己提供者的maven專案 pom檔案匯入
我是直接將提供者部署到tomcat上 所以在web.xml中新增<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dyb</groupId> <artifactId>firstdubbo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>firstdubbo Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.4.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.4.RELEASE</version> </dependency> </dependencies> <build> <finalName>firstdubbo</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--contextConfigLocation在 ContextLoaderListener類中的預設值是 /WEB-INF/applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>/WEB-INF/applicationContext.xml</param-value> -->
<param-value>classpath:spring/dubbo-provider.xml</param-value>
</context-param>
<display-name>Archetype Created Web Application</display-name>
</web-app>
定義自己要提供的介面 和 實現類
配置提供者的配置檔案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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 具體的實現bean -->
<!-- <bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" /> -->
<!-- 提供方應用資訊,用於計算依賴關係 -->
<dubbo:application name="dubboprovider" />
<!-- 使用multicast廣播註冊中心暴露服務地址 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper註冊中心暴露服務地址 即zookeeper的所在伺服器ip地址和埠號 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo協議在20880埠暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 宣告需要暴露的服務介面 -->
<bean id="firstService" class="com.my.service.impl.FirstServiceImpl"></bean>
<dubbo:service interface="com.my.service.FirstService"
ref="firstService" />
</beans>
提供者寫弄完了 然後部署到tomcat上
接下來是消費者
新建一個消費者的專案 pom匯入之前的提供者
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my</groupId>
<artifactId>dubboconsumer</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>dubboconsumer Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.dyb</groupId>
<artifactId>firstdubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>dubboconsumer</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
新增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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 消費方應用資訊,用於計算依賴關係 -->
<dubbo:application name="dubbocunsumer" />
<!-- 使用multicast廣播註冊中心暴露服務地址 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper註冊中心暴露服務地址 即zookeeper的所在伺服器ip地址和埠號 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo協議在20880埠暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--消費者引用-->
<dubbo:reference id="firstService" interface="com.my.service.FirstService"/>
</beans>
寫一個類來使用這個介面
public class MyConsumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
context.start();
try {
FirstService firstService = (FirstService) context.getBean("firstService");
firstService.test();
} catch (Exception e) {
}
}
}
執行後 成功呼叫了介面 好神奇
我這種直接匯入了提供者工程這種不好 應該講介面單獨打包比較好。
到這裡我的初級的spring+dubbo+zookeeper的整合就結束了。初學者,寫的不好,望見諒!