bladex从blade-dev.yaml 读取配置信息

blade-dev.yaml配置======nacos文件配置
#sap配置
sap:
  api:
    read:
      url: http://read.xxxxxxxx.com.cn
      port: 80
      username: admin
      password: xxxxx
    write:
      url: http://write.xxxxxxx.com.cn
      port: 80
      username: admin
      password: xxxxx

appliction-dev.yaml配置======= 使用的微服务模块
#服务器端口
server:
  port: 8105

#数据源配置
spring:
  datasource:
    url: ${blade.datasource.dev.url}
    username: ${blade.datasource.dev.username}
    password: ${blade.datasource.dev.password}
#sap配置
sap:
  api:
    read:
      url: ${sap.api.read.url}
      port: ${sap.api.read.port}
      username: ${sap.api.read.username}
      password: ${sap.api.read.password}
    write:
      url: ${sap.api.write.url}
      port: ${sap.api.write.port}
      username: ${sap.api.write.username}
      password: ${sap.api.write.password}

util类
@Component
public class SapUtil {
    public static String readUrl ;
    public static String readPort ;
    public static String readUsername ;
    public static String readPassword;
    public static String writeUrl ;
    public static String writePort ;
    public static String writeUsername ;
    public static String writePassword;

    @Value("${sap.api.read.url}")
    public void setReadUrl(String readUrl){
        SapUtil.readUrl = readUrl;
    }
    @Value("${sap.api.read.port}")
    private void setReadPort(String readPort){
        SapUtil.readPort = readPort;
    }
    @Value("${sap.api.read.username}")
    public void setReadUsername(String readUsername) {
        SapUtil.readUsername = readUsername;
    }
    @Value("${sap.api.read.password}")
    public void setReadPassword(String readPassword) {
        SapUtil.readPassword = readPassword;
    }
    @Value("${sap.api.write.url}")
    public void setWriteUrl(String writeUrl) {
        SapUtil.writeUrl = writeUrl;
    }
    @Value("${sap.api.write.port}")
    public void setWritePort(String writePort) {
        SapUtil.writePort = writePort;
    }
    @Value("${sap.api.write.username}")
    public void setWriteUsername(String writeUsername) {
        SapUtil.writeUsername = writeUsername;
    }
    @Value("${sap.api.write.password}")
    public void setWritePassword(String writePassword) {
        SapUtil.writePassword = writePassword;
    }

    private static String readToken = "";
    private static long readCreateDate = 0l;

    private static String writeToken ="";
    private static long writeCreateDate = 0l ;

    public static String getReadToken(){
        if("".equals(readToken) || Func.isEmpty(readToken) || Func.isNull(readToken)){
            return getToken(readUsername,readPassword);
        }
        else{
            if(DateUtil.now().getTime() - readCreateDate >= 43000000l ){
                return getToken(readUsername,readPassword);
            }
        }
        return readToken;
    }

    public static String getWriteToken(){
        if("".equals(writeToken) || Func.isEmpty(writeToken) || Func.isNull(writeToken)){
            return getToken(writeUsername,writePassword);
        }
        else{
            if(DateUtil.now().getTime() - writeCreateDate >= 43000000l ){
                return getToken(writeUsername,writePassword);
            }
        }
        return writeToken;
    }

    public static String getToken(String username,String password){
        Authorize auth = new Authorize();
        auth.setUsername(username);
        auth.setPassword(password);
        try {
            String res = HttpUtil.doJsonPost(SapConstant.LOGIN_AUTHORIZE, JSONObject.toJSONString(auth),null);
            if(!Func.isEmpty(res)&&!Func.isNull(res)){
                Token token = JSONObject.parseObject(res, Token.class);
                readToken=token.getToken();
                readCreateDate = DateUtil.now().getTime();
            }
        }
        catch(Exception e){
            // TODO
        }
        return readToken;
    }

}

 

posted @ 2020-07-30 13:30  红尘沙漏  阅读(2270)  评论(0编辑  收藏  举报