1.安装zookeeper 并启动
下载并解压 zookeeper-3.4.11.tar.gz
1.复制zoo_sample.cfg 到 zoo.cfg 编辑
2.安装rabbitmq 并启动
3.项目构建
3.1.api 接口项目
3.2.服务端工程
3.3.消费端工程
4 dubbo-demo-api
4.1pom.xml 依赖
junit
junit
3.8.1
test
com.alibaba
dubbo
2.8.4
spring-expression
org.springframework
spring-beans
org.springframework
spring-aop
org.springframework
org.springframework
spring-core
4.2.4.RELEASE
org.jboss.resteasy
resteasy-jaxrs
3.0.7.Final
org.jboss.resteasy
resteasy-client
3.0.7.Final
javax.validation
validation-api
1.0.0.GA
org.jboss.resteasy
resteasy-jackson-provider
3.0.7.Final
org.jboss.resteasy
resteasy-jaxb-provider
3.0.7.Final
org.jboss.resteasy
resteasy-netty
3.0.7.Final
org.jboss.resteasy
resteasy-jdk-http
3.0.7.Final
org.apache.tomcat.embed
tomcat-embed-core
8.0.11
org.apache.tomcat.embed
tomcat-embed-logging-juli
8.0.11
com.alibaba
fastjson
1.2.5
javax.ws.rs
javax.ws.rs-api
2.0
dubbo-demo-api
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
UTF-8
4.2服务接口类
package com.service.soa.api.query;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
@Path("query")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ContentType.APPLICATION_JSON_UTF_8 })
public interface QueryUserServiceApi {
@POST
@Path("test")
public String test();
}
******************************************************************************
package com.service.soa.api.register;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.service.soa.bean.EmailRequestBean;
import com.service.soa.bean.EmailResponseBean;
@Path("register")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ContentType.APPLICATION_JSON_UTF_8 })
public interface RegisterServiceApi {
/**
*
* @Title: queryOrderInfo
* @Description: TODO(注册)
* @param @param emailRequestBean
* @param @return 参数
* @return String 返回类型
* @throws
*/
@POST
@Path("sendMessage")
public EmailResponseBean sendMessage(EmailRequestBean emailRequestBean);
@GET
@Path("greeting")
public String sayHello();
}
5 RabbitMqS 服务提供项目 相关 配置及类
5.1 pom.xml依赖
4.0.0
com.java.rabbit
RabbitMqS
war
0.0.1-SNAPSHOT
RabbitMqS Maven Webapp
http://maven.apache.org
UTF-8
2.8.4
3.2.8
4.3.2.RELEASE
5.1.39
4.12
0.10
4.0.1
1.2.46
1.2.17
1.7.25
3.4
4.0.35.Final
junit
junit
4.12
test
com.service.api
dubbo-demo-api
0.0.1-SNAPSHOT
org.springframework
spring-test
4.2.5.RELEASE
org.springframework
spring-context
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-aspects
${spring.version}
com.fasterxml.jackson.core
jackson-annotations
2.5.0
com.fasterxml.jackson.core
jackson-core
2.5.0
com.fasterxml.jackson.core
jackson-databind
2.5.0
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-orm
4.0.2.RELEASE
org.springframework.security
spring-security-config
4.0.2.RELEASE
org.springframework.security
spring-security-web
4.0.2.RELEASE
org.springframework.security
spring-security-core
4.0.2.RELEASE
org.springframework
spring-test
4.1.6.RELEASE
test
org.aspectj
aspectjweaver
1.8.7
org.apache.cxf
cxf-rt-frontend-jaxrs
3.1.2
org.codehaus.jackson
jackson-jaxrs
1.9.2
commons-net
commons-net
1.4.1
org.apache.commons
commons-lang3
3.0
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet.jsp
jsp-api
2.1
provided
javax.servlet
jstl
1.2
redis.clients
jedis
2.9.0
org.springframework.data
spring-data-redis
1.7.2.RELEASE
org.springframework.amqp
spring-rabbit
2.0.2.RELEASE
javax.mail
mail
1.4.7
com.alibaba
dubbo
${dubbox.version}
org.springframework
spring-beans
org.springframework
spring-aop
org.springframework
spring-expression
org.springframework
spring-core
org.springframework
spring-context
org.springframework
spring-web
slf4j-api
org.slf4j
commons-logging
commons-logging
javax.servlet
javax.servlet-api
com.google.guava
guava
org.apache.httpcomponents
httpclient
org.apache.httpcomponents
httpcore
org.apache.curator
curator-framework
org.apache.curator
curator-client
commons-codec
commons-codec
log4j
log4j
org.slf4j
slf4j-log4j12
com.101tec
zkclient
${zkclient.version}
org.apache.curator
curator-framework
${curator-framework.version}
com.alibaba
fastjson
${fastjson.version}
io.netty
netty-all
${netty-all.version}
javax.ws.rs
javax.ws.rs-api
2.0
org.jboss.resteasy
resteasy-jaxrs
3.0.7.Final
org.jboss.resteasy
resteasy-client
3.0.7.Final
javax.validation
validation-api
1.0.0.GA
org.jboss.resteasy
resteasy-jackson-provider
3.0.7.Final
org.jboss.resteasy
resteasy-jaxb-provider
3.0.7.Final
org.jboss.resteasy
resteasy-netty
3.0.7.Final
org.jboss.resteasy
resteasy-jdk-http
3.0.7.Final
org.apache.tomcat.embed
tomcat-embed-core
8.5.40
org.apache.tomcat.embed
tomcat-embed-logging-juli
8.0.11
com.alibaba
druid
1.0.11
org.mybatis
mybatis-spring
1.3.0
org.mybatis
mybatis
3.4.1
mysql
mysql-connector-java
5.1.37
org.apache.shiro
shiro-core
1.3.0
org.apache.shiro
shiro-spring
1.3.0
org.apache.shiro
shiro-web
1.3.0
RabbitMqS
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
UTF-8
5.2 application-context.xml
classpath:properties/config.properties
classpath:properties/dubbo.properties
classpath:properties/spring-email.properties
classpath:properties/dbconfig.properties
5.3application-Email.xml
true
25000
true
5.4application-provider-dubbo.xml
5.5application-rabbitMq.xml
5.6jdbc-Context.xml
5.7mybatis-config.xml
5.8spring-dubbo-service-api.xml
5.9spring-mvc.xml
5.10 配置文件 *.properties
#=============================mysql===========================================#
jdbc.type=mysql
jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://192.168.80.75:3306/isharedb?useUnicode=true&characterEncoding=utf-8
jdbc.url=jdbc:mysql://localhost:3306/dumz_db?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
#监控统计拦截
jdbc.filters=stat
#最大并发连接数
jdbc.maxActive=20
#初始化连接数量
jdbc.initialSize=1
#连接等待超时
jdbc.maxWait=60000
#最小空闲连接数
jdbc.minIdle=10
#检测需要关闭的空闲连接
jdbc.timeBetweenEvictionRunsMillis=60000
#连接在池中最小生存的时间
jdbc.minEvictableIdleTimeMillis=300000
#连接是否有效的sql
jdbc.validationQuery=SELECT 'x'
#并且保证安全性
jdbc.testWhileIdle=true
#申请连接时执行validationQuery检测连接是否有效
jdbc.testOnBorrow=false
#归还连接时执行validationQuery检测连接是否有效
jdbc.testOnReturn=false
#=================================redis================================================#
#redis settings
redis.keyPrefix=users
redis.host=127.0.0.1
redis.port=6379
#超时时间
redis.timeout=2000
#注意,如果没有password,此处不设置值,但这一项要保留
redis.password=123456
#数据库索引号 index 用数字值指定,以 0 作为起始索引值(默认使用 0 号数据库)
redis.db.index=0
######################################################################################
===========================dubbo====================================================
#dubbo应用名称
dubbo.provider.name=soa-RegisterProvider
#dubbo服务端口
dubbo.provider.port=8890
#dubbo zookeeper注册地址
dubbo.registry=zookeeper://127.0.0.1:2181
#rest 服务端口
dubbo.provider.rest=8087
######################################################################################
============================= Email ==============================================
email.host=smtp.qq.com
#发件人邮箱登录名
[email protected]
#发件人密码
email.password=uztwtgvafngvechg
executor.corePoolSize=1
executor.maxPoolSize=2
executor.queueCapacity=20
executor.keepAliveSeconds=5000
5.11 服务接口实现类
package com.service.soa.access.register;
import java.util.Date;
import java.util.UUID;
import javax.annotation.Resource;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.service.soa.api.register.RegisterServiceApi;
import com.service.soa.bean.EmailRequestBean;
import com.service.soa.bean.EmailResponseBean;
import com.service.soa.dao.UserDao;
import com.service.soa.pojo.Email;
import com.service.soa.pojo.User;
import com.service.soa.service.SendEmailService;
import com.service.soa.util.RespUtils;
import com.service.soa.util.UidUtils;
/**
*
* @ClassName: RegisterAccess
* @Description: TODO(邮件通知)
* @author MAOJIAJIE
* @date 2019年4月20日
*
*/
@Service(value = "registerAccess")
public class RegisterAccess implements RegisterServiceApi {
private static Logger log = LoggerFactory.getLogger(RegisterAccess.class);
@Resource
private UserDao userDao;
@Resource
RabbitTemplate template;
// @Transactional(rollbackFor = Exception.class)
@Transactional
public EmailResponseBean sendMessage(EmailRequestBean emailRequestBean) {
// TODO Auto-generated method stub
Email email = new Email();
//收件人邮件地址
email.setToAddress(emailRequestBean.getToAddress());
//邮件主题
email.setText("站点测试邮箱");
email.setContent("注册成功!");
//入库
User user = new User();
user.setUserId(UidUtils.createUtil());
user.setUserName(emailRequestBean.getUname());
user.setCreateTime(new Date());
user.setUserStart("01");
user.setOlineStart("01");
user.setUserEmail(emailRequestBean.getToAddress());
user.setUserPsw(emailRequestBean.getUpassword());
//插入表中
userDao.insert(user);
//测试注解事物
// System.out.println(1 / 0);
try {
//发送到消息队列中
template.convertAndSend(email);
log.info("****************用户注册成功!");
} catch (AmqpException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
log.info("用户注册失败:原因【"+e.toString()+"】");
}
return RespUtils.createBaseResp("000", "测试成功!", email.getToAddress());
}
/**
* 测试接口
* Title: sayHello
* Description:
* @return
* @see com.service.soa.api.register.RegisterServiceApi#sayHello()
*/
public String sayHello() {
// TODO Auto-generated method stub
return "hello";
}
}
package com.service.soa.access.quey;
import org.springframework.stereotype.Service;
import com.service.soa.api.query.QueryUserServiceApi;
@Service(value = "userServiceAccess")
public class UserServiceAccess implements QueryUserServiceApi{
@Override
public String test() {
// TODO Auto-generated method stub
return "hellojava";
}
}
6.服务消费方工程 dumz-webFront
6.1 pom 依赖
4.0.0
com.service.soa.dumz
dumz-webFront
war
0.0.1-SNAPSHOT
dumz-webFront Maven Webapp
http://maven.apache.org
UTF-8
2.8.4
3.2.8
4.3.2.RELEASE
4.1.6.RELEASE
5.1.39
4.12
0.10
4.0.1
1.2.46
1.2.17
1.7.25
3.4
4.0.35.Final
1.0.2-SNAPSHOT
junit
junit
4.12
test
com.service.api
dubbo-demo-api
0.0.1-SNAPSHOT
javax.servlet
javax.servlet-api
4.0.0-b01
javax.servlet.jsp
jsp-api
2.2.1-b03
javax.servlet
jstl
1.2
log4j
log4j
1.2.17
org.slf4j
slf4j-log4j12
1.7.21
com.google.code.gson
gson
2.7
net.sf.json-lib
json-lib
2.4
jdk15
org.springframework
spring-context
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-aspects
${spring.version}
com.fasterxml.jackson.core
jackson-annotations
2.5.0
com.fasterxml.jackson.core
jackson-core
2.5.0
com.fasterxml.jackson.core
jackson-databind
2.5.0
org.springframework
spring-webmvc
${spring.version}
com.alibaba
druid
1.0.11
org.springframework
spring-orm
4.0.2.RELEASE
org.springframework.security
spring-security-config
4.0.2.RELEASE
org.springframework.security
spring-security-web
4.0.2.RELEASE
org.springframework.security
spring-security-core
4.0.2.RELEASE
com.alibaba
dubbo
2.8.4
org.springframework
spring
org.apache.zookeeper
zookeeper
3.4.11
org.slf4j
slf4j-log4j12
junit
junit
org.jboss.resteasy
resteasy-jaxrs
3.0.11.Final
org.jboss.resteasy
resteasy-client
3.0.11.Final
org.jboss.resteasy
resteasy-jaxrs
org.apache.curator
curator-framework
2.5.0
org.apache.curator
curator-client
org.apache.zookeeper
zookeeper
com.google.guava
guava
org.apache.curator
curator-client
2.5.0
com.google.guava
guava
io.netty
netty
log4j
log4j
org.slf4j
slf4j-api
org.slf4j
slf4j-log4j12
dumz-webFront
1.8
UTF-8
6.2 application-context.xml
classpath:config/properties/dubbo.properties
text/plain;charset=UTF-8
6.3dubbo-service-consumer.xml
6.4spring-mvc.xml
6.5 .properties 配置文件
================================================dubbo=================================
#dubbo zookeeper注册地址
dubbo.consumer.name = dumz-web
dubbo.registry=zookeeper://127.0.0.1:2181
dubbo.file=E:\\dubbo\\dubbo.cache
6.6 服务消费类
package com.umz.soa.controller.query;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.dubbo.config.annotation.Reference;
import com.service.soa.api.query.QueryUserServiceApi;
import com.service.soa.api.register.RegisterServiceApi;
import com.service.soa.bean.EmailRequestBean;
import com.service.soa.bean.EmailResponseBean;
import com.sun.swing.internal.plaf.metal.resources.metal;
import com.umz.soa.util.RespUtils;
@Controller
@RequestMapping("/query")
public class queryTestServiceController {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource
RegisterServiceApi registerServiceApi;
@Resource
QueryUserServiceApi queryUserServiceApi;
// @Resource
// MerChantUserRemote metal;
@RequestMapping(value = "/test.do",method=RequestMethod.GET)
@ResponseBody
public String test() {
logger.info("测试远程接口");
return queryUserServiceApi.test();
}
/**
* 首页
* @Title: indexPage
* @Description: TODO
* @return
* @return: String
*/
@RequestMapping(value = "/main.do", method = RequestMethod.GET)
public String indexPage() {
return "/pages/user/admin";
}
}
package com.umz.soa.controller.register;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.service.soa.api.query.QueryUserServiceApi;
import com.service.soa.api.register.RegisterServiceApi;
import com.service.soa.bean.EmailRequestBean;
import com.service.soa.bean.EmailResponseBean;
import com.umz.soa.util.RespUtils;
@Controller
public class RegisterServiceController {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource
RegisterServiceApi registerServiceApi;
@Resource
QueryUserServiceApi queryUserServiceApi;
@RequestMapping(value = "/register/sendEmail.do")
@ResponseBody
public EmailResponseBean register() {
logger.info("测试远程接口");
// String result= registerServiceApi.sayHello();
EmailRequestBean emailRequestBean = new EmailRequestBean();
emailRequestBean.setUname("测试用户4");
emailRequestBean.setUpassword("123456");
emailRequestBean.setToAddress("[email protected]");
registerServiceApi.sendMessage(emailRequestBean);
return RespUtils.createBaseResp("000", "测试成功!", emailRequestBean.getToAddress());
// System.out.println("远程调用的结果:"+emailResponseBean);
}
}
6.7 项目中相应类 dubbo-demo-api
package com.umz.soa.util;
import com.service.soa.bean.EmailResponseBean;
public class RespUtils {
/**
*
* @Title: createBaseResp
* @Description: TODO(邮件发送返回实体类)
* @param @param retcode
* @param @param retmsg
* @param @return 参数
* @return EmailResponseBean 返回类型
* @throws
*/
public static EmailResponseBean createBaseResp(String retcode ,String retmsg,String toAddress){
EmailResponseBean emailResponseBean = new EmailResponseBean();
emailResponseBean.setRetcode(retcode);
emailResponseBean.setRetmsg(retmsg);
emailResponseBean.setToAddress(toAddress);
return emailResponseBean;
}
}
package com.service.soa.bean;
import java.io.Serializable;
/**
*
* @ClassName: BaseRespBean
* @Description: TODO(返回标识)
* @author MAOJIAJIE
* @date 2019年4月20日
*
*/
public class BaseRespBean implements Serializable {
private static final long serialVersionUID = 8449818967081230166L;
private String retcode; // 响应码 000000:成功 其他:失败
private String retmsg; // 响应信息
public String getRetcode() {
return retcode;
}
public void setRetcode(String retcode) {
this.retcode = retcode;
}
public String getRetmsg() {
return retmsg;
}
public void setRetmsg(String retmsg) {
this.retmsg = retmsg;
}
public BaseRespBean(){
}
public BaseRespBean(String retcode,String retmsg){
this.retcode = retcode;
this.retmsg = retmsg;
}
}
package com.service.soa.bean;
import java.io.Serializable;
/**
*
* @ClassName: EmailRequestBean
* @Description: TODO(邮箱请求实体类)
* @author MAOJIAJIE
* @date 2019年4月19日
*
*/
public class EmailRequestBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String uname;//收件人性名
private String upassword;//收件人密码
private String uregdate; //注册时间
private Integer ustate;//状态
private String fromAddress; //发件人
private String toAddress; //收件人
private String text; //邮件标题
private String content; //邮件内容
private String copyPerson; //抄送人
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpassword() {
return upassword;
}
public void setUpassword(String upassword) {
this.upassword = upassword;
}
public String getUregdate() {
return uregdate;
}
public void setUregdate(String uregdate) {
this.uregdate = uregdate;
}
public Integer getUstate() {
return ustate;
}
public void setUstate(Integer ustate) {
this.ustate = ustate;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getCopyPerson() {
return copyPerson;
}
public void setCopyPerson(String copyPerson) {
this.copyPerson = copyPerson;
}
@Override
public String toString() {
return "EmailRequestBean [uname=" + uname + ", upassword=" + upassword + ", uregdate=" + uregdate + ", ustate="
+ ustate + ", fromAddress=" + fromAddress + ", toAddress=" + toAddress + ", text=" + text + ", content="
+ content + ", copyPerson=" + copyPerson + "]";
}
}
package com.service.soa.bean;
import com.sun.xml.bind.v2.schemagen.xmlschema.List;
/**
*
* @ClassName: EmailRequestBean
* @Description: TODO(邮箱返回实体类)
* @author MAOJIAJIE
* @date 2019年4月19日
*
*/
public class EmailResponseBean extends BaseRespBean {
private static final long serialVersionUID = 1L;
private String msg;
private String toAddress;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
@Override
public String toString() {
return "EmailResponseBean [msg=" + msg + ", toAddress=" + toAddress + "]";
}
}
7.测试
7.1启动服务项目 RabbitMqS
打开dubbo 管理界面 下面表示注册成功的两个服务接口
7.2运行项目 dumz-webFront 测试消费接口
7.3测试接口 浏览器中输入地址
7.4测试邮箱注册接口
服务方调用日志打印信息如下:
邮件通知成功