欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合阿里云短信服務(wù)的方法

 更新時間:2021年10月22日 09:58:20   作者:一生酷到底  
在實際項目中經(jīng)常有發(fā)送短信的功能,今天進說一下SpringBoot整合阿里云短信服務(wù)的相關(guān)知識,新建短信微服務(wù),編寫發(fā)送短信接口的方法文中給大家介紹的很詳細,需要的朋友參考下吧

一、新建短信微服務(wù)

1、在service模塊下創(chuàng)建子模塊service-msm

2.創(chuàng)建controller和service代碼

3.配置application.properties

# 服務(wù)端口
server.port=8006
# 服務(wù)名
spring.application.name=service-msm

# mysql數(shù)據(jù)庫連接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

spring.redis.host=192.168.44.131
spring.redis.port=6379
spring.redis.database= 0
spring.redis.timeout=1800000

spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
  #最大阻塞等待時間(負數(shù)表示沒限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
  #最小空閑


#返回json的全局時間格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

#配置mapper xml文件的路徑
mybatis-plus.mapper-locations=classpath:com/atguigu/cmsservice/mapper/xml/*.xml

#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

4、創(chuàng)建啟動類

@ComponentScan({"com.south"})
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消數(shù)據(jù)源自動配置
public class ServiceMsmApplication {
	public static void main(String[] args) {
		SpringApplication.run(ServiceMsmApplication.class, args);
	}
}

二、阿里云短信服務(wù)

幫助文檔:

https://help.aliyun.com/product/44282.html?spm=5176.10629532.0.0.38311cbeYzBm73

三、編寫發(fā)送短信接口

1.在service-msm的pom中引入依賴

  <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
        </dependency>
    </dependencies>

2.編寫controller,根據(jù)手機號發(fā)送短信

@CrossOrigin //跨域
public class MsmApiController {

    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping(value = "/send/{phone}")
    public R code(@PathVariable String phone) {
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)) return R.ok();

        code = RandomUtil.getFourBitRandom();
        Map<String,Object> param = new HashMap<>();
        param.put("code", code);
        boolean isSend = msmService.send(phone, "SMS_180051135", param);
        if(isSend) {
            redisTemplate.opsForValue().set(phone, code,5,TimeUnit.MINUTES);
            return R.ok();
        } else {
            return R.error().message("發(fā)送短信失敗");
        }
    }
}

3.編寫service

@Service
public class MsmServiceImpl implements MsmService {

    /**
     * 發(fā)送短信
     */
    public boolean send(String PhoneNumbers, String templateCode, Map<String,Object> param) {

        if(StringUtils.isEmpty(PhoneNumbers)) return false;

        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAIq6nIPY09VROj", "FQ7UcixT9wEqMv9F35nORPqKr8XkTF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", PhoneNumbers);
        request.putQueryParameter("SignName", "我的谷粒在線教育網(wǎng)站");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}

到此這篇關(guān)于SpringBoot整合阿里云短信服務(wù)的文章就介紹到這了,更多相關(guān)SpringBoot阿里云短信服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot 整合fluent mybatis的過程,看這篇夠了

    springboot 整合fluent mybatis的過程,看這篇夠了

    這篇文章主要介紹了springboot 整合fluent mybatis的過程,配置數(shù)據(jù)庫連接創(chuàng)建數(shù)據(jù)庫的詳細代碼,本文給大家介紹的非常詳細,需要的朋友可以參考下
    2021-08-08
  • Spring 整合多個配置文件的方法

    Spring 整合多個配置文件的方法

    在一些大型應(yīng)用中,可能存在多個配置文件,這篇文章給大家介紹了Spring 整合多個配置文件的方法,非常不錯,具有一定的參考借鑒價值,感興趣的朋友一起看看吧
    2018-05-05
  • Java實現(xiàn)隊列的N種方法

    Java實現(xiàn)隊列的N種方法

    在Java中,我們可以使用不同的方式來實現(xiàn)隊列,本文主要介紹了Java實現(xiàn)隊列的N種方法,具有一定的參考價值,感興趣的可以了解一下
    2023-10-10
  • SpringBoot設(shè)置Session失效時間的解決方案

    SpringBoot設(shè)置Session失效時間的解決方案

    當(dāng)過期時間是大于1分鐘的時候是沒有什么問題的,但是如果設(shè)置過期時間小于1分鐘,就會失效,這篇文章主要介紹了SpringBoot設(shè)置Session失效時間的解決方案,需要的朋友可以參考下
    2024-05-05
  • springboot的控制反轉(zhuǎn)和自動裝配示例代碼

    springboot的控制反轉(zhuǎn)和自動裝配示例代碼

    這篇文章主要介紹了springboot的控制反轉(zhuǎn)和自動裝配的相關(guān)知識,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06
  • SpringBoot+Mybatis plus實現(xiàn)多數(shù)據(jù)源整合的實踐

    SpringBoot+Mybatis plus實現(xiàn)多數(shù)據(jù)源整合的實踐

    本文主要介紹了SpringBoot+Mybatis plus實現(xiàn)多數(shù)據(jù)源整合的實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • SpringBoot實現(xiàn)ImportBeanDefinitionRegistrar動態(tài)注入

    SpringBoot實現(xiàn)ImportBeanDefinitionRegistrar動態(tài)注入

    在閱讀Spring Boot源碼時,看到Spring Boot中大量使用ImportBeanDefinitionRegistrar來實現(xiàn)Bean的動態(tài)注入,它是Spring中一個強大的擴展接口,本文就來詳細的介紹一下如何使用,感興趣的可以了解一下
    2024-02-02
  • Java線程池的幾種實現(xiàn)方法及常見問題解答

    Java線程池的幾種實現(xiàn)方法及常見問題解答

    下面小編就為大家?guī)硪黄狫ava線程池的幾種實現(xiàn)方法及常見問題解答。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • Java實現(xiàn)發(fā)送郵件并攜帶附件

    Java實現(xiàn)發(fā)送郵件并攜帶附件

    這篇文章主要為大家詳細介紹了Java實現(xiàn)發(fā)送郵件并攜帶附件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 淺談Maven的安裝及修改為阿里云下載依賴

    淺談Maven的安裝及修改為阿里云下載依賴

    下面小編就為大家?guī)硪黄獪\談Maven的安裝及修改為阿里云下載依賴。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05

最新評論