1.在dubbo的interface工程service层结构下写入接口
public interface LqSystemSetConfigDubboService {
//获取redis中的value值
SystemSetConfigDubbo getByKey(Long orgId);
//更新redis数据
boolean setRedis(Long orgId);
//根据key键删除redis
boolean deletedRedis(Long orgId);
}
2.在dubbo的provider工程service层下写入实现类
@Transactional
@Component
@org.springframework.stereotype.Service("lqSystemSetConfigDubboService")
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = LqSystemSetConfigDubboService.class)
public class LqSystemSetConfigDubboServiceImpl implements LqSystemSetConfigDubboService {
private static Logger logger = LogManager.getLogger("service");
@Autowired
RedisTemplate redisTemplate;
@Autowired
LqSystemSetConfigMapper lqSystemSetConfigMapper;
@Override
public SystemSetConfigDubbo getByKey(Long orgId) {
log.info("----getByKey------"+orgId);
if (orgId ==null){
logger.error("数据参数错误"+orgId);
return null;
}
Object object = RedisUtil.getValue(redisTemplate, CarownerhomeKeyName.systemSetConfigLq + orgId);
if (object==null){
SystemSetConfigDubbo systemSetConfigDubbo = lqSystemSetConfigMapper.selectByOrgId(orgId);
if (systemSetConfigDubbo==null){
logger.info("暂无数据");
return null;
}else {
return systemSetConfigDubbo;
}
}else {
SystemSetConfigRedis systemSetConfigRedis=(SystemSetConfigRedis)object;
SystemSetConfigDubbo systemSetConfigDubbo =new SystemSetConfigDubbo();
BeanUtils.copyProperties(systemSetConfigRedis,systemSetConfigDubbo);
return systemSetConfigDubbo;
}
}
@Override
public boolean setRedis(Long orgId) {
if (orgId==null){
logger.error("数据参数错误"+orgId);
return false;
}
SystemSetConfigDubbo systemSetConfigDubbo = lqSystemSetConfigMapper.selectByOrgId(orgId);
if (systemSetConfigDubbo==null){
return false;
}else {
SystemSetConfigRedis systemSetConfigRedis=new SystemSetConfigRedis();
BeanUtils.copyProperties(systemSetConfigDubbo,systemSetConfigRedis);
boolean b = RedisUtil.setRedis(redisTemplate, CarownerhomeKeyName.systemSetConfigLq + orgId, systemSetConfigRedis);
if (b){
return true;
}else {
return false;
}
}
}
@Override
public boolean deletedRedis(Long orgId) {
if (orgId==null){
logger.error("数据参数错误"+orgId);
return false;
}
Object o = RedisUtil.deleteRedis(redisTemplate, CarownerhomeKeyName.systemSetConfigLq + orgId);
if (o instanceof Boolean){
return (Boolean)o;
}else {
return false;
}
}
}
3.springBoot工程调用
@RestController
@RequestMapping("/testLqSystemSetConfig")
public class LqSystemSetConfigController {
@Reference
LqSystemSetConfigDubboService lqSystemSetConfigDubboService;
@GetMapping("/setRedis/{orgId}")
public boolean testSeRedis(@PathVariable Long orgId){
boolean b = lqSystemSetConfigDubboService.setRedis(orgId);
return b;
}
@GetMapping("/getRedis/{orgId}")
public SystemSetConfigDubbo testGetRedis(@PathVariable Long orgId){
SystemSetConfigDubbo systemSetConfigDubbo = lqSystemSetConfigDubboService.getByKey(orgId);
return systemSetConfigDubbo;
}
@GetMapping("/deletedRedis")
public boolean testDeletedRedis(@RequestParam(required = true) long orgId){
boolean b = lqSystemSetConfigDubboService.deletedRedis(orgId);
return b;
}
}
4.struts2框架中,需要在xml文件中,配置dubbo接口
<?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="carownerhomeCrm-customer" logger="slf4j" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<!-- register:false,只订阅,不注册 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- dubbo接口 -->
<dubbo:reference interface="com.zhiguan.carowner.springbootDubbo.service.system.LqSystemSetConfigDubboService"
id="LqSystemSetConfigDubboService"/>
</beans>
然后就可义调用了
public class LqSystemSetConfigAction extends BaseAction {
private static final long serialVersionUID = 1L;
private static Logger logger = (Logger) LogManager.getLogger("action");
@Resource
LqSystemSetConfigDubboService lqSystemSetConfigDubboService;
@Resource
SystemSetConfigService systemSetConfigService;
public void testSetRedis(){
logger.info("数据正常");
String orgId = request.getParameter("orgId");
if (orgId.isEmpty()){
logger.error("参数错误");
return;
}
boolean b = lqSystemSetConfigDubboService.setRedis(Long.valueOf(orgId));
if (b){
this.writeJson(b, true);
}else {
this.writeJson(b, false);
}
}
public void testGetRedis(){
String orgId = request.getParameter("orgId");
if (orgId.isEmpty()){
logger.error("参数错误");
return;
}
SystemSetConfigDubbo systemSetConfigDubbo = lqSystemSetConfigDubboService.getByKey(Long.valueOf(orgId));
if (systemSetConfigDubbo==null){
SystemSetConfig setConfig = systemSetConfigService.selectOneByBak5(Long.parseLong(orgId));
if (setConfig==null){
this.writeJson("暂无数据", true);
}else {
this.writeJson(setConfig, true);
}
}
this.writeJson(systemSetConfigDubbo, true);
}
public void testDeletedRedis(){
String orgId = request.getParameter("orgId");
if (orgId.isEmpty()){
logger.error("参数错误");
return;
}
boolean b = lqSystemSetConfigDubboService.deletedRedis(Long.valueOf(orgId));
if (b){
this.writeJson(b, true);
}else {
this.writeJson(b, false);
}
}
}