♻️ 重构代码。重写LCN 部分代码。

This commit is contained in:
冷冷 committed 2018-08-19 01:55:34 +08:00
1 parent 55d98ad2e7
commit f4b25bbb2a
83 files changed
+253 -679

No files matched your search

@@ -34,4 +34,9 @@ public interface ServiceNameConstant {
* UMPS模块
*/
String UMPS_SERVICE = "pigx-upms";
/**
* 分布式事务协调服务
*/
String TX_MANAGER = "pigx-tx-manager";
}
@@ -15,16 +15,39 @@
<description>pigx lcn transaction</description>
<dependencies>
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-core</artifactId>
<version>${pigx.version}</version>
</dependency>
<!--lcn 模块端控制依赖-->
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-client</artifactId>
<version>${lcn.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-plugins-db</artifactId>
<version>${lcn.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--feign 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
@@ -15,23 +15,20 @@
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.transaction.tx;
package com.pig4cloud.pigx.common.transaction;
import com.pig4cloud.pigx.common.transaction.ribbon.loadbalancer.LcnRibbonConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Created by lorne on 2017/6/26.
*@author LCN on 2017/6/26.
*
* @author LCN
* @since 4.1.0
*/
@Configuration
@ComponentScan
@RibbonClients(defaultConfiguration = LcnRibbonConfiguration.class)
@ComponentScan({"com.codingapi.tx", "com.pig4cloud.pigx.common.transaction"})
public class TransactionConfiguration {
@@ -15,37 +15,33 @@
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.transaction.ribbon.loadbalancer;
package com.pig4cloud.pigx.common.transaction.rewrite;
import com.netflix.loadbalancer.NoOpLoadBalancer;
import com.netflix.loadbalancer.Server;
import com.codingapi.tx.netty.service.TxManagerHttpRequestService;
import com.lorne.core.framework.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
/**
* created by foxdd 2017-12-05
*
* @author LCN
* @since 4.1.0
* @author lengleng
* @date 2018/8/19
* 请求tx-manager接口实现
*/
@Slf4j
public class LcnNoOpLoadBalancerProxy extends NoOpLoadBalancer {
@Service
public class TxManagerHttpRequestServiceImpl implements TxManagerHttpRequestService {
private LcnLoadBalancerRule lcnLoadBalancerRule = new LcnLoadBalancerRule();
public LcnNoOpLoadBalancerProxy() {
super();
@Override
public String httpGet(String url) {
String res = HttpUtils.get(url);
log.debug("请求接口 {},响应报文:{}", url, res);
return res;
}
@Override
public Server chooseServer(Object key) {
log.debug("enter chooseServer method, key:" + key);
List<Server> serverList = new ArrayList<Server>();
return lcnLoadBalancerRule.proxy(serverList, super.chooseServer(key));
public String httpPost(String url, String params) {
String res = HttpUtils.post(url, params);
log.debug("请求接口 {},响应报文:{}", url, res);
return res;
}
}
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.transaction.rewrite;
import com.codingapi.tx.config.service.TxManagerTxUrlService;
import com.pig4cloud.pigx.common.core.constant.ServiceNameConstant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
/**
* @author lengleng
* @date 2018/8/19
* 使用服务发现重写 Txmanager 获取规则
*/
@Slf4j
@Service
@AllArgsConstructor
public class TxManagerTxUrlServiceImpl implements TxManagerTxUrlService {
private final LoadBalancerClient loadBalancerClient;
@Override
public String getTxUrl() {
ServiceInstance serviceInstance = loadBalancerClient.choose(ServiceNameConstant.TX_MANAGER);
String host = serviceInstance.getHost();
Integer port = serviceInstance.getPort();
String url = String.format("http://%s:%s/tx/manager/", host, port);
log.info("tm.manager.url -> {}", url);
return url;
}
}
@@ -1,111 +0,0 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.transaction.ribbon;
import com.codingapi.tx.aop.bean.TxTransactionLocal;
import com.lorne.core.framework.utils.encode.MD5Util;
import com.netflix.loadbalancer.Server;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import java.util.List;
/**
* created by foxdd 2017-12-05
*
* @author LCN
* @since 4.1.0
*/
@Slf4j
public class LcnLoadBalancerRule {
public Server proxy(List<Server> servers, Server server) {
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
if (txTransactionLocal == null) {
return server;
}
try {
log.debug("LCNBalanceProxy - > start");
String groupId = txTransactionLocal.getGroupId();
//取出组件的appName
String appName = server.getMetaInfo().getAppName();
String key = MD5Util.md5((groupId + "_" + appName).getBytes());
//如果只有一个可调用模块,则用当前的,且需要将数据记录到redis中
if (servers.size() == 1) {
putServer(key, txTransactionLocal, server);
log.debug("LCNBalanceProxy -> only one server available");
return server;
}
Server oldServer = getServer(txTransactionLocal, servers, key);
if (oldServer != null) {
log.debug("LCNBalanceProxy - > load old server ");
return server;
}
putServer(key, txTransactionLocal, server);
log.debug("LCNBalanceProxy - > load new server ");
return server;
} finally {
log.debug("LCNBalanceProxy - > end");
}
}
private void putServer(String key, TxTransactionLocal txTransactionLocal, Server server) {
String serviceName = server.getMetaInfo().getAppName();
String address = server.getHostPort();
String md5 = MD5Util.md5((address + serviceName).getBytes());
log.debug("putServer->address->" + address + ",md5-->" + md5);
txTransactionLocal.putLoadBalance(key, md5);
}
private Server getServer(TxTransactionLocal txTransactionLocal, List<Server> servers, String key) {
String val = txTransactionLocal.getLoadBalance(key);
if (StringUtils.isEmpty(val)) {
return null;
}
for (Server server : servers) {
String serviceName = server.getMetaInfo().getAppName();
String address = server.getHostPort();
String md5 = MD5Util.md5((address + serviceName).getBytes());
log.debug("getServer->address->" + address + ",md5-->" + md5);
if (val.equals(md5)) {
return server;
}
}
return null;
}
}
@@ -1,51 +0,0 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.transaction.ribbon;
import com.netflix.loadbalancer.NoOpLoadBalancer;
import com.netflix.loadbalancer.Server;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
/**
* created by foxdd 2017-12-05
*
* @author LCN
* @since 4.1.0
*/
@Slf4j
public class LcnNoOpLoadBalancerProxy extends NoOpLoadBalancer {
private LcnLoadBalancerRule lcnLoadBalancerRule = new LcnLoadBalancerRule();
public LcnNoOpLoadBalancerProxy() {
super();
}
@Override
public Server chooseServer(Object key) {
log.debug("enter chooseServer method, key:" + key);
List<Server> serverList = new ArrayList<Server>();
return lcnLoadBalancerRule.proxy(serverList, super.chooseServer(key));
}
}
@@ -1,52 +0,0 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.transaction.ribbon;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author LCN
* @since 4.1.0
*/
@Configuration
public class LcnRibbonConfiguration {
/**
* 为ribbon的loadbalancer做代理,相比于重写IRULE,重写loadbalancer更有利于用户自选LB算法,而且有默认LB算法可用
*
* @param config
* @param serverList
* @param serverListFilter
* @param rule
* @param ping
* @param serverListUpdater
* @return
*/
@Bean
public ILoadBalancer ribbonLoadBalancer(IClientConfig config,
ServerList<Server> serverList, ServerListFilter<Server> serverListFilter,
IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
return new LcnZoneAwareLoadBalancerProxy(config, rule, ping, serverList,
serverListFilter, serverListUpdater);
}
}
@@ -1,59 +0,0 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.transaction.ribbon;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
/**
* created by foxdd 2017-12-05
*
* @author LCN
* @since 4.1.0
*/
@Slf4j
public class LcnZoneAwareLoadBalancerProxy extends ZoneAwareLoadBalancer<Server> {
LcnLoadBalancerRule lcnLoadBalancerRule = new LcnLoadBalancerRule();
public LcnZoneAwareLoadBalancerProxy(IClientConfig clientConfig, IRule rule,
IPing ping, ServerList<Server> serverList, ServerListFilter<Server> filter,
ServerListUpdater serverListUpdater) {
super(clientConfig, rule, ping, serverList, filter, serverListUpdater);
}
@Override
public Server chooseServer(Object key) {
log.info("enter chooseServer method, key:" + key);
//利用Dynamic负载类中的方法,可以在运行时更新服务实例清单
List<Server> serverList;
//获取处理之后的serverlist
serverList = super.getServerListImpl().getUpdatedListOfServers();
//获取过滤之后的serverlist
serverList = super.getFilter().getFilteredListOfServers(serverList);
if (null == serverList || serverList.isEmpty()) {
return super.chooseServer(key);
}
return lcnLoadBalancerRule.proxy(serverList, super.chooseServer(key));
}
}
@@ -1,111 +0,0 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.transaction.ribbon.loadbalancer;
import com.codingapi.tx.aop.bean.TxTransactionLocal;
import com.lorne.core.framework.utils.encode.MD5Util;
import com.netflix.loadbalancer.Server;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import java.util.List;
/**
* created by foxdd 2017-12-05
*
* @author LCN
* @since 4.1.0
*/
@Slf4j
public class LcnLoadBalancerRule {
public Server proxy(List<Server> servers, Server server) {
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
if (txTransactionLocal == null) {
return server;
}
try {
log.debug("LCNBalanceProxy - > start");
String groupId = txTransactionLocal.getGroupId();
//取出组件的appName
String appName = server.getMetaInfo().getAppName();
String key = MD5Util.md5((groupId + "_" + appName).getBytes());
//如果只有一个可调用模块,则用当前的,且需要将数据记录到redis中
if (servers.size() == 1) {
putServer(key, txTransactionLocal, server);
log.debug("LCNBalanceProxy -> only one server available");
return server;
}
Server oldServer = getServer(txTransactionLocal, servers, key);
if (oldServer != null) {
log.debug("LCNBalanceProxy - > load old server ");
return server;
}
putServer(key, txTransactionLocal, server);
log.debug("LCNBalanceProxy - > load new server ");
return server;
} finally {
log.debug("LCNBalanceProxy - > end");
}
}
private void putServer(String key, TxTransactionLocal txTransactionLocal, Server server) {
String serviceName = server.getMetaInfo().getAppName();
String address = server.getHostPort();
String md5 = MD5Util.md5((address + serviceName).getBytes());
log.debug("putServer->address->" + address + ",md5-->" + md5);
txTransactionLocal.putLoadBalance(key, md5);
}
private Server getServer(TxTransactionLocal txTransactionLocal, List<Server> servers, String key) {
String val = txTransactionLocal.getLoadBalance(key);
if (StringUtils.isEmpty(val)) {
return null;
}
for (Server server : servers) {
String serviceName = server.getMetaInfo().getAppName();
String address = server.getHostPort();
String md5 = MD5Util.md5((address + serviceName).getBytes());
log.debug("getServer->address->" + address + ",md5-->" + md5);
if (val.equals(md5)) {
return server;
}
}
return null;
}
}
@@ -1,52 +0,0 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.transaction.ribbon.loadbalancer;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author LCN
* @since 4.1.0
*/
@Configuration
public class LcnRibbonConfiguration {
/**
* 为ribbon的loadbalancer做代理,相比于重写IRULE,重写loadbalancer更有利于用户自选LB算法,而且有默认LB算法可用
*
* @param config
* @param serverList
* @param serverListFilter
* @param rule
* @param ping
* @param serverListUpdater
* @return
*/
@Bean
public ILoadBalancer ribbonLoadBalancer(IClientConfig config,
ServerList<Server> serverList, ServerListFilter<Server> serverListFilter,
IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
return new LcnZoneAwareLoadBalancerProxy(config, rule, ping, serverList,
serverListFilter, serverListUpdater);
}
}
@@ -1,59 +0,0 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.transaction.ribbon.loadbalancer;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
/**
* created by foxdd 2017-12-05
*
* @author LCN
* @since 4.1.0
*/
@Slf4j
public class LcnZoneAwareLoadBalancerProxy extends ZoneAwareLoadBalancer<Server> {
LcnLoadBalancerRule lcnLoadBalancerRule = new LcnLoadBalancerRule();
public LcnZoneAwareLoadBalancerProxy(IClientConfig clientConfig, IRule rule,
IPing ping, ServerList<Server> serverList, ServerListFilter<Server> filter,
ServerListUpdater serverListUpdater) {
super(clientConfig, rule, ping, serverList, filter, serverListUpdater);
}
@Override
public Server chooseServer(Object key) {
log.info("enter chooseServer method, key:" + key);
//利用Dynamic负载类中的方法,可以在运行时更新服务实例清单
List<Server> serverList;
//获取处理之后的serverlist
serverList = super.getServerListImpl().getUpdatedListOfServers();
//获取过滤之后的serverlist
serverList = super.getFilter().getFilteredListOfServers(serverList);
if (null == serverList || serverList.isEmpty()) {
return super.chooseServer(key);
}
return lcnLoadBalancerRule.proxy(serverList, super.chooseServer(key));
}
}
@@ -23,7 +23,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* create by lorne on 2018/1/18
* @author LCN on 2018/1/18
*
* @author LCN
* @since 4.1.0
@@ -25,7 +25,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by lorne on 2017/6/26.
*@author LCN on 2017/6/26.
*
* @author LCN
* @since 4.1.0
@@ -27,7 +27,7 @@ import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
/**
* Created by lorne on 2017/7/3.
*@author LCN on 2017/7/3.
*
* @author LCN
* @since 4.1.0
@@ -27,7 +27,7 @@ import org.springframework.stereotype.Component;
/**
* LCN 事务拦截器
* create by lorne on 2018/1/5
* @author LCN on 2018/1/5
*
* @author LCN
* @since 4.1.0
@@ -28,7 +28,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* Created by lorne on 2017/6/7.
*@author LCN on 2017/6/7.
*
* @author LCN
* @since 4.1.0
@@ -29,7 +29,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Created by lorne on 2017/7/12.
*@author LCN on 2017/7/12.
*
* @author LCN
* @since 4.1.0
@@ -1 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.pig4cloud.pigx.common.transaction.tx.TransactionConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.pig4cloud.pigx.common.transaction.TransactionConfiguration
+6
View File
@@ -102,6 +102,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--分布式事务依赖-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-transaction</artifactId>
<version>${pigx.version}</version>
</dependency>
</dependencies>
<build>
+5 -5
View File
@@ -33,6 +33,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--配置中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
@@ -56,11 +61,6 @@
<artifactId>netty-all</artifactId>
<version>4.1.12.Final</version>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--工具类核心包-->
<dependency>
<groupId>com.pig4cloud</groupId>
@@ -21,7 +21,11 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author LCN
* @author lengleng
* tx-manager ,进行了代码逻辑和代码规范重构
*/
@SpringBootApplication
@EnableDiscoveryClient
public class PigxTxManagerApplication {
@@ -24,7 +24,7 @@ import com.pig4cloud.pigx.manager.compensate.model.TxModel;
import com.pig4cloud.pigx.manager.model.ModelInfo;
import com.pig4cloud.pigx.manager.model.ModelName;
import com.pig4cloud.pigx.manager.model.TxState;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@@ -33,17 +33,17 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by lorne on 2017/7/1.
*@author LCN on 2017/7/1.
*
* @author LCN
* @author lengleng
*/
@RestController
@AllArgsConstructor
@RequestMapping("/admin")
public class AdminController {
@Autowired
private ApiAdminService apiAdminService;
@Autowired
private ApiModelService apiModelService;
private final ApiAdminService apiAdminService;
private final ApiModelService apiModelService;
@RequestMapping(value = "/onlines", method = RequestMethod.GET)
@@ -20,21 +20,22 @@ package com.pig4cloud.pigx.manager.api.controller;
import com.pig4cloud.pigx.manager.api.service.ApiTxManagerService;
import com.pig4cloud.pigx.manager.model.TxServer;
import com.pig4cloud.pigx.manager.model.TxState;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by lorne on 2017/7/1.
*@author LCN on 2017/7/1.
*
* @author LCN
* @author lengleng
*/
@RestController
@AllArgsConstructor
@RequestMapping("/tx/manager")
public class TxManagerController {
@Autowired
private ApiTxManagerService apiTxManagerService;
private final ApiTxManagerService apiTxManagerService;
@RequestMapping("/getServer")
public TxServer getServer() {
@@ -25,7 +25,8 @@ import com.pig4cloud.pigx.manager.model.TxState;
import java.util.List;
/**
* create by lorne on 2017/11/12
* @author LCN on 2017/11/12
* @author LCN
*/
public interface ApiAdminService {
@@ -22,7 +22,8 @@ import com.pig4cloud.pigx.manager.model.ModelInfo;
import java.util.List;
/**
* create by lorne on 2017/11/13
* @author LCN on 2017/11/13
* @author LCN
*/
public interface ApiModelService {
@@ -21,7 +21,7 @@ import com.pig4cloud.pigx.manager.model.TxServer;
import com.pig4cloud.pigx.manager.model.TxState;
/**
* Created by lorne on 2017/7/1.
*@author LCN on 2017/7/1.
*/
public interface ApiTxManagerService {
@@ -26,26 +26,23 @@ import com.pig4cloud.pigx.manager.manager.service.MicroService;
import com.pig4cloud.pigx.manager.model.ModelName;
import com.pig4cloud.pigx.manager.model.TxState;
import com.pig4cloud.pigx.manager.redis.RedisServerService;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* create by lorne on 2017/11/12
* @author LCN on 2017/11/12
*
* @author LCN
* @author lengleng
*/
@Service
@AllArgsConstructor
public class ApiAdminServiceImpl implements ApiAdminService {
@Autowired
private MicroService eurekaService;
@Autowired
private RedisServerService redisServerService;
@Autowired
private CompensateService compensateService;
private final MicroService eurekaService;
private final RedisServerService redisServerService;
private final CompensateService compensateService;
@Override
public TxState getState() {
@@ -25,7 +25,8 @@ import org.springframework.stereotype.Service;
import java.util.List;
/**
* create by lorne on 2017/11/13
* @author LCN on 2017/11/13
* @author LCN
*/
@Service
public class ApiModelServiceImpl implements ApiModelService {
@@ -27,31 +27,28 @@ import com.pig4cloud.pigx.manager.manager.service.TxManagerSenderService;
import com.pig4cloud.pigx.manager.manager.service.TxManagerService;
import com.pig4cloud.pigx.manager.model.TxServer;
import com.pig4cloud.pigx.manager.model.TxState;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
/**
* Created by lorne on 2017/7/1.
*@author LCN on 2017/7/1.
*
* @author LCN
* @author lengleng
*/
@Service
@AllArgsConstructor
public class ApiTxManagerServiceImpl implements ApiTxManagerService {
private final TxManagerService managerService;
@Autowired
private TxManagerService managerService;
private final MicroService eurekaService;
@Autowired
private MicroService eurekaService;
private final CompensateService compensateService;
@Autowired
private CompensateService compensateService;
private final TxManagerSenderService txManagerSenderService;
@Autowired
private TxManagerSenderService txManagerSenderService;
@Autowired
private ConfigReader configReader;
private final ConfigReader configReader;
@Override
@@ -22,7 +22,7 @@ import com.pig4cloud.pigx.manager.compensate.model.TransactionCompensateMsg;
import java.util.List;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
public interface CompensateDao {
@@ -30,7 +30,7 @@ import java.util.ArrayList;
import java.util.List;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service
public class CompensateDaoImpl implements CompensateDao {
@@ -20,7 +20,7 @@ package com.pig4cloud.pigx.manager.compensate.model;
import com.pig4cloud.pigx.manager.netty.model.TxGroup;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
public class TransactionCompensateMsg {
@@ -18,7 +18,7 @@
package com.pig4cloud.pigx.manager.compensate.model;
/**
* create by lorne on 2017/11/12
* @author LCN on 2017/11/12
*/
public class TxModel {
@@ -26,7 +26,7 @@ import com.pig4cloud.pigx.manager.netty.model.TxGroup;
import java.util.List;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
public interface CompensateService {
@@ -47,12 +47,12 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service
public class CompensateServiceImpl implements CompensateService {
private static final String SUCCESS = "success";
private static final String SUCCESS1 = "SUCCESS";
private Logger logger = LoggerFactory.getLogger(CompensateServiceImpl.class);
@Autowired
@@ -109,7 +109,7 @@ public class CompensateServiceImpl implements CompensateService {
logger.error("Compensate Callback Result->" + res);
if (configReader.isCompensateAuto()) {
//自动补偿,是否自动执行补偿
if (res.contains("success") || res.contains("SUCCESS")) {
if (res.contains(SUCCESS) || res.contains(SUCCESS1)) {
//自动补偿
autoCompensate(compensateKey, transactionCompensateMsg);
}
@@ -134,7 +134,7 @@ public class CompensateServiceImpl implements CompensateService {
boolean autoExecuteRes = false;
try {
int executeCount = 0;
autoExecuteRes = _executeCompensate(json);
autoExecuteRes = executeCompensate_(json);
logger.info("Automatic Compensate Result->" + autoExecuteRes + ",json->" + json);
while (!autoExecuteRes) {
logger.info("Compensate Failure, Entering Compensate Queue->" + autoExecuteRes + ",json->" + json);
@@ -148,7 +148,7 @@ public class CompensateServiceImpl implements CompensateService {
} catch (InterruptedException e) {
e.printStackTrace();
}
autoExecuteRes = _executeCompensate(json);
autoExecuteRes = executeCompensate_(json);
}
//执行成功删除数据
@@ -181,7 +181,7 @@ public class CompensateServiceImpl implements CompensateService {
public List<ModelName> loadModelList() {
List<String> keys = compensateDao.loadCompensateKeys();
Map<String, Integer> models = new HashMap<String, Integer>();
Map<String, Integer> models = new HashMap<>(16);
for (String key : keys) {
if (key.length() > 36) {
@@ -296,6 +296,7 @@ public class CompensateServiceImpl implements CompensateService {
logger.info("Compensate Loaded->" + JSON.toJSONString(txGroup));
}
@Override
public TxGroup getCompensateByGroupId(String groupId) {
String json = compensateDao.getCompensateByGroupId(groupId);
if (json == null) {
@@ -315,7 +316,7 @@ public class CompensateServiceImpl implements CompensateService {
throw new ServiceException("no data existing");
}
boolean hasOk = _executeCompensate(json);
boolean hasOk = executeCompensate_(json);
if (hasOk) {
// 删除本地补偿数据
compensateDao.deleteCompensateByPath(path);
@@ -326,7 +327,7 @@ public class CompensateServiceImpl implements CompensateService {
}
private boolean _executeCompensate(String json) throws ServiceException {
private boolean executeCompensate_(String json) throws ServiceException {
JSONObject jsonObject = JSON.parseObject(json);
String model = jsonObject.getString("model");
@@ -21,7 +21,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Component
public class ConfigReader {
@@ -24,7 +24,7 @@ import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* create by lorne on 2017/8/17
* @author LCN on 2017/8/17
*/
@Configuration
public class CorsConfig {
@@ -23,7 +23,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* Created by lorne on 2017/7/5.
*@author LCN on 2017/7/5.
*/
@Configuration
@@ -18,7 +18,7 @@
package com.pig4cloud.pigx.manager.framework.utils;
/**
* Created by lorne on 2017/6/8.
*@author LCN on 2017/6/8.
*/
public class Constants {
@@ -25,7 +25,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Created by lorne on 2017/6/30.
*@author LCN on 2017/6/30.
*/
public class SocketManager {
@@ -24,7 +24,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;
/**
* Created by lorne on 2017/7/6.
*@author LCN on 2017/7/6.
*/
public class SocketUtils {
@@ -26,7 +26,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* create by lorne on 2017/8/7
* @author LCN on 2017/8/7
*/
@Component
public class ApplicationStartListener implements ApplicationListener<WebServerInitializedEvent> {
@@ -27,7 +27,7 @@ import javax.servlet.ServletContextListener;
/**
* Created by lorne on 2017/7/1.
*@author LCN on 2017/7/1.
*/
@Component
@@ -18,7 +18,7 @@
package com.pig4cloud.pigx.manager.listener.service;
/**
* Created by lorne on 2017/7/4.
*@author LCN on 2017/7/4.
*/
public interface InitService {
@@ -26,7 +26,7 @@ import org.springframework.stereotype.Service;
/**
* Created by lorne on 2017/7/4.
*@author LCN on 2017/7/4.
*/
@Service
public class InitServiceImpl implements InitService {
@@ -23,7 +23,7 @@ import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* create by lorne on 2017/11/13
* @author LCN on 2017/11/13
*/
public class ModelInfoManager {
@@ -20,7 +20,7 @@ package com.pig4cloud.pigx.manager.manager.service;
import com.pig4cloud.pigx.manager.model.LoadBalanceInfo;
/**
* create by lorne on 2017/12/5
* @author LCN on 2017/12/5
*/
public interface LoadBalanceService {
@@ -21,11 +21,11 @@ import com.pig4cloud.pigx.manager.model.TxServer;
import com.pig4cloud.pigx.manager.model.TxState;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
public interface MicroService {
String tmKey = "tx-manager";
String TMKEY = "tx-manager";
TxServer getServer();
@@ -20,7 +20,7 @@ package com.pig4cloud.pigx.manager.manager.service;
import com.pig4cloud.pigx.manager.netty.model.TxGroup;
/**
* Created by lorne on 2017/6/9.
*@author LCN on 2017/6/9.
*/
public interface TxManagerSenderService {
@@ -20,7 +20,7 @@ package com.pig4cloud.pigx.manager.manager.service;
import com.pig4cloud.pigx.manager.netty.model.TxGroup;
/**
* Created by lorne on 2017/6/7.
*@author LCN on 2017/6/7.
*/
public interface TxManagerService {
@@ -25,7 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* create by lorne on 2017/12/5
* @author LCN on 2017/12/5
*/
@Service
public class LoadBalanceServiceImpl implements LoadBalanceService {
@@ -36,7 +36,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service
public class MicroServiceImpl implements MicroService {
@@ -87,7 +87,7 @@ public class MicroServiceImpl implements MicroService {
private List<String> getServices() {
List<String> urls = new ArrayList<>();
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(tmKey);
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(TMKEY);
for (ServiceInstance instanceInfo : serviceInstances) {
urls.add(instanceInfo.getUri().toASCIIString());
}
@@ -48,7 +48,7 @@ import java.util.concurrent.*;
/**
* Created by lorne on 2017/6/9.
* @author LCN on 2017/6/9.
*/
@Service
public class TxManagerSenderServiceImpl implements TxManagerSenderService {
@@ -148,8 +148,8 @@ public class TxManagerSenderServiceImpl implements TxManagerSenderService {
final JSONObject jsonObject = new JSONObject();
jsonObject.put("a", "t");
if (txGroup.getIsCompensate() == 1) { //补偿请求
//补偿请求
if (txGroup.getIsCompensate() == 1) {
jsonObject.put("c", txInfo.getIsCommit());
} else { //正常业务
jsonObject.put("c", checkSate);
@@ -246,21 +246,18 @@ public class TxManagerSenderServiceImpl implements TxManagerSenderService {
//创建Task
final Task task = ConditionUtils.getInstance().createTask(key);
threadPool.execute(new Runnable() {
@Override
public void run() {
while (!task.isAwait() && !Thread.currentThread().interrupted()) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadPool.execute(() -> {
while (!task.isAwait() && !Thread.interrupted()) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Channel channel = SocketManager.getInstance().getChannelByModelName(model);
if (channel != null && channel.isActive()) {
SocketUtils.sendMsg(channel, msg);
}
Channel channel = SocketManager.getInstance().getChannelByModelName(model);
if (channel != null && channel.isActive()) {
SocketUtils.sendMsg(channel, msg);
}
});
@@ -283,28 +280,20 @@ public class TxManagerSenderServiceImpl implements TxManagerSenderService {
private void threadAwaitSend(final Task task, final TxInfo txInfo, final String msg) {
threadPool.execute(new Runnable() {
@Override
public void run() {
while (!task.isAwait() && !Thread.currentThread().interrupted()) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadPool.execute(() -> {
while (!task.isAwait() && !Thread.interrupted()) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (txInfo != null && txInfo.getChannel() != null) {
txInfo.getChannel().send(msg, task);
} else {
task.setBack(new IBack() {
@Override
public Object doing(Object... objs) throws Throwable {
return "-2";
}
});
task.signalTask();
}
if (txInfo != null && txInfo.getChannel() != null) {
txInfo.getChannel().send(msg, task);
} else {
task.setBack(objs -> "-2");
task.signalTask();
}
});
@@ -35,7 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by lorne on 2017/6/7.
*@author LCN on 2017/6/7.
*/
@Service
public class TxManagerServiceImpl implements TxManagerService {
@@ -25,7 +25,7 @@ import io.netty.channel.Channel;
import org.apache.commons.lang.StringUtils;
/**
* create by lorne on 2017/8/7
* @author LCN on 2017/8/7
*/
public class ChannelSender {
@@ -19,7 +19,7 @@ package com.pig4cloud.pigx.manager.model;
/**
* 负载均衡模块信息
* create by lorne on 2017/12/5
* @author LCN on 2017/12/5
*/
public class LoadBalanceInfo {
@@ -19,7 +19,7 @@ package com.pig4cloud.pigx.manager.model;
/**
* 模块信息
* create by lorne on 2017/11/13
* @author LCN on 2017/11/13
*/
public class ModelInfo {
@@ -20,7 +20,7 @@
package com.pig4cloud.pigx.manager.model;
/**
* create by lorne on 2017/11/22
* @author LCN on 2017/11/22
*/
public class ModelName {
@@ -18,7 +18,7 @@
package com.pig4cloud.pigx.manager.model;
/**
* Created by lorne on 2017/7/1.
*@author LCN on 2017/7/1.
*/
public class TxServer {
@@ -20,7 +20,7 @@ package com.pig4cloud.pigx.manager.model;
import java.util.List;
/**
* Created by lorne on 2017/7/1.
*@author LCN on 2017/7/1.
*/
public class TxState {
@@ -18,7 +18,7 @@
package com.pig4cloud.pigx.manager.netty.handler;
/**
* Created by lorne on 2017/6/29.
* @author LCN on 2017/6/29.
*/
import com.alibaba.fastjson.JSONObject;
@@ -40,6 +40,8 @@ import java.util.concurrent.Executor;
/**
* Handles a server-side channel.
*
* @author lengleng
*/
@ChannelHandler.Sharable
@@ -25,7 +25,7 @@ import java.util.ArrayList;
import java.util.List;
/**
* Created by lorne on 2017/6/7.
*@author LCN on 2017/6/7.
*/
public class TxGroup {
@@ -22,7 +22,7 @@ import com.lorne.core.framework.model.JsonModel;
import com.pig4cloud.pigx.manager.model.ChannelSender;
/**
* Created by lorne on 2017/6/7.
*@author LCN on 2017/6/7.
*/
public class TxInfo extends JsonModel {
@@ -20,7 +20,7 @@ package com.pig4cloud.pigx.manager.netty.service;
import com.alibaba.fastjson.JSONObject;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
public interface IActionService {
@@ -18,7 +18,7 @@
package com.pig4cloud.pigx.manager.netty.service;
/**
* Created by lorne on 2017/6/30.
*@author LCN on 2017/6/30.
*/
public interface NettyServerService {
@@ -18,7 +18,7 @@
package com.pig4cloud.pigx.manager.netty.service;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
public interface NettyService {
@@ -26,7 +26,7 @@ import org.springframework.stereotype.Service;
/**
* 添加事务组
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "atg")
public class ActionATGServiceImpl implements IActionService {
@@ -26,7 +26,7 @@ import org.springframework.stereotype.Service;
/**
* 创建事务组
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "cg")
public class ActionCGServiceImpl implements IActionService {
@@ -25,7 +25,7 @@ import org.springframework.stereotype.Service;
/**
* 检查事务组
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "ckg")
public class ActionCKGServiceImpl implements IActionService {
@@ -22,7 +22,7 @@ import org.springframework.stereotype.Service;
/**
* 补偿回调
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "c")
public class ActionCServiceImpl extends BaseSignalTaskService implements IActionService {
@@ -25,7 +25,7 @@ import org.springframework.stereotype.Service;
/**
* 关闭事务组
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "ctg")
public class ActionCTGServiceImpl implements IActionService {
@@ -26,7 +26,7 @@ import org.springframework.stereotype.Service;
/**
* 获取负载模块信息
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "glb")
public class ActionGLBServiceImpl implements IActionService {
@@ -25,7 +25,7 @@ import org.springframework.stereotype.Service;
/**
* 心跳包
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "h")
public class ActionHServiceImpl implements IActionService {
@@ -26,7 +26,7 @@ import org.springframework.stereotype.Service;
/**
* 添加负载模块信息
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "plb")
public class ActionPLBServiceImpl implements IActionService {
@@ -25,7 +25,7 @@ import org.springframework.stereotype.Service;
/**
* 强制回滚事务组
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "rg")
public class ActionRGServiceImpl implements IActionService {
@@ -22,7 +22,7 @@ import org.springframework.stereotype.Service;
/**
* 通知事务回调
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "t")
public class ActionTServiceImpl extends BaseSignalTaskService implements IActionService {
@@ -26,7 +26,7 @@ import org.springframework.stereotype.Service;
/**
* 上传模块信息
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service(value = "umi")
public class ActionUMIServiceImpl implements IActionService {
@@ -23,7 +23,7 @@ import com.lorne.core.framework.utils.task.IBack;
import com.lorne.core.framework.utils.task.Task;
/**
* create by lorne on 2017/11/13
* @author LCN on 2017/11/13
*/
public class BaseSignalTaskService {
@@ -45,7 +45,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* Created by lorne on 2017/6/30.
*@author LCN on 2017/6/30.
*/
@Service
public class NettyServerServiceImpl implements NettyServerService, DisposableBean {
@@ -71,7 +71,7 @@ public class NettyServerServiceImpl implements NettyServerService, DisposableBea
public void start() {
final int heartTime = configReader.getTransactionNettyHeartTime() + 10;
txCoreServerHandler = new TxCoreServerHandler(threadPool, nettyService);
bossGroup = new NioEventLoopGroup(50); // (1)
bossGroup = new NioEventLoopGroup(50);
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
@@ -24,7 +24,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service
public class NettyServiceImpl implements NettyService {
@@ -23,7 +23,7 @@ import com.pig4cloud.pigx.manager.netty.model.TxGroup;
import java.util.List;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
public interface RedisServerService {
@@ -35,7 +35,7 @@ import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* create by lorne on 2017/11/11
* @author LCN on 2017/11/11
*/
@Service
public class RedisServerServiceImpl implements RedisServerService {