用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

公园    -  云代码空间

——

(十七) 构建dubbo分布式平台-创建dubbo服务生产者

2018-03-15|802阅||

摘要:(十七) 构建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>

分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨
更多详细源码参考来源


顶 1踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: 公园
    • 等级: 初级设计师
    • 积分: 2760
    • 代码: 0 个
    • 文章: 89 篇
    • 随想: 0 条
    • 访问: 10 次
    • 关注

    人气代码

      标签

      MVC(9)教程(4)Spring(5)Redis分布式缓存(1)spring,spr(1)mvc,web开发,(1)spring,spr(1)mvc,web开发,(1)Zookeeper集(1)restful,(2)kafka,(2)shiro,Spri(1)MVC,mybati(1)跟我学习dubbo,(1)J2ee分布式架构,(1)shiro(1)跟我学习dubbo-(1)跟我学习dubbo-(1)spring+spr(1)Spring4+Sp(1)DUBBO与ZOOK(1)【分享】微服务分布式(1)Springmvc+(1)JEESZ(1)RestFul服务介(1)J2EE分布式架构(3)dubbo+spri(1)基于redis分布式(1)SSM框架Sprin(1)分布式缓存Redis(1)Centos下单节点(1)JEESZ分布式框架(1)JEESZ分布式框架(1)分布式架构sprin(1)+mybatis(8)+shiro+(3)Activiti(3)分布式服务:spri(1)+(11)Dubbo+Zook(1)JEESZ-kafk(1)JEESZ-Zook(1)FastDFS安装、(1)FastDFS分布式(1)分布式服务:spri(1)Dubbo+Zook(1)分布式架构sprin(1)FastDFS分布式(1)FastDFS安装、(1)Maven启用代理访(1)如何从Maven远程(1)Maven安装配置(1)Maven本地资源库(1)使用Maven创建J(1)使用Maven创建W(1)Maven(2)POM(1)构建生命周期(1)SSM框架——详细整(1)JavaEE的13种(1)使用Maven构建和(1)Maven存储库(1)(一)构建dubbo(1)(二)构建dubbo(1)(三)构建dubbo(1)分布式框架简介SSM(1)springmvc+(1)Maven快照(1)Maven项目模板(1)Maven构建自动化(1)dubbo(4)springmvc(4)mybatis(2)java企业架构(1)SSM框架——Spr(1)分布式服务:spri(1)Dubbo+Zook(1)分布式服务:spri(1)Dubbo+Zook(1)springmvc+(1)分布式架构sprin(1)mvc配置(2)dbcp数据源+jd(1)详细介绍(1)(十三)(1)(十四)(1)(十五)(1)MVC原理(1)入门示例讲解(1)(十六)(1)(十七)(1)Springmvc+(1)j2ee分布式架构核(1)【分享】微服务分布式(1)Springmvc+(1)dbcp数据源+jd(1)DUBBO+SPRI(1)JEESZ分布式系统(1)构建dubbo分布式(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)构建dubbo分布式(1)Springmvc+(1)构建springmv(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)跟我学习dubbo-(1)构建dubbo分布式(1)构建dubbo分布式(1)【企业级框架整合】S(1)构建dubbo分布式(1)【推荐】微服务大型分(1)springmvc整(1)【分享】微服务分布式(1)Springmvc+(1)SpringBoot(4)企业级(4)(十七)上传文件(1)(十四)在sprin(1)(十五)Spring(1)(十六)用restT(1)

      站长推荐