Commit 1ed36a7c by 刘鑫

FEAT: BEAN 接口连通性测试

1 parent bc4b886a
package com.dituhui.pea.order.common;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) {
// ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(),
// ObjectMapper.DefaultTyping.EVERYTHING, JsonTypeInfo.As.PROPERTY);
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
package com.dituhui.pea.order.common;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* redis服务
*/
@Component
@Slf4j
@RequiredArgsConstructor
public class RedisService {
private final RedisTemplate<String, String> redisTemplate;
public long getExpireTime(String key) {
try {
Long expire = redisTemplate.getExpire(key);
if (Objects.nonNull(expire)) {
return expire;
}
} catch (Exception e) {
log.error("[set]---------->redis获取过期时间失败, key为: {}, 失败原因:{}", key, e.getMessage(), e);
}
return 0;
}
public boolean hasKey(String key) {
try {
return Boolean.TRUE.equals(redisTemplate.hasKey(key));
} catch (Exception e) {
log.error("[set]---------->redis判定key是否存在失败, key为: {}, 失败原因:{}", key, e.getMessage(), e);
}
return false;
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, String value) {
boolean result = false;
try {
ValueOperations<String, String> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
log.error("[set]---------->redis存值失败, key为: {}, 失败原因:{}", key, e.getMessage(), e);
}
return result;
}
/**
* 写入缓存设置时效时间
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, String value, Long expireTime) {
boolean result = false;
try {
ValueOperations<String, String> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
log.error("[set]---------->redis存值失败, key为: {}, 失败原因:{}", key, e.getMessage(), e);
}
return result;
}
/**
* 读取缓存
*
* @param key
* @return
*/
public String get(final String key) {
String result = null;
ValueOperations<String, String> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
}
package com.dituhui.pea.order.controller;
import com.dituhui.pea.order.service.impl.BeanRemoteServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//TODO 仅用于联通测试
@RestController
@RequestMapping("/pea-order/beant")
@RequiredArgsConstructor
@Slf4j
public class TestController {
private final BeanRemoteServiceImpl beanRemoteServiceImpl;
@GetMapping("/test")
public void test() {
beanRemoteServiceImpl.testAllDepartment();
}
}
package com.dituhui.pea.order.service.impl;
import com.dituhui.pea.common.BusinessException;
import com.dituhui.pea.order.common.RedisService;
import com.dituhui.pea.order.common.jackson.JsonUtil;
import com.dituhui.pea.order.feign.IBeanRemoteService;
import com.dituhui.pea.order.feign.bean.AccessToken;
import com.dituhui.pea.order.feign.bean.BeanR;
import com.dituhui.pea.order.feign.bean.Department;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
/**
* 调用BEAN远程接口实现
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class BeanRemoteServiceImpl {
private final static String CACHE_ACCESS_TOKEN_KEY = "BEAN:APP:ACCESS_TOKEN";
private final IBeanRemoteService beanRemoteService;
private final RedisService redisService;
@Value("${bean.app-key}")
private String appKey;
@Value("${bean.secret-key}")
private String secretKey;
//TODO 仅用于联通测试
public void testAllDepartment() {
String accessToken = getAccessToken();
BeanR<List<Department>> listBeanR = beanRemoteService.allDepartment(accessToken);
log.info("[testAllDepartment]【/api/openapi/department/queryList】返回值-------------------->{}", JsonUtil.toJson(listBeanR));
}
private String getAccessToken() {
if (redisService.hasKey(CACHE_ACCESS_TOKEN_KEY) && redisService.getExpireTime(CACHE_ACCESS_TOKEN_KEY) > 500) {
return redisService.get(CACHE_ACCESS_TOKEN_KEY);
}
AccessToken.Result remoteBeanAccessToken = getRemoteBeanAccessToken(appKey, secretKey);
if (Objects.isNull(remoteBeanAccessToken)) {
throw new BusinessException("调用BEAN获取ACCESS_TOKEN接口错误--------> 空返回值");
}
final String accessToken = remoteBeanAccessToken.getAccess_token();
final long expireTime = Long.parseLong(remoteBeanAccessToken.getExpire_time());
//存REDIS获取
redisService.set(CACHE_ACCESS_TOKEN_KEY, accessToken, expireTime);
return accessToken;
}
private AccessToken.Result getRemoteBeanAccessToken(String appKey, String secretKey) {
final long timeMillis = System.currentTimeMillis();
final String verifyCode = DigestUtils.md5Hex(secretKey + "_" + timeMillis);
AccessToken.Request requestParam = new AccessToken.Request();
requestParam.setTimestamp(timeMillis);
requestParam.setAppKey(appKey);
requestParam.setVerifyCode(verifyCode);
log.info("[BEAN]【BeanRemoteServiceImpl】请求参数------------->{}", JsonUtil.toJson(requestParam));
BeanR<AccessToken.Result> accessTokenR = beanRemoteService.getAccessToken(requestParam);
return accessTokenR.getData();
}
}
......@@ -66,6 +66,7 @@ SaaS:
bean:
server: https://bean-test.bshg.com.cn
app-key: eDZEhTCxAcM9paRfwPjAM7RGkfmbf15S_PEA
secret-key: 9kB3ESrYhh8A08nta7CjnYn2Jw1wAEXH_PEA
scheduler:
init-engineer-capacity:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!