02-信贷路由项目rose框架拆分dubbo
项目架构和 rose 框架搭建见 https://www.cnblogs.com/yuanpeng-java/p/9835984.html
1.dubbo 框架架构及组成
2.注册中心安装及配置
https://www.cnblogs.com/yuanpeng-java/p/9489335.html
3.服务提供方的配置
新建 maven 项目tyrRouter-order-provider
配置 pom.xml 文件(添加 dubbo 依赖和 zookeeper 依赖,数据库文件)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.tcredit</groupId> <artifactId>tyrRouter</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>tyrRouter-order-provider</artifactId> <properties> <jdk.version>1.7</jdk.version> </properties> <!-- 添加依赖 --> <dependencies> <!--rose--> <dependency> <groupId>net.paoding</groupId> <artifactId>rose</artifactId> </dependency> <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <!-- zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> <!-- zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <!--数据库 --> <dependency> <groupId>com.wzzx</groupId> <artifactId>wzzx-datasource4jade</artifactId> </dependency> <dependency> <groupId>com.tcredit</groupId> <artifactId>tyrRouter-order-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <profiles> <!-- 开发,默认激活 --> <profile> <id>dev</id> <properties> <env>dev</env> <env-s>-dev</env-s> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>mock</id> <properties> <env>mock</env> <env-s>-mock</env-s> </properties> </profile> <profile> <id>test</id> <properties> <env>test</env> <env-s>-test</env-s> </properties> </profile> <profile> <id>uat</id> <properties> <env>uat</env> <env-s>-uat</env-s> </properties> </profile> <profile> <id>prod</id> <properties> <env>prod</env> <env-s></env-s> </properties> </profile> </profiles> <build> <finalName>${project.artifactId}-${project.version}</finalName> <defaultGoal>compile</defaultGoal> <filters> <filter>${project.basedir}/filters/${env}/config.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <!--需要过滤的文件--> <include>*.*</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> <exclude>*.*</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> <encoding>UTF-8</encoding> <compilerArguments> <verbose /> <bootclasspath>${java.home}/lib/rt.jar:${java.home}/lib/jce.jar</bootclasspath> </compilerArguments> </configuration> </plugin> </plugins> </build> </project>
配置变量文件(filter.dev.conf.properties)
#mysql服务
tyr_host=172.19.160.18
tyr_r1_host=172.19.160.17
tyr_r2_host=172.19.160.19
database=tyr_dev_test
#日志
logPath=/Users/mryuan/work/log/tyr-api-provider/stdout.log
#内部dubbo服务地址
dubbo.zookeeper.url=172.19.73.16:2181
zookeeper.hosts=172.19.73.16:2181
配置数据库文件(datasource.xml 文件)
#公司环境数据库配置
auth:
admin : admin
#######################################
#mysql的默认连接配置
default:
type: mysql
port: 3306
initialSize: 1
maxActive: 20
maxWait: 10000
filters:
- stat
auth: root
#######################################
server:
#mysql服务
tyr:
host: ${tyr_host}
tyr_r1:
host: ${tyr_r1_host}
tyr_r2:
host: ${tyr_r2_host}
datasource:
tyr:
server: tyr
database: ${database}
auth: admin
#auth: worker
配置 applicationContext-order-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:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.tcredit.order.provider" /> <bean id="dubboConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:dubbo.properties</value> </property> </bean> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="tyrRouter-order-provider" /> <dubbo:registry protocol="zookeeper" address="${dubbo.zookeeper}" id="defRegistry" default="true" /> <!-- 用dubbo协议在20889端口暴露服务 --> <dubbo:protocol name="dubbo" port="${dubbo.port}" threads="500" accepts="1000" /> <!-- 自动从注册中心发现监控服务 --> <dubbo:monitor protocol="registry" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.tcredit.order.service.CashLoanOrderService" ref="cashLoanOrderService" timeout="${dubbo.timeout}" version="1.0.0" retries="0" /> <bean id="cashLoanOrderService" class="com.tcredit.order.provider.service.impl.CashLoanOrderServiceImpl" /> </beans>
配置dubbo文件(dubbo.properties)
#dubbo配置
dubbo.zookeeper=${dubbo.zookeeper.url}
dubbo.port=20880
dubbo.timeout=6000000
配置日志文件(log4j.properties)
#日志
log4j.rootCategory=INFO,console,stdout
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%t] %C.%M(%L) | %m%n
log4j.appender.stdout=org.apache.log4j.DailyRollingFileAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%t] %C.%M(%L) | %m%n
log4j.appender.stdout.encoding=UTF-8
log4j.appender.stdout.File=${logPath}
log4j.appender.stdout.DatePattern='.'yyyy-MM-dd
配置环境变量类文件(settings.properties)
#数据库
jade.datasource=datasource.yaml
配置启动类
package com.tcredit.order.provider;
import net.paoding.rose.scanning.context.RoseAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Provider {
private final static Logger logger = LoggerFactory.getLogger(Provider.class);
private static volatile boolean running = true;
public static void main(String[] args) throws Exception {
final RoseAppContext ctx = new RoseAppContext();
ctx.start();
logger.info("dubbo provider started..");
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
ctx.stop();
ctx.close();
logger.info("dubbo provider stopped!");
} catch (Throwable t) {
logger.error(t.getMessage(), t);
}
synchronized (Provider.class) {
running = false;
Provider.class.notify();
}
}
});
synchronized (Provider.class) {
while (running) {
try {
Provider.class.wait();
} catch (Throwable e) {
}
}
}
}
}
编写测试类,启动注册中心和监控中心,启动服务,在监控中心查看是否有服务注册
package com.tcredit.order.provider.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tcredit.order.entity.CsrInfoEntity;
import com.tcredit.order.provider.dao.CsrInfoDAO;
import com.tcredit.order.provider.dao.TestDAO;
import com.tcredit.order.service.TestService;
@Service
public class TestServcieImpl implements TestService {
@Autowired
private TestDAO testDAO;
@Autowired
private CsrInfoDAO csrInfoDAO;
@Override
public void delAllData(Integer csrId) {
testDAO.delAllData(csrId);
}
@Override
public CsrInfoEntity getCsrInfoByTel(String tel) {
//根据手机号查询单个对象
CsrInfoEntity csrInfoEntity = new CsrInfoEntity();
csrInfoEntity.setUserMp(tel);
return csrInfoDAO.getCsrInfo(csrInfoEntity);
}
}
4.服务消费方配置
新建 maven 项目, tyrRouter-common-client
配置 pom.xml 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.tcredit</groupId> <artifactId>tyrRouter</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>tyrRouter-common-client</artifactId> <dependencies> <dependency> <groupId>com.yeepay.credit</groupId> <artifactId>dubbo-client-registry</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <!-- zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> <dependency> <groupId>com.tcredit</groupId> <artifactId>tyrRouter-order-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
编写消费者
package com.tcredit.client; import org.springframework.stereotype.Component; import com.tcredit.order.entity.CsrInfoEntity; import com.tcredit.order.service.TestService; import com.yeepay.credit.dubbo.client.registry.AbstractDubboClient; @Component public class TestClient extends AbstractDubboClient<TestService> { @Override public Class<?> dubboServiceClass() { return TestService.class; } /** * 根据手机号,获取用户信息 * @author 袁朋 * @date 2018年10月31日 上午11:07:30 */ public CsrInfoEntity getCsrInfoByTel(String tel) { return dubboService.getCsrInfoByTel(tel); } /** * 根据 csrId 清空数据 * @author 袁朋 * @date 2018年10月31日 上午11:12:54 */ public void delAllData(Integer csrId) { dubboService.delAllData(csrId); } }
改造tyrRouter-log-web 中 Rose 框架
去掉项目中数据库相关配置,pom.xml 文件中引入tyrRouter-common-client,改造扫描包路径,使其可以扫描到 client 为 com.tcredit
setting.properties文件中增加 dubbo 相关配置
#dubbo 注册中心地址
dubbo.zookeeper.url=172.19.73.16:2181
编写测试类,启动对象,测试
package com.tcredit.log.web.controllers;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.tcredit.client.TestClient;
import com.tcredit.log.web.util.AESUtil;
import com.tcredit.log.web.util.ClassUtils;
import com.tcredit.log.web.util.JsonResolver;
import com.tcredit.log.web.util.ObjectUtils;
import com.tcredit.log.web.util.SettingUtil;
import com.tcredit.log.web.util.ValidateUtils;
import com.tcredit.order.entity.CsrInfoEntity;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
/** 测试类
* @desc 用于删除数据,清空状态
* @author 袁朋
* @date 2018年10月23日 下午5:00:42 */
@Path("/test/")
public class TestController {
private static final Logger logger = LoggerFactory.getLogger(TestController.class);
@Autowired
private TestClient testClient;
/** 删除所有数据
* @author 袁朋
* @date 2018年10月23日 下午5:09:18 */
@Get("delAllData")
public void delAllData(JsonResolver json, @Param("mp") String mp) {
if (StringUtils.isBlank(mp) || !ValidateUtils.isMobile(mp)) {
json.setStatus(true).setMessage("别逗我,请输入正确的手机号");
return;
}
try {
// 将手机号解密--根据解密后的手机号查询 csrId-- 根据 csrId 清空所有数据
String tel = AESUtil.encode(SettingUtil.getValue("yzj.tokenId").replace("-", ""), mp, "UTF-8");
CsrInfoEntity csrInfo = testClient.getCsrInfoByTel(tel);
if (ObjectUtils.isEmpty(csrInfo)) {
json.setStatus(true).setMessage("该手机号:" + mp + ",查不到信息无须删除");
return;
}
testClient.delAllData(csrInfo.getCsrId());
json.setStatus(true).setMessage("该手机号:" + mp + ",被帅气的删除了,手气真好");
} catch (Exception e) {
e.printStackTrace();
json.setStatus(false).setMessage("数据异常了,请联系MrYuan,类名:" + ClassUtils.getClassName());
logger.error("delAllData", e);
}
}
/**
* 根据手机号查订单信息
* @author 袁朋
* @date 2018年10月30日 下午6:30:48
*/
@Get("getCsrInfoByMp")
public void getCsrInfoByMp(JsonResolver json, @Param("mp") String mp) {
if (StringUtils.isBlank(mp) || !ValidateUtils.isMobile(mp)) {
json.setStatus(true).setMessage("别逗我,请输入正确的手机号");
return;
}
try {
// 将手机号解密--根据解密后的手机号查询 csrId-- 根据 csrId 清空所有数据
String tel = AESUtil.encode(SettingUtil.getValue("yzj.tokenId").replace("-", ""), mp, "UTF-8");
CsrInfoEntity csrInfo = testClient.getCsrInfoByTel(tel);
if (ObjectUtils.isEmpty(csrInfo)) {
json.setStatus(true).setMessage("该手机号:" + mp + ",查不到信息无须删除");
return;
}
json.setStatus(true).setData("csrInfo", csrInfo);
} catch (Exception e) {
e.printStackTrace();
json.setStatus(false).setMessage("数据异常了,请联系MrYuan,类名:" + ClassUtils.getClassName());
logger.error("delAllData", e);
}
}
}