Advanced Development monomer

Distributed development: to provide the back-end node mouth + front-end to receive information rendering!

Monomer Development: to provide the back-end data + data acquisition front-end rendering!

Overall, there is no difference from the development process, the monomer and distributed nature, but technical and ideological slightly different!

The nature of distributed development: The network is not reliable! Communications between services, service collapse how to do, how to access the client, service registration and discovery!

 

Proficient Swagger

  Learn Swagger:

    • Known as the world's most popular API framework
    • RESTful API documentation generated automatically, and the corresponding codes
    • Direct interface to run the test, do not download postman
    • Support for multiple languages: (java, php, etc.)
    • Official website: https: //swagger.io/

  Integrated Swagger:

  1. Import dependence
  2. Write the configuration, open the annotation @ EnableSwagger2 // open swagger2
  3. test
 1 <dependency>
 2     <groupId>io.springfox</groupId>
 3     <artifactId>springfox-swagger2</artifactId>
 4     <version>2.7.0</version>
 5 </dependency>
 6 <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui -->
 7 <dependency>
 8     <groupId>com.github.xiaoymin</groupId>
 9     <artifactId>swagger-bootstrap-ui</artifactId>
10     <version>1.9.3</version>
11 </dependency>
12 
13 <!-- 原生ui -->
14 <!--<dependency>
15     <groupId>io.springfox</groupId>
16     <artifactId>springfox-swagger-ui</artifactId>
17     <version>2.7.0</version>
18 </dependency>-->

 

 1 package com.coding.config;
 2 
 3 import lombok.var;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.core.env.Environment;
 7 import org.springframework.core.env.Profiles;
 8 import springfox.documentation.RequestHandler;
 9 import springfox.documentation.builders.PathSelectors;
10 import springfox.documentation.builders.RequestHandlerSelectors;
11 importspringfox.documentation.service.ApiInfo;
 12 is  Import springfox.documentation.service.Contact;
 13 is  Import springfox.documentation.service.VendorExtension;
 14  Import springfox.documentation.spi.DocumentationType;
 15  Import springfox.documentation.spring.web.plugins. Docket No.;
 16  
. 17  Import of java.util.ArrayList;
 18 is  
. 19  @Configuration
 20 is  public  class SwaggerConfig {
 21 is  
22 is      // different testing needs to write a plurality of bean interfaces 
23 is      @Bean
 24      public Docket No. docket3 () {
 25 
26         return new Docket(DocumentationType.SWAGGER_2).groupName("group3");
27     }
28 
29     @Bean
30     public Docket docket2(){
31 
32         return new Docket(DocumentationType.SWAGGER_2).groupName("group2");
33     }
34 
35     @Bean
36     public Docket docket1(){
37 
38         return new Docket(DocumentationType.SWAGGER_2).groupName("group1");
39     }
40 
41     @Bean
42     public Docket docket(Environment environment){
43         Profiles of = Profiles.of("dev","test");
44         boolean b = environment.acceptsProfiles(of);
45 
46         return new Docket(DocumentationType.SWAGGER_2)
47                 .groupName("group")
48                 .apiInfo(apiInfo())
49                 .enable(b) // 判断
50                 .select()
51                 .apis(RequestHandlerSelectors.basePackage("com.coding.controller")) // 扫描此包下的接口
52                 .paths(PathSelectors.ant("/**")) // 测试此路径下的方法
53                 .build();
54     }
55 
56     Contact contact = new Contact("Vin","https://home.cnblogs.com/u/ShallowPen/","[email protected]");
57     /*String title,
58       String description,
59       String version,
60       String termsOfServiceUrl,
61       String contactName,
62       String license,
63       String licenseUrl*/
64     private ApiInfo apiInfo(){
65         return  new new ApiInfo (
 66                  "Information Document Interface" ,
 67                  "All the test address" ,
 68                  "v1.0" ,
 69                  "https://home.cnblogs.com/u/ShallowPen/" ,
 70                  Business Card,
 71                  "the Apache 2.0 " ,
 72                  " at http://www.apache.org/licenses/LICENSE-2.0 " ,
 73 is                  new new the ArrayList <> ()
 74          );
 75      }
 76  
77 }

 

Asynchronous tasks

  1. In business methods, plus @Async comment
  2. Open comments of support @EnableAsync // enables asynchronous processing
. 1  Package com.coding.service;
 2  
. 3  Import org.springframework.scheduling.annotation.Async;
 . 4  Import org.springframework.stereotype.Service;
 . 5  
. 6  Import java.util.concurrent.TimeUnit;
 . 7  
. 8  @Service
 . 9  public  class AsyncService {
 10  
. 11  
12 is      // added @Async asynchronous processing, second stage processing 
13 is      @Async // tell this is an asynchronous method Spring! Use the default thread pool, good efficiency! 
14      public  void Hello () {
 15          the try {
 16             TimeUnit.SECONDS.sleep (. 3); // Add test code delay 
. 17          } the catch (InterruptedException E) {
 18 is              e.printStackTrace ();
 . 19          }
 20 is          System.out.println ( "Data Processing ..." );
 21      }
 22 }

 

Regular tasks

  1. @Scheduled annotations on service code (cron = "0 * * * * 0-7")
  2. Open comments support @EnableScheduling // enable the scheduled task support

  Work will be used in regular tasks, send a log message every day, Spring also provides a corresponding support!

