(十七) 構建dubbo分佈式平臺-建立dubbo服務生產者

在前面的章節中,咱們定義了dubbo服務的接口,接下來針對於接口的實現,咱們要定義服務的生產者,服務生產者最終會被註冊到註冊中心上。java

1. 建立ant-bookmark-service項目(dubbo服務生產者),其中pom.xml文件定義以下:mysql

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.sml.sz</groupId>
		<artifactId>ant-project</artifactId>
		<version>1.0.0</version>
	</parent>
	<artifactId>ant-member-service</artifactId>
	<packaging>war</packaging>
	<name>ant-member-service Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<description>ant的個人收藏Dubbo服務接口提供者</description>
	
	<dependencies>
		<!-- alibaba druid connection pool -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid.version}</version>
		</dependency>

		<!-- jdbc driver -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.driver.version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc14</artifactId>
			<version>${oracle.driver.version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>net.sourceforge.jtds</groupId>
			<artifactId>jtds</artifactId>
			<version>${mssql.driver.version}</version>
			<scope>runtime</scope>
		</dependency>

		<!-- AOP相關包 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.7.4</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.4</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>3.1</version>
		</dependency>

		<!-- dubbo 須要的jar start -->
		<dependency>
			<groupId>org.jboss.netty</groupId>
			<artifactId>netty</artifactId>
			<version>3.2.5.Final</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- zookeeper jar -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.5</version>
		</dependency>

		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.3</version>
		</dependency>
		<!-- dubbo 須要的jar end -->

		<!-- Dubbo服務接口(member) -->
		<dependency>
			<groupId>com.sml.sz</groupId>
			<artifactId>ant-member-facade</artifactId>
		</dependency>
		
		<!-- ant config begin -->
		<!--  系統公共配置 -->
		<dependency>
			<groupId>com.sml.sz</groupId>
			<artifactId>ant-config</artifactId>
		</dependency> 
		<!-- ant config end 公共配置 -->
		
		<!-- ant framework begin -->
		<!--  系統核心框架包 -->
		<dependency>
			<groupId>com.sml.sz</groupId>
			<artifactId>ant-framework</artifactId>
		</dependency>
		<!-- ant framework end -->

	</dependencies>
	<build>
		<finalName>ant-member-service</finalName>
	</build>
</project>

 2. 定義spring-dubbo-provider.xml配置:spring

<?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="ant-service-bookmark" />

	<!-- 使用zookeeper註冊中心暴露服務地址 -->
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

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

	<!-- 監控中心配置,protocol="registry",表示從註冊中心發現監控中心地址 -->
	<!-- <dubbo:monitor protocol="registry" /> -->

	<!-- 當ProtocolConfig和ServiceConfig某屬性沒有配置時,採用此缺省值 -->
	<dubbo:provider timeout="30000" threadpool="fixed" threads="100" accepts="1000" />

	<!-- 和本地bean同樣實現服務 -->
	<!-- 聲明須要暴露的服務接口 -->
	<!-- 個人收藏管理 -->
	<dubbo:service retries="0" interface="com.sml.sz.bookmark.service.BookmarkTagFacade" ref="bookmarkTagFacade" />
</beans>

3. 定義BookmarkTagFacadeImpl.java,這個是dubbo服務接口的實現類(裏面定義了基礎的增刪改查):sql

/**
 * 個人收藏連接管理Service
 * @author ant
 * @version 2016-01-30
 */
@Component("bookmarkTagFacade")
@Transactional(readOnly = true)
public class BookmarkTagFacadeImpl extends CrudService<BookmarkTagDao, BookmarkTag> implements BookmarkTagFacade{

	@Autowired
	private BookmarkDao bookmarkDao;
	
	public BookmarkTag get(String id) {
		BookmarkTag bookmarkTag = super.get(id);
		bookmarkTag.setBookmarkList(bookmarkDao.findList(new Bookmark(bookmarkTag)));
		return bookmarkTag;
	}
	
	public List<BookmarkTag> findList(BookmarkTag bookmarkTag) {
		return super.findList(bookmarkTag);
	}
	
	public Page<BookmarkTag> findPage(Page<BookmarkTag> page, BookmarkTag bookmarkTag) {
		User user = UserUtils.getUser();
		System.out.println(user.getId());
		return super.findPage(page, bookmarkTag);
	}
	
	
	@Transactional(readOnly = false)
	public void save(BookmarkTag bookmarkTag) {
		super.save(bookmarkTag);
		for (Bookmark bookmark : bookmarkTag.getBookmarkList()){
			bookmark.setCreateBy(bookmarkTag.getCreateBy());
			bookmark.setUpdateBy(bookmarkTag.getUpdateBy());
			if (bookmark.getId() == null){
				continue;
			}
			if (Bookmark.DEL_FLAG_NORMAL.equals(bookmark.getDelFlag())){
				if (StringUtils.isBlank(bookmark.getId())){
					bookmark.setTagId(bookmarkTag.getId());
					bookmark.preInsert();
					bookmarkDao.insert(bookmark);
				}else{
					bookmark.preUpdate();
					bookmarkDao.update(bookmark);
				}
			}else{
				bookmarkDao.delete(bookmark);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BookmarkTag bookmarkTag) {
		super.delete(bookmarkTag);
		bookmarkDao.delete(new Bookmark(bookmarkTag));
	}
	
}

4. 定義dao:BookmarkDao.java、BookmarkTagDao.java ,以下:apache

/**
 * 個人收藏連接管理DAO接口
 * @author ant
 * @version 2016-01-30
 */
@MyBatisDao
public interface BookmarkTagDao extends CrudDao<BookmarkTag> {
	
}

/**
 * 個人收藏連接管理DAO接口
 * @author ant
 * @version 2016-01-30
 */
@MyBatisDao
public interface BookmarkDao extends CrudDao<Bookmark> {
	
}

 5. 定義dao.xml文件:BookmarkTagDao.xml,以下:mybatis

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sml.sz.bookmark.dao.BookmarkTagDao">
    
	<sql id="bookmarkTagColumns">
		a.id AS "id",
		a.bookmarktagname AS "bookmarktagname",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag"
	</sql>
    
	<select id="get" resultType="BookmarkTag">
		SELECT 
			<include refid="bookmarkTagColumns"/>
		FROM t_bookmark_tag a
		<include refid="bookmarkTagJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="BookmarkTag">
		SELECT 
			<include refid="bookmarkTagColumns"/>
		FROM t_bookmark_tag a
		<include refid="bookmarkTagJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="BookmarkTag">
		SELECT 
			<include refid="bookmarkTagColumns"/>
		FROM t_bookmark_tag a
		<include refid="bookmarkTagJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO t_bookmark_tag(
			id,
			bookmarktagname,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{bookmarktagname},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE t_bookmark_tag SET 	
			bookmarktagname = #{bookmarktagname},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	<update id="delete">
		delete from t_bookmark_tag WHERE id = #{id}
	</update>
	
</mapper>

歡迎你們跟我一塊兒學習《構建dubbo分佈式平臺》,但願你們持續關注後面的文章!oracle

相關文章
相關標籤/搜索