阿里云docker容器部署spring boot項目 加載外部文件資源

首先 spring boot項目默認搭的是jar,這時候jar和以前web 項目的war包部署在加載外部資源文件還是有點區(qū)別的

1.首先把需要加載的文件放在服務器某一個文件夾里:比如 /opt/config

對應的配置文件


image.png

系統(tǒng)里面加載的配置文件類

import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.ScheduledUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;


@Configuration
@PropertySource("classpath:wxpay.properties") //讀取配置文件
@ConfigurationProperties(prefix = "wxpay") //讀取wxpay節(jié)點
@Data //使用set方法將wxpay節(jié)點中的值填充到當前類的屬性中
@Slf4j
public class WxPayConfig {
    // private String url =new ClassPathResource("wxxcqpay_key.pem").getPath();
    // private String url = WxPayConfig.class.getResource("/apiclient_key.pem").getPath();
    /**
     * 阿里云 加載資源
     */
    //  public ClassPathResource classPathResource = new ClassPathResource("/opt/upload/apiclient_key.pem");

    /**
     * 本地環(huán)境 加載資源
     */
    //private String filePath = WxPayConfig.class.getResource("/apiclient_key.pem").getPath();
    //private String filePath = WxPayConfig.class.getResource("/opt/upload/wxxcqpay_key.pem").getPath();
 //   private String url = new ClassPathResource("/wxxcqpay_key.pem").getPath();
    // 商戶號
    private String mchId;

    private String secret;

    // 商戶API證書序列號
    private String mchSerialNo;

    // 商戶私鑰文件
    public String privateKeyPath;

    // APIv3密鑰
    private String apiV3Key;

    // APPID
    private String appid;

    // 微信服務器地址
    private String domain;

    // 接收結(jié)果通知地址
    private String notifyDomain;

    //    // APIv2密鑰
    private String partnerKey;
    /**
     * 獲取商戶的私鑰文件
     *
     * @param filename
     * @return getPartnerKey
     */
    public PrivateKey getPrivateKey(String filename) {
        log.info("filename-------->{}", filename);
        log.info("url-------->{}", url);
        try {
            return PemUtil.loadPrivateKey(new FileInputStream(filename));
        } catch (FileNotFoundException e) {
            throw new RuntimeException("私鑰文件不存在1", e);
        }
    }
    /**
     * 獲取簽名驗證器
     *
     * @return
     * @throws UnsupportedEncodingException
     */
    @Bean
    public ScheduledUpdateCertificatesVerifier getVerifier() throws UnsupportedEncodingException {
        log.info("獲取簽名驗證器---");
        // 阿里云環(huán)境
//        ApplicationHome applicationHome = new ApplicationHome(WxPayConfig.class);
//        String s = applicationHome.getSource().getParentFile().toString();
//        String filePath = s + "/apiclient_key.pem";
        //  PrivateKey privateKey = getPrivateKey(URLDecoder.decode(filePath, "utf-8"));
        // PrivateKey privateKey = getPrivateKey(URLDecoder.decode(url, "utf-8"));
        //獲取商戶私鑰
        PrivateKey privateKey = getPrivateKey(privateKeyPath);
        log.info("獲取簽名驗證器", privateKey);
        PrivateKeySigner privateKeySigner = new PrivateKeySigner(mchSerialNo, privateKey);
        WechatPay2Credentials wechatPay2Credentials = new WechatPay2Credentials(mchId, privateKeySigner);
        ScheduledUpdateCertificatesVerifier verifier = new ScheduledUpdateCertificatesVerifier(
                wechatPay2Credentials,
                apiV3Key.getBytes(StandardCharsets.UTF_8));
        return verifier;
    }
    /**
     * 獲取http請求對象
     *
     * @param verifier
     * @return
     * @throws UnsupportedEncodingException
     */
    @Bean(name = "wxPayClient")
    public CloseableHttpClient getWxPayClient(ScheduledUpdateCertificatesVerifier verifier) throws UnsupportedEncodingException {
        log.info("獲取httpClient");
        // 阿里云環(huán)境
//        ApplicationHome applicationHome = new ApplicationHome(WxPayConfig.class);
//        String s = applicationHome.getSource().getParentFile().toString();
//        String filePath = s + "/apiclient_key.pem";

        //獲取商戶私鑰
        //  PrivateKey privateKey = getPrivateKey(URLDecoder.decode(filePath, "utf-8"));
        //  PrivateKey privateKey = getPrivateKey(URLDecoder.decode(url, "utf-8"));
        //獲取商戶私鑰
        PrivateKey privateKey = getPrivateKey(privateKeyPath);
        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
                .withMerchant(mchId, mchSerialNo, privateKey)
                .withValidator(new WechatPay2Validator(verifier));
        // ... 接下來,你仍然可以通過builder設置各種參數(shù),來配置你的HttpClient
        // 通過WechatPayHttpClientBuilder構造的HttpClient,會自動的處理簽名和驗簽,并進行證書自動更新
        CloseableHttpClient httpClient = builder.build();
        return httpClient;
    }
    /**
     * 獲取HttpClient,無需進行應答簽名驗證,跳過驗簽的流程
     */
    @Bean(name = "wxPayNoSignClient")
    public CloseableHttpClient getWxPayNoSignClient() throws UnsupportedEncodingException {

        //獲取商戶私鑰
//        PrivateKey privateKey = getPrivateKey(privateKeyPath);

        // 阿里云環(huán)境
//        ApplicationHome applicationHome = new ApplicationHome(WxPayConfig.class);
//        String s = applicationHome.getSource().getParentFile().toString();
//        String filePath = s + "/apiclient_key.pem";

        // PrivateKey privateKey = getPrivateKey(URLDecoder.decode(filePath, "utf-8"));
        //    PrivateKey privateKey = getPrivateKey(URLDecoder.decode(url, "utf-8"));
        //獲取商戶私鑰
        PrivateKey privateKey = getPrivateKey(privateKeyPath);
        //用于構造HttpClient
        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
                //設置商戶信息
                .withMerchant(mchId, mchSerialNo, privateKey)
                //無需進行簽名驗證、通過withValidator((response) -> true)實現(xiàn)
                .withValidator((response) -> true);

        // 通過WechatPayHttpClientBuilder構造的HttpClient,會自動的處理簽名和驗簽,并進行證書自動更新
        CloseableHttpClient httpClient = builder.build();

        log.info("== getWxPayNoSignClient END ==");

        return httpClient;
    }
}

2.編寫鏡像文件 (最簡單的)

FROM  openjdk:8
ADD new-authority-system-service-0.0.1-SNAPSHOT.jar /app.jar
EXPOSE 8082
ENTRYPOINT ["java","-jar","/app.jar"]

3.將JAR包打成鏡像

docker build -t system-service:0.0.1 .  
image.png

注意:Dockerfile文件和jar文件在同一個目錄下


image.png

4.運行鏡像文件并進行目錄的掛載(資源文件)

 docker run --name system-service -d -p 8082:8082 -v /opt/config:/opt/configsystem-service:0.0.1
#/opt/config:/opt/config  之前在服務器上存放的資源文件路徑(可以自定義,但是要和圖一的路徑相對應)

完成!

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容