7 changed files with 266 additions and 0 deletions
@ -0,0 +1,93 @@
|
||||
package com.logpm.factorydata.olo.aspect; |
||||
|
||||
import cn.hutool.http.HttpRequest; |
||||
import cn.hutool.http.HttpResponse; |
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; |
||||
import lombok.AllArgsConstructor; |
||||
import org.aspectj.lang.ProceedingJoinPoint; |
||||
import org.aspectj.lang.annotation.Around; |
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.springblade.common.cache.CacheNames; |
||||
import org.springblade.common.constant.LauncherConstant; |
||||
import org.springblade.core.redis.cache.BladeRedis; |
||||
import org.springblade.core.redis.lock.LockType; |
||||
import org.springblade.core.redis.lock.RedisLockClient; |
||||
import org.springblade.core.tool.utils.ThreadLocalUtil; |
||||
import org.springframework.core.env.Environment; |
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.mock.web.MockHttpServletRequest; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.web.context.request.RequestContextHolder; |
||||
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import java.util.Objects; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
@Aspect |
||||
@Component |
||||
@AllArgsConstructor |
||||
public class JobAnnotationAspect { |
||||
private final BladeRedis bladeRedis; |
||||
private final Environment environment; |
||||
private final RedisLockClient redisLockClient; |
||||
|
||||
@Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") |
||||
public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { |
||||
|
||||
|
||||
JSONObject jsonObject = mockLogin(); |
||||
MockHttpServletRequest mockRequest = new MockHttpServletRequest(); |
||||
mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); |
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); |
||||
|
||||
HttpHeaders httpHeaders = new HttpHeaders(); |
||||
httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") ); |
||||
httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); |
||||
ThreadLocalUtil.put("bladeContext", httpHeaders); |
||||
|
||||
DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); |
||||
// 执行原方法
|
||||
Object result = joinPoint.proceed(); |
||||
// 在方法执行后,从数据源上下文中移除租户ID
|
||||
DynamicDataSourceContextHolder.poll(); |
||||
|
||||
return result; |
||||
|
||||
|
||||
} |
||||
|
||||
private JSONObject mockLogin() throws InterruptedException { |
||||
|
||||
String account ="shujutongbu"; |
||||
JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); |
||||
if(Objects.isNull(data)){ |
||||
boolean flag = redisLockClient.tryLock("local_server_user", LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS); |
||||
if(flag){ |
||||
data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); |
||||
if(Objects.isNull(data)){ |
||||
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; |
||||
HttpRequest urlRequest = HttpRequest.post(url); |
||||
urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); |
||||
urlRequest.header("Tenant-Id", "627683"); |
||||
|
||||
Map<String, Object> params = new HashMap<>(); |
||||
params.put("grant_type", "local_server"); |
||||
params.put("scope", "all"); |
||||
params.put("username", account); |
||||
params.put("tenantId", "627683"); |
||||
HttpResponse execute = urlRequest.form(params).execute(); |
||||
String body = execute.body(); |
||||
data = JSONObject.parseObject(body); |
||||
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); |
||||
redisLockClient.unLock("local_server_user", LockType.FAIR); |
||||
} |
||||
} |
||||
} |
||||
return data; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,74 @@
|
||||
package com.logpm.factorydata.olo.config; |
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
/** |
||||
* xxl-job config |
||||
* |
||||
* @author xuxueli 2017-04-28 |
||||
*/ |
||||
@Configuration(proxyBeanMethods = false) |
||||
public class XxlJobConfig { |
||||
private final Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); |
||||
|
||||
@Value("${xxl.job.admin.addresses}") |
||||
private String adminAddresses; |
||||
|
||||
@Value("${xxl.job.executor.appname}") |
||||
private String appName; |
||||
|
||||
@Value("${xxl.job.executor.ip}") |
||||
private String ip; |
||||
|
||||
@Value("${xxl.job.executor.port}") |
||||
private int port; |
||||
|
||||
@Value("${xxl.job.accessToken}") |
||||
private String accessToken; |
||||
|
||||
@Value("${xxl.job.executor.logpath}") |
||||
private String logPath; |
||||
|
||||
@Value("${xxl.job.executor.logretentiondays}") |
||||
private int logRetentionDays; |
||||
|
||||
|
||||
@Bean |
||||
public XxlJobSpringExecutor xxlJobExecutor() { |
||||
logger.info(">>>>>>>>>>> xxl-job config init."); |
||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); |
||||
xxlJobSpringExecutor.setAdminAddresses(adminAddresses); |
||||
xxlJobSpringExecutor.setAppName(appName); |
||||
xxlJobSpringExecutor.setIp(ip); |
||||
xxlJobSpringExecutor.setPort(port); |
||||
xxlJobSpringExecutor.setAccessToken(accessToken); |
||||
xxlJobSpringExecutor.setLogPath(logPath); |
||||
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); |
||||
|
||||
return xxlJobSpringExecutor; |
||||
} |
||||
|
||||
/** |
||||
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP; |
||||
* |
||||
* 1、引入依赖: |
||||
* <dependency> |
||||
* <groupId>org.springframework.cloud</groupId> |
||||
* <artifactId>spring-cloud-commons</artifactId> |
||||
* <version>${version}</version> |
||||
* </dependency> |
||||
* |
||||
* 2、配置文件,或者容器启动变量 |
||||
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.' |
||||
* |
||||
* 3、获取IP |
||||
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); |
||||
*/ |
||||
|
||||
|
||||
} |
@ -0,0 +1,36 @@
|
||||
package com.logpm.factorydata.olo.job; |
||||
|
||||
import com.logpm.factorydata.olo.service.DeliveryNoteService; |
||||
import com.xxl.job.core.biz.model.ReturnT; |
||||
import com.xxl.job.core.handler.annotation.XxlJob; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
/** |
||||
* @author zhaoqiaobo |
||||
* @create 2024-04-02 |
||||
*/ |
||||
@AllArgsConstructor |
||||
@Component |
||||
@Slf4j |
||||
public class PushDataJob { |
||||
|
||||
private final DeliveryNoteService deliveryNoteService; |
||||
|
||||
/** |
||||
* 定时扫描我乐手动维护的数据,推送给我乐 |
||||
* |
||||
* @param param |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
@XxlJob("pushNodeData") |
||||
public ReturnT<String> pushDataJob(String param) throws Exception { |
||||
|
||||
deliveryNoteService.customPushNodeData(); |
||||
|
||||
return ReturnT.SUCCESS; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue