【dubbo】入門程式

00潤物無聲00發表於2017-07-23

  瞭解了理論知識,下面這個例子,使用dubbo實現系統間的遠端呼叫。


3大流程

1.windows安裝zookeeper

2.編寫後臺服務(服務提供者)並啟動

3.編寫呼叫服務(服務消費者),並測試


windows安裝zookeeper

1.解壓zookeeper3.4.8到本地資料夾G:\Java\zookeeper-3.4.8

2.修改conf資料夾中的zoo.example.cfg

  修改名稱為zoo.cfg

  修改zoo.cfg中dataDir為 dataDir=G:/Java/zookeeper-data 

  在本地必須手動建立該資料夾

3.執行bin目錄下的zkServer.cmd,啟動zookeeper.


編寫後臺服務(服務提供者)並啟動

  1.建立maven工程


  2.在pom.xml中加入依賴

    dubbo版本後來沒有更新,在依賴中排出Spring的傳遞依賴,使用固定版本整合。

<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.dynamic.dubbo</groupId>
	<artifactId>dubbo-b</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<!-- dubbo採用spring配置方式,所以需要匯入spring容器依賴 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.3.RELEASE</version>
		</dependency>


		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.4</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<!-- 排出傳遞spring依賴 -->
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!-- zookeeper的依賴 -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.3.3</version>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<port>8181</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>


  3.編寫基本服務介面,查詢

pojo:物件要遠端呼叫,在網路中傳輸,所以進行序列化

package com.dynamic.dubbo.pojo;
import java.io.Serializable;
public class User implements Serializable{

	//該物件唯一的序列化ID
	private static final long serialVersionUID = -7821136735047262187L;
	private Long id;
	private String username;
	private String password;
	private Integer age;
	
}

server

package com.dynamic.dubbo.service;
import java.util.List;
import com.dynamic.dubbo.pojo.User;
public interface UserService {
		//查詢所有使用者資料
	public List<User> queryAll();

}

serviceImpl

package com.dynamic.dubbo.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.dynamic.dubbo.pojo.User;
import com.dynamic.dubbo.service.UserService;

public class UserServiceImpl implements UserService {

	/**
	 * 實現查詢
	 */
	public List<User> queryAll() {

		List<User> userlist = new ArrayList<User>();

		for (int i = 0; i < 10; i++) {
			User user = new User();
			user.setAge(10 + i);
			user.setId(Long.valueOf(i + 1));
			user.setUsername("username_" + i);
			user.setPassword("123456");
			userlist.add(user);
		}
		return userlist;
	}
}

  4.配置dubbo的配置檔案

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 提供方應用資訊,用於計算依賴關係 -->
	<dubbo:application name="dubbo-b-server" />

	<!-- 這裡使用的註冊中心是zookeeper -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181"
		client="zkclient" />

	<!-- 用dubbo協議在20880埠暴露服務 -->
	<dubbo:protocol name="dubbo" port="20880" />

	<!-- 將該介面暴露到dubbo中 -->
	<dubbo:service interface="com.dynamic.dubbo.service.UserService"
		ref="userServiceImpl" />

	<!-- 將具體的實現類加入到Spring的容器中 -->
	<bean id="userServiceImpl" class="com.dynamic.dubbo.service.impl.UserServiceImpl" />

</beans>

  5.web.xml中啟動

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<display-name>dubbo-b</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:dubbo/dubbo-*.xml</param-value>
	</context-param>

	<!--Spring的ApplicationContext 載入 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


編寫服務呼叫(服務消費者)

  1.建立maven工程

  2.在pom.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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dynamic.dubbo</groupId>
  <artifactId>dubbo-a</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  <!-- dubbo採用Spring配置方式,所以需要匯入Spring容器依賴 -->
  
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-webmvc</artifactId>
  		<version>4.1.3.RELEASE</version>
  	</dependency>
  	
  	<dependency>
  		<groupId>org.slf4j</groupId>
  		<artifactId>slf4j-log4j12</artifactId>
  		<version>1.6.4</version>
  	</dependency>
  	
  	<dependency>
  		<groupId>com.alibaba</groupId>
  		<artifactId>dubbo</artifactId>
  		<version>2.5.3</version>
  		<exclusions>
  			<exclusion>
  			<!-- 排出傳遞spring依賴 -->
  				<groupId>org.springframework</groupId>
  				<artifactId>spring</artifactId>
  			</exclusion>
  		</exclusions>
  	</dependency>
  	
  	
  	<dependency>
  		<groupId>org.apache.zookeeper</groupId>
  		<artifactId>zookeeper</artifactId>
  		<version>3.3.3</version>
  	</dependency>
  	
  	<dependency>
  		<groupId>com.github.sgroschupf</groupId>
  		<artifactId>zkclient</artifactId>
  		<version>0.1</version>
  	</dependency>
  	
  	<dependency>
  		<groupId>junit</groupId>
 		<artifactId>junit</artifactId>
 		<version>4.10</version>
 		<scope>test</scope>
  	</dependency>
  </dependencies>
  
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.tomcat.maven</groupId>
  			<artifactId>tomcat7-maven-plugin</artifactId>
  			<version>2.2</version>
  			<configuration>
  				<port>8182</port>
  				<path>/</path>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
  
  	
</project>

  3.編寫介面

  pojo

package com.dynamic.dubbo.pojo;
import java.io.Serializable;
public class User implements Serializable{

	//該物件唯一的序列化ID
	private static final long serialVersionUID = -7821136735047262187L;
	private Long id;
	private String username;
	private String password;
	private Integer age;
}

  userService

package com.dynamic.dubbo.service;
import java.util.List;
import com.dynamic.dubbo.pojo.User;
public interface UserService {
		//查詢所有使用者資料
	public List<User> queryAll();

}

 測試類

package com.dynamic.dubbo.test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dynamic.dubbo.pojo.User;
import com.dynamic.dubbo.service.UserService;

public class UserServiceTest {

	private UserService userService;

	@Before
	public void setUp() throws Exception {
		// 完成Service的初始化
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:dubbo/dubbo-*.xml");
		this.userService = applicationContext.getBean(UserService.class);
	}

	@Test
	public void queryAll() throws Exception {
		List<User> list = userService.queryAll();
		for (User user : list) {
			System.out.println("使用者名稱----------->" + user.getUsername());
		}
	}
}

  4.配置dubbo的配置檔案

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 提供方應用資訊,用於計算依賴關係 -->
	<dubbo:application name="dubbo-a-consumer" />

	<!-- 這裡使用的註冊中心是zookeeper -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181"
		client="zkclient" />

	<dubbo:reference id="userService"   interface="com.dynamic.dubbo.service.UserService"></dubbo:reference>
</beans>

結論:

  dubbo與Spring整合,整個入門程式的核心部分在於服務提供者和服務消費者的配置檔案的內容。

  服務提供者:

    1.<dubbo:application>提供方應用資訊

    2.<dubbo:registry>註冊中心

    3.<dubbo:protocol>dubbo協議在20880埠暴露服務

    4.<dubbo:service>介面暴露在dubbo中

    5.<bean>實現類再Spring容器中管理

  服務消費者

    1.<dubbo:application>消費方應用資訊

    2.<dubbo:registry>註冊中心

    3.<dubbo:reference>引用服務配置,用於建立一個遠端服務代理,一個引用可以指向多個註冊中心



相關文章