(十七) 构建dubbo分布式平台-创建dubbo服务生产者
在前面的章节中,我们定义了dubbo服务的接口,接下来针对于接口的实现,我们要定义服务的生产者,服务生产者最终会被注册到注册中心上。
1. 创建ant-bookmark-service项目(dubbo服务生产者),其中pom.xml文件定义如下:
<?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配置:
<?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服务接口的实现类(里面定义了基础的增删改查):
/** * 我的收藏链接管理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 ,如下:
/** * 我的收藏链接管理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,如下:
<?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分布式平台》,希望大家持续关注后面的文章!
相关推荐
ATenhong 2020-10-15
supperme 2020-09-08
doctorvian 2020-08-02
aNian 2020-08-01
kongjunlongaa 2020-06-29
Fightingxr 2020-06-26
whileinsist 2020-06-24
doctorvian 2020-06-16
XuNeely 2020-06-16
wangyangsoftware 2020-06-16
大步流星 2020-06-16
aNian 2020-06-16
gaoyongstone 2020-06-16
MartellJenkins 2020-06-11
范群松 2020-06-11
Fightingxr 2020-06-08
XuNeely 2020-06-07
大步流星 2020-06-05