日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
SpringBoot3進(jìn)階用法,你學(xué)會(huì)了嗎?

一、簡(jiǎn)介

在上篇《SpringBoot3基礎(chǔ)》中已經(jīng)完成入門(mén)案例的開(kāi)發(fā)和測(cè)試,在這篇內(nèi)容中再來(lái)看看進(jìn)階功能的用法;

我們提供的服務(wù)有:網(wǎng)站制作、成都做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、達(dá)茂旗ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的達(dá)茂旗網(wǎng)站制作公司

主要涉及如下幾個(gè)功能點(diǎn):

調(diào)度任務(wù):在應(yīng)用中提供一定的輕量級(jí)的調(diào)度能力,比如方法按指定的定時(shí)規(guī)則執(zhí)行,或者異步執(zhí)行,從而完成相應(yīng)的代碼邏輯;

郵件發(fā)送:郵件作為消息體系中的渠道,是常用的功能;

應(yīng)用監(jiān)控:實(shí)時(shí)或定期監(jiān)控應(yīng)用的健康狀態(tài),以及各種關(guān)鍵的指標(biāo)信息;

切面編程:通過(guò)預(yù)編譯方式和運(yùn)行期動(dòng)態(tài)代理實(shí)現(xiàn)程序中部分功能統(tǒng)一維護(hù)的技術(shù),可以將業(yè)務(wù)流程中的部分邏輯解耦處理,提升可復(fù)用性;

二、工程搭建

1、工程結(jié)構(gòu)

圖片

2、依賴(lài)管理



    org.springframework.boot
    spring-boot-starter-web
    ${spring-boot.version}




    org.springframework.boot
    spring-boot-starter-actuator
    ${spring-boot.version}




    org.springframework.boot
    spring-boot-starter-aop
    ${spring-boot.version}




    org.springframework.boot
    spring-boot-starter-mail
    ${spring-boot.version}

這里再細(xì)致的查看一下各個(gè)功能的組件依賴(lài)體系,SpringBoot只是提供了強(qiáng)大的集成能力;

圖片

3、啟動(dòng)類(lèi)

注意在啟動(dòng)類(lèi)中使用注解開(kāi)啟了異步EnableAsync和調(diào)度EnableScheduling的能力;

@EnableAsync
@EnableScheduling
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

三、切面編程

1、定義注解

定義一個(gè)方法級(jí)的注解;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface DefAop {
    /**
     * 模塊描述
     */
    String modelDesc();

    /**
     * 其他信息
     */
    String otherInfo();
}

2、注解切面

在切面中使用Around環(huán)繞通知類(lèi)型,會(huì)攔截到DefAop注解標(biāo)記的方法,然后解析獲取各種信息,進(jìn)而嵌入自定義的流程邏輯;

@Component
@Aspect
public class LogicAop {

    private static final Logger logger = LoggerFactory.getLogger(LogicAop.class) ;
    
    /**
     * 切入點(diǎn)
     */
    @Pointcut("@annotation(com.boot.senior.aop.DefAop)")
    public void defAopPointCut() {

    }

    /**
     * 環(huán)繞切入
     */
    @Around("defAopPointCut()")
    public Object around (ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object result = null ;
        try{
            // 執(zhí)行方法
            result = proceedingJoinPoint.proceed();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            // 處理邏輯
            buildLogicAop(proceedingJoinPoint) ;
        }
        return result ;
    }

    /**
     * 構(gòu)建處理邏輯
     */
    private void buildLogicAop (ProceedingJoinPoint point){
        // 獲取方法
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method reqMethod = signature.getMethod();

        // 獲取注解
        DefAop defAop = reqMethod.getAnnotation(DefAop.class);
        String modelDesc = defAop.modelDesc() ;
        String otherInfo = defAop.otherInfo();
        logger.info("DefAop-modelDesc:{}",modelDesc);
        logger.info("DefAop-otherInfo:{}",otherInfo);
    }
}

四、調(diào)度任務(wù)

1、異步處理

1.1 方法定義

