品優購項目筆記(十一):SpringBoot和阿里大於

SpringBoot

介紹

springboot是spring組織生產的一個後端全棧框架(不包括頁面的技術)

作用:提倡零配置,不用整合框架結構,直接編寫業務代碼,給企業提高人員利用率,提高開發效率。

缺點:springboot只適合小項目使用

小例子(1)

一、創建maven的普通項目
二、引入依賴,並配置spring-boot-starter-parent爲父級項目

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

三、創建包結構,並在上層包中創建一個啓動類
在這裏插入圖片描述
四、啓動類編寫

package cn.itcast;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 啓動類
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        //啓動當前項目中的tomcat插件,運行當前項目
        SpringApplication.run(Application.class,args);
    }
}

五、寫一個controller進行測試

@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/hello")
    public String hello(){
        return "helloWorld";
    }
}

六、運行啓動類的main方法,即可啓動項目
在這裏插入圖片描述
在這裏插入圖片描述

小例子(2)

一、修改tomcat啓動端口
在resources目錄下,創建application.properties文件,並加上

server.port=8081

二、讀取properties文件屬性

注入屬性Environment,並使用getProperty("屬性名")即可

url=123456
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private Environment env;

    @RequestMapping("/hello")
    public String hello(){
        String url = env.getProperty("url");
        return "helloWorld"+url;
    }
}

三、熱部署
修改代碼後不用重啓tomcat

注意:idea不生效,eclipse生效

<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-devtools</artifactId>  
</dependency>  

springboot與activemq整合

一、添加座標

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

二、配置avtivemq服務器
application.properties中配置,如果不配,則使用默認的消息服務器

spring.activemq.broker-url=tcp://192.168.200.128:61616

三、編寫消息發送方

@RestController
@RequestMapping("/testJms")
public class TestJms {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @RequestMapping("/send")
    public void send(String text){
        //發送消息到消息服務器
        //參數1:指定隊列名稱
        //參數2:發送的內容
        jmsMessagingTemplate.convertAndSend("itcast", text);
    }

}

四、編寫消息的接收方

@Component
public class Consumer {

    /**
     * 定義監聽器,監聽從消息服務器發來的消息
     * destination是指從哪個隊列中接收消息
     * @param text
     */
    @JmsListener(destination="itcast")
    public void readMessage(String text){
        System.out.println("接收到消息:"+text);
    }

}

五、發送、接收消息
在這裏插入圖片描述
在這裏插入圖片描述

阿里大於

一、註冊阿里雲賬號,申請模板和簽名
二、添加子賬戶,使用子賬戶的id和密碼
三、發短信流程圖
在這裏插入圖片描述
四、添加發短信工具類

@Component
public class SmsUtil {
    //產品名稱:雲通信短信API產品,開發者無需替換
    static final String product = "Dysmsapi";
    //產品域名,開發者無需替換
    static final String domain = "dysmsapi.aliyuncs.com";

    @Autowired
    private Environment env;

    // TODO 此處需要替換成開發者自己的AK(在阿里雲訪問控制檯尋找)

    /**
     * 發送短信
     * @param mobile 手機號
     * @param template_code 模板號
     * @param sign_name 簽名
     * @param param 參數
     * @return
     * @throws ClientException
     */
    public SendSmsResponse sendSms(String mobile,String template_code,String sign_name,String param) throws ClientException {

        String accessKeyId =env.getProperty("accessKeyId");
        String accessKeySecret = env.getProperty("accessKeySecret");

        //可自助調整超時時間
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暫不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //組裝請求對象-具體描述見控制檯-文檔部分內容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待發送手機號
        request.setPhoneNumbers(mobile);
        //必填:短信簽名-可在短信控制檯中找到
        request.setSignName(sign_name);
        //必填:短信模板-可在短信控制檯中找到
        request.setTemplateCode(template_code);
        //可選:模板中的變量替換JSON串,如模板內容爲"親愛的${name},您的驗證碼爲${code}"時,此處的值爲
        request.setTemplateParam(param);

        //選填-上行短信擴展碼(無特殊需求用戶請忽略此字段)
        //request.setSmsUpExtendCode("90997");

        //可選:outId爲提供給業務方擴展字段,最終在短信回執消息中將此值帶回給調用者
        request.setOutId("yourOutId");

        //hint 此處可能會拋出異常,注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;
    }

}

五、使用消息服務器監聽消息

@Component
public class SmsListener {
    @Autowired
    private SmsUtil smsUtil;

    @JmsListener(destination="sms")
    public void sendSms(Map<String,String> map){
        try {
            SendSmsResponse response = smsUtil.sendSms(
                    map.get("mobile"),
                    map.get("template_code"),
                    map.get("sign_name"),
                    map.get("param")  );
            System.out.println("Code=" + response.getCode());
            System.out.println("Message=" + response.getMessage());
            System.out.println("RequestId=" + response.getRequestId());
            System.out.println("BizId=" + response.getBizId());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}

六、發送消息

分別發送手機號、模板code、簽名名稱、驗證碼

@RequestMapping("/sendSms")
    public void sendSms(){
        Map map=new HashMap();
        map.put("mobile", "手機號");
        map.put("template_code", "模板id");
        map.put("sign_name", "簽名名稱");
        map.put("param", "{\"code\":\"123456\"}");
        jmsMessagingTemplate.convertAndSend("sms",map);

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章