(17) Building a dubbo distributed platform - creating a dubbo service producer

In the previous chapters, we defined the interface of the dubbo service. Next, for the implementation of the interface, we need to define the producer of the service, and the producer of the service will eventually be registered in the registry.

1. Create an ant-bookmark-service project (dubbo service producer), where the pom.xml file is defined as follows:

<?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 My Favorites Dubbo Service Interface Provider</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 related packages-->
		<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>

		<!-- Jar start required by dubbo -->
		<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>
		<!-- jar needed by dubbo end -->

		<!-- Dubbo service interface (member) -->
		<dependency>
			<groupId>com.sml.sz</groupId>
			<artifactId>ant-member-facade</artifactId>
		</dependency>
		
		<!-- ant config begin -->
		<!-- System public configuration-->
		<dependency>
			<groupId>com.sml.sz</groupId>
			<artifactId>ant-config</artifactId>
		</dependency>
		<!-- ant config end public configuration -->
		
		<!-- ant framework begin -->
		<!-- System core framework package-->
		<dependency>
			<groupId>com.sml.sz</groupId>
			<artifactId>ant-framework</artifactId>
		</dependency>
		<!-- ant framework end -->

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

 2. Define the spring-dubbo-provider.xml configuration:

 

<?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">

	<!-- Provider Application Information-->
	<dubbo:application name="ant-service-bookmark" />

	<!-- Use the zookeeper registry to expose the service address -->
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

	<!-- Expose services on port 20801 using the dubbo protocol -->
	<dubbo:protocol name="dubbo" port="20801" />

	<!-- Monitoring center configuration, protocol="registry", indicating that the monitoring center address is found from the registry -->
	<!-- <dubbo:monitor protocol="registry" /> -->

	<!-- When a property of ProtocolConfig and ServiceConfig is not configured, use this default value-->
	<dubbo:provider timeout="30000" threadpool="fixed" threads="100" accepts="1000" />

	<!-- Implement service like local bean -->
	<!-- Declare the service interface that needs to be exposed -->
	<!-- My Collection Management-->
	<dubbo:service retries="0" interface="com.sml.sz.bookmark.service.BookmarkTagFacade" ref="bookmarkTagFacade" />
</beans>  

 

3. Define BookmarkTagFacadeImpl.java, which is the implementation class of the dubbo service interface (which defines basic additions, deletions, changes, and checks):

/**
 * My Favorites Link Management 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. Define dao: BookmarkDao.java, BookmarkTagDao.java, as follows:

/**
 * My favorite link management DAO interface
 * @author ant
 * @version 2016-01-30
 */
@MyBatisDao
public interface BookmarkTagDao extends CrudDao<BookmarkTag> {
	
}

/**
 * My favorite link management DAO interface
 * @author ant
 * @version 2016-01-30
 */
@MyBatisDao
public interface BookmarkDao extends CrudDao<Bookmark> {
	
}

 5. Define the dao.xml file: BookmarkTagDao.xml, as follows:

<?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>

 

It ends here!

Welcome everyone to learn "Building a Dubbo Distributed Platform" with me, and I hope you will continue to pay attention to the following articles!

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326161634&siteId=291194637