Cron Expressions

 

 

  •  * Indicates any time
  •  - representing the interval // 6-7
  •  L represents the last
  •  ? For days / weeks clash matches
  •  W represents a working day
  •  # 4 # 2 // day of the week, the second Wednesday

Common examples:

(1) * * * * 0/2? Represents every two seconds to perform tasks 
(1) 0/2 0 * * *? Means every two minutes to perform the task 
(1) 0021 *? Represents the 1st of every month of 2:00 to adjust the mandate 
? (2) 0 15 10 * MON-FRI said on Monday through Friday 10:15 am each day the job is executed 
(3) 0 15 10 6L 2002-2006 represents 2002--? 2006 per month The last execution for 10:15 am on Friday 
(4) * * 0 0 10,14,16? 10 am every day, 14:00, 4:00 
(5) * * 0 0/30 9-17? nine to five every half hour during working hours 
(6) 0 0 12? * WED represent every Wednesday 12:00 
(7) 0012 * *? 12:00 daily trigger 
(8) 01 510? * * 10:15 am every day trigger 
(9) 01510 * *? 10:15 every morning trigger 
(10) 01510 * *? triggered daily at 10:15 am 
(11) 01510 * *? 2005 2005 triggered 10:15 every morning 
(12) 0 14 * * *? triggered during a day at 2:00 pm to 2:59 pm every 1 minute 
(13) 0/5 0 14 * *? 5 per day during the 2:00 pm to 2:55 pm minute trigger 
(14) 0/5 0 * 14, 18 *? during 2:00 to 2:55 every afternoon and the next Trigger every 5 minutes during the afternoon 6:00 to 6:55 of
(15) 00-514 * *? Triggered every minute during 2:00 pm to 2:05 pm every day 
(16) 0 10,44 14? 3 WED March of each year and Wednesday's 2:10 pm 2 : 44 trigger 
? (17) 0 15 10 * MON-FRI 10:15 am Monday through Friday, trigger 
(18) 0151015 * 15th of each month 10:15 am trigger? 
(19) from 0 15 10 L * ? trigger the last day of each month at 10:15 am 
(20) 0 15 10? * 6L last Friday of each month at 10:15 am on the trigger 
(21) 0 15 10? * 6L 2002-2005 2002 years to 2005 the last Friday of each month at 10:15 am on the trigger 
(22) 01 510? * 6 # 3 the third Friday of each month at 10:15 am triggering

 

have a test

 1 package com.coding.service;
 2 
 3 import org.springframework.scheduling.annotation.Scheduled;
 4 import org.springframework.stereotype.Service;
 5 
 6 @Service
 7 public class ScheduledService {
 8     //                 秒 分 时 日 月 星期
 9     @Scheduled(cron = "0/2 * * * * ? ")
10     public void hello(){
11         System.out.println("hello。。。。。。。。");
12     }
13 }

 

Mail Task

  1. Import dependence maven
  2. Profiles
  3. Use test
1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-mail</artifactId>
4 </dependency>

 

Analysis source MailSenderAutoCon fi guration, configuration class found by introducing the assembly

1 @Bean
2 @ConditionalOnMissingBean(JavaMailSender.class)
3 JavaMailSenderImpl mailSender(MailProperties properties) {
4     JavaMailSenderImpl sender = new JavaMailSenderImpl();
5     applyProperties(properties, sender);
6     return sender;
7 }

 

Configuration properties file

1  # sender information (typically a company's account
 2  [email protected] 
 . 3  spring.mail.password = PHTQALPJHAHWMBSQ
 . 4  spring.mail.host smtp.163.com =
 . 5  
. 6  # QQ Comparison special, you need to configure ssl secure connection, if no other configuration of mailbox
 7 spring.mail.properties.mail.smtp.ssl.enable to true =

 

Write test code

 1 package com.coding;
 2 
 3 import org.junit.jupiter.api.Test;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.boot.test.context.SpringBootTest;
 6 import org.springframework.mail.SimpleMailMessage;
 7 import org.springframework.mail.javamail.JavaMailSenderImpl;
 8 import org.springframework.mail.javamail.MimeMessageHelper;
 9 
10 import javax.mail.MessagingException;
11 import javax.mail.internet.MimeMessage;
12 
13 @SpringBootTest
 14  class Day0325SwaggerApplicationTests {
 15  
16      @Autowired
 . 17      JavaMailSenderImpl javaMailSender;
 18 is  
. 19      @Test
 20 is      void sayMail () {
 21 is          // simply send a mail message 
22 is          SimpleMailMessage Message = new new SimpleMailMessage ();
 23 is          message.setSubject ( "not in class tomorrow "); // message subject 
24-          message.setText (" learn "); // message content 
25          message.setTo (" [email protected] "); // recipient 
26         message.setFrom ( "[email protected]"); // sender 
27          javaMailSender.send (Message);
 28      }
 29  
30      // complex test message 
31 is      @Test
 32      void sayMail2 () throws MessagingException {
 33 is          the MimeMessage MimeMessage = javaMailSender.createMimeMessage ();
 34 is          MimeMessageHelper Helper = new new MimeMessageHelper (MimeMessage, to true );
 35          helper.setSubject ( "test message complex" );
 36          helper.setText ( "<style = h1 of 'Color: red'> is red I </ h1> ",true);
37         helper.setTo("[email protected]");
38         helper.setFrom("[email protected]");
39         javaMailSender.send(mimeMessage);
40     }
41 }

 

Guess you like

Origin www.cnblogs.com/ShallowPen/p/12589022.html