通過(guò)Async注解標(biāo)識(shí)兩個(gè)方法,方法在執(zhí)行時(shí)會(huì)休眠10秒,其中一個(gè)注解指定異步執(zhí)行使用asyncPool線(xiàn)程池;

@Service
public class AsyncService {

    private static final Logger log = LoggerFactory.getLogger(AsyncService.class);

    @Async
    public void asyncJob (){
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("async-job-01-end...");
    }

    @Async("asyncPool")
    public void asyncJobPool (){
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("async-job-02-end...");
    }
}

1.2 線(xiàn)程池

定義一個(gè)ThreadPoolTaskExecutor線(xiàn)程池對(duì)象;

@Configuration
public class PoolConfig {

    @Bean("asyncPool")
    public Executor asyncPool () {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 線(xiàn)程池命名前綴
        executor.setThreadNamePrefix("async-pool-");
        // 核心線(xiàn)程數(shù)5
        executor.setCorePoolSize(5);
        // 最大線(xiàn)程數(shù)10
        executor.setMaxPoolSize(10);
        // 緩沖執(zhí)行任務(wù)的隊(duì)列50
        executor.setQueueCapacity(50);
        // 線(xiàn)程的空閑時(shí)間60秒
        executor.setKeepAliveSeconds(60);
        // 線(xiàn)程池對(duì)拒絕任務(wù)的處理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 線(xiàn)程池關(guān)閉的時(shí)等待所有任務(wù)都完成再繼續(xù)銷(xiāo)毀其他的Bean
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // 設(shè)置線(xiàn)程池中任務(wù)的等待時(shí)間
        executor.setAwaitTerminationSeconds(300);
        return executor;
    }
}

1.3 輸出信息

從輸出的日志信息中可以發(fā)現(xiàn),兩個(gè)異步方法所使用的線(xiàn)程池不一樣,asyncJob采用默認(rèn)的cTaskExecutor線(xiàn)程池,asyncJobPool方法采用的是async-pool線(xiàn)程池;

[schedule-pool-1] c.boot.senior.schedule.ScheduleService   : async-job-02-end...
[cTaskExecutor-1] c.boot.senior.schedule.ScheduleService   : async-job-01-end...

2、調(diào)度任務(wù)

2.1 調(diào)度配置

通過(guò)實(shí)現(xiàn)SchedulingConfigurer接口,來(lái)修改調(diào)度任務(wù)的配置,這里重新定義任務(wù)執(zhí)行的線(xiàn)程池;

@Configuration
public class ScheduleConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
    }
}

2.2 調(diào)度方法

通過(guò)Scheduled注解來(lái)標(biāo)記方法,基于定時(shí)器的規(guī)則設(shè)定,來(lái)統(tǒng)一管理方法的執(zhí)行時(shí)間;

@Component
public class ScheduleJob {
    private static final Logger log = LoggerFactory.getLogger(ScheduleJob.class);

    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;

    /**
     * 上一次開(kāi)始執(zhí)行時(shí)間點(diǎn)之后10秒再執(zhí)行
     */
    @Scheduled(fixedRate = 10000)
    private void timerJob1(){
        log.info("timer-job-1:{}",format.format(new Date()));
    }

    /**
     * 上一次執(zhí)行完畢時(shí)間點(diǎn)之后10秒再執(zhí)行
     */
    @Scheduled(fixedDelay = 10000)
    private void timerJob2(){
        log.info("timer-job-2:{}",format.format(new Date()));
    }

    /**
     * Cron表達(dá)式:每30秒執(zhí)行一次
     */
    @Scheduled(cron = "0/30 * * * * ?")
    private void timerJob3(){
        log.info("timer-job-3:{}",format.format(new Date()));
    }
}

五、郵件發(fā)送

1、郵件配置

采用QQ郵箱來(lái)模擬郵件的發(fā)送方,需要先開(kāi)啟smtp郵件傳輸協(xié)議,在QQ郵箱的設(shè)置/賬戶(hù)路徑下,并且獲取相應(yīng)的授權(quán)碼,在項(xiàng)目的配置中使用;

spring:
  application:
    name: boot-senior
  # 郵件配置
  mail:
    host: smtp.qq.com
    port: 465
    protocol: smtps
    username: 郵箱賬號(hào)
    password: 郵箱授權(quán)碼
    properties:
      mail.smtp.ssl.enable: true

2、方法封裝

定義一個(gè)簡(jiǎn)單的郵件發(fā)送方法,并且可以添加附件,是常用的功能之一;另外也可以通過(guò)Html靜態(tài)頁(yè)渲染,再轉(zhuǎn)換為郵件內(nèi)容的方式;

@Service
public class SendMailService {

    @Value("${spring.mail.username}")
    private String userName ;

    @Resource
    private JavaMailSender sender;

    /**
     * 帶附件的郵件發(fā)送方法
     * @param toUsers 接收人
     * @param subject 主題
     * @param content 內(nèi)容
     * @param attachPath 附件地址
     * @return java.lang.String
     * @since 2023-07-10 17:03
     */
    public String sendMail (String[] toUsers,String subject,
                            String content,String attachPath) throws Exception {
        // MIME郵件類(lèi)
        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 郵件發(fā)送方From和接收方To
        helper.setFrom(userName);
        helper.setTo(toUsers);
        // 郵件主題和內(nèi)容
        helper.setSubject(subject);
        helper.setText(content);
        // 郵件中的附件
        File attachFile = ResourceUtils.getFile(attachPath);
        helper.addAttachment(attachFile.getName(), attachFile);
        // 執(zhí)行郵件發(fā)送命令
        sender.send(mimeMessage);
        return "send...mail...sus" ;
    }
}

測(cè)試結(jié)果

圖片

六、應(yīng)用監(jiān)控

1、監(jiān)控配置

在springboot的actuator組件中,可以通過(guò)提供的Rest接口,來(lái)獲取應(yīng)用的監(jiān)控信息;

# 應(yīng)用監(jiān)控配置
management:
  endpoints:
    web:
      exposure:
        # 打開(kāi)所有的監(jiān)控點(diǎn)
        include: "*"
      base-path: /monitor
  endpoint:
    health:
      enabled: true
      show-details: always
    beans:
      enabled: true
    shutdown:
      enabled: true

2、相關(guān)接口

2.1 Get類(lèi)型接口:主機(jī):端口/monitor/health,查看應(yīng)用的健康信息,三個(gè)核心指標(biāo):status狀態(tài),diskSpace磁盤(pán)空間,ping檢查;

{
    /* 狀態(tài)值 */
 "status": "UP",
 "components": {
     /* 磁盤(pán)空間 */
  "diskSpace": {
   "status": "UP",
   "details": {
    "total": 250685575168,
    "free": 112149811200,
    "threshold": 10485760,
    "path": "Path/butte-spring-parent/.",
    "exists": true
   }
  },
  /* Ping檢查 */
  "ping": {
   "status": "UP"
  }
 }
}

2.2 Get類(lèi)型接口:主機(jī):端口/monitor/beans,查看bean列表;

{
 "contexts": {
  "boot-senior": {
   "beans": {
    "asyncPool": {
     "scope": "singleton",
     "type": "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor",
     "resource": "class path resource [com/boot/senior/schedule/PoolConfig.class]"
    },
    "asyncService": {
     "scope": "singleton",
     "type": "com.boot.senior.schedule.AsyncService$$SpringCGLIB$$0"
    }
   }
  }
 }
}

2.3 Post類(lèi)型接口:主機(jī):端口/monitor/shutdown,關(guān)閉應(yīng)用程序;

{
    "message": "Shutting down, bye..."
}

七、參考源碼

文檔倉(cāng)庫(kù):
https://gitee.com/cicadasmile/butte-java-note

源碼倉(cāng)庫(kù):
https://gitee.com/cicadasmile/butte-spring-parent

網(wǎng)頁(yè)名稱(chēng):SpringBoot3進(jìn)階用法,你學(xué)會(huì)了嗎?
瀏覽路徑:http://www.5511xx.com/article/dpjoshe.html