阅读《Spring Boot2精髓》内容总结

第一章:例子

一.spring boot作用

  • 1:管理spring容器。
  • 2:管理第三方插件。
  • 3:提供很多默认服务(一些类的Starters)。

二.Hello world实例

  • 1.创建maven项目。

  • 2.引入parent配置。

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.14.RELEASE</version>
      <relativePath />
    </parent>
    
  • 3.增加starter依赖。

    <dependencies>
      <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
      </dependency>
    </dependencies>
    
  • 4.引入热部署依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
    </dependency>
    
  • 5.编写启动类。

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

第二章:基础

一.常用注解

  • 1.@Controller:声明一个mvc管理类 @RestController
  • 2.@RequestMapping:url路径映射
  • 3.@Service:声明一个业务处理类
  • 4.@Transactional
  • 5.@Repository:声明一个存储访问类
  • 6.@Component:声明此类是Spring管理的类,通常用于无法用其他注解描述的Spring管理类。
  • 7.@Configuration:声明配置类。通常与@Bean配合使用
  • 8.@PostConstruct:当bean被容器初始化后,会调用此注解的方法
  • 9.@PreDestory:容器被销毁之前会调用此注解方法。

二.切面

  • 1.@Aspect,声明一个切面类
  • 2.@Around,表达式声明,@within表示目标类型带有的注解

第三章:MVC

一.URL路径映射方法

1.ant路径表达式:*表示匹配任意字符,××表示统配任意路径,?匹配单个字符 二.HTTP method匹配

  • 1.GET,@GetMapping
  • 2.POST,@PostMapping
  • 3.HEAD,同GET,用于返回URL对应的元信息。
  • 4.PUT,同POST,@PutMapping
  • 5.DELETE,@DeleteMapping
  • 6.PATCH,类似PUT, @PatchMapping

三.consumes and produces

  • consumes:请求的HTTP头的Content-Type媒体类型。
  • produces:对应HTTP请求的Accept,可以用枚举类MediaType。

四.params and header匹配

  • 1.两个关键字用法基本一直,用来做请求条件匹配。

  • 2.条件:

     例子:@RequestMapping(value="rq1",params={"username","age=20","!pwd"})
    
    • a, 存在则通过;
    • b, 不存在则通过;
    • c, 参数等于某值则通过

五.跨域支持

Spring boot提供了对CORS的支持,可以实现addCorsMappings接口来添加特定配置

六.通用错误处理

Spring boot,Controller中抛出的异常默认交给/error来处理,应用程序可以将/error映射到一个特定的Controller中处理来代替默认实现,需要继承AbstractErrorController来统一处理。 AbstractErrorController提供了多个可以从request中获取错误信息的方法:

  • timestamp,错误发生的时间
  • staus,对应HTTP Status
  • error,错误信息
  • message,详细错误信息
  • exception,错误异常
  • path,请求路径
  • errors,@Validataed校验的时候,错误信息放在这个字段上。

第四章:View

详细参考:http://ibeetl.com

第五章:DB

一.配置数据源

  • 1.配置

     spring.datasource.url=
     spring.datasource.username=
     spring.datasource.password=
     spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
  • 2.配置类

     @Configuration
     public class DataSourceConfig{
     @Bean(name = "dataSource")
     public DataSource datasource(Environment env) {
        // 创建datasource对象并返回
     }
     }
    

第六章:JPA

一.集成

  • 1.加入依赖

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

2.修改配置

  • spring.jpa.hibernate.ddl-auto:是否自动建库,默认为none
  • spring.jpa.show-sql:是否自动打印sql,默认为false

第七章:配置

一.服务器配置

  • 1.端口: server.port=8080
  • 2.上下文默认为"/",server.servlet.context-path=/config
  • 3.地址:server.address=127.0.0.1
  • 4.回话时间:server.session.timeout=
  • 5.error路径:server.error.path=
  • 6.启动信息:可以在classpath中增加banner.txt,可以设置bannergif(png,jpg)
  • 7.日志配置 logging.file = my.log logging.path = /mnt/logs logging.level.root = info logging.levle.org = warn logging.level.cn.lomis = debug

二.读取应用配置

  • 1.Environment

    @Configuration
     public class EnvConfig{
       @AutoWired
       private Environment env;
       public int getServerPort(){
     	return env.getProperty("server.port", Integer.class);
       }
     }
    
  • 2.其他命令:user.dir, user.home, JAVA_HOME

  • 3.@Value

  • 4.@ConfigurationProperties

三.自动装配

  • 1.@Configuration and @Bean
  • 2.@ConditionalOnBean(Class):上下文中某类不存在才实例化
  • 3.@ConditionalOnMissingClass(Class):classpath中某类不存在才实例化
  • 4.@ConditionalOnProperty(name="", havingValue="true", mathIfMissing = true):根据配置读取并比较后决定是否生效

第八章:部署

一.部署方式

  • 1.jar
  • 2.war

二.多环境部署

  • 1.配置文件动态选择:application-{profile}.properties

     test:测试版本
     prod:正式版本
     pre-prod:预发版本
     demo1.0:演示版本
    
  • 2.@Profile

     @Profile("test")
     @Profile({"test", "prod"})
     @Profile({"test", "!prod"})
    

第九章:testing

一.JUnit

  • 1.注解:

     @Test,
     @BeforeClass,
     @Before,
     @After,
     @AfterClass 
    
  • 2.Assert:

      asserEquals,
      asserSame,
      asserTrue,
      asserFalse,
      asserNotNull,
      asserArrayEquals
    
  • 3.Suite:容器

二.Spring Boot单元测试

  • 1.加入依赖

     <dependency>
     	<groupId>org.springframework.boot</groupId>
     	<artifactId>spring-boot-starter-test</artifactId>
     </dependency>
    
  • 2.注解:

     @SpringBootTest
    

第十章:REST

一.RestTemplate

提供与http请求一样的方法实现

第十一章:MongoDB

一.集成

  • 1.加入依赖

    <dependency>
       <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-mongodb</artifactId>
     </dependency>
    
  • 2.修改配置

     spring.data.mongodb.uri=mongodb://127.0.0.1:27017/baike
    
  • 3.MongoTemplate

     insert,save
     findById/find
     updateFirst/updateMulti
     remove
    

第十二章:Redis

一.集成

  • 1.加入依赖

     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-redis</artifactId>
     </dependency>
    
  • 2.修改配置

     spring.redis.host=
     spring.redis.password=
     spring.redis.port=
     spring.reids.pool.max-active= #最大连接数,0表示无限制
    
  • 3.自定义序列化策略

     指定key的序列化策略为StringRedisSerializer
    

第十三章:ES

一.安装

下载解压即用,端口默认9200

二.基本概念

  • Index,文档(document)的集合,一个Index下包含多个Type,类似数据库。
  • Type,用来进一步组织document,类似数据库的表。
  • Document,文档是es能够存储和搜索的基本信息,类似数据库表的记录。
  • Node,集群中的一台es server。
  • 集群,同样集群名的节点将组合为es集群。
  • 分区(Shards)
  • 复制(Replicas)

三.操作

  • 1:添加文档,

     curl -XPOST 'localhost:9200/product/book/1?pretty' -H 'Content-Type':application/json' -d '{"name":"这里是名称","type":"food","postDate":"2018-06-17"}'
    

    index=product, type=book, key=1(主键可以是任意形式,如果未制定,es会自动生成), pretty输出格式可选操作

  • 2:根据主键查询,

     curl -XGET 'localhost:9200/product/book/1?pretty'
    
  • 3:根据主键更新,

     curl -XPUT 'localhost:9200/product/book/1?pretty' -H 'Content-Type':application/json' -d '{"name":"这里是名称","type":"food","postDate":"2018-06-17"}'
    
  • 4:部分更新,

     curl -XPUT 'localhost:9200/product/book/1/_update?pretty' -H 'Content-Type':application/json' -d '{"doc":{"name":"这里是名称"}}', _update声明,doc包含需要更新的文档片段
    
  • 5:根据主键删除,

     curl -XDELETE 'localhost:9200/product/book/1?pretty'
    
  • 6:搜索

     GET:  curl -G --data-urlencode 'q=name:名称' 'localhost:9200/product/book/$_search?pretty'
     POST: curl -XPOST 'localhost:9200/product/book/_search?pretty' -H 'Content-Type:application/json' -d '{"query":{"macth":{"name":"名称"}}}'
    

四.Spring Data Elastic

  • 1.加入依赖

     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
     </dependency>
    
  • 2.修改配置

     spring.data.elasticsearch.cluster-nodes=127.0.0.1:9200
    
  • 3.编写Entity

      @Document(indexName="product", type="book")
      public class BookEntity{
     @Id
         String id;
      }
    

第十四章:cache

一.Spring boot cache

  • 1.集成 加入依赖

     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-cache</artifactId>
     </dependency>
    
  • 2.修改配置

     spring.cache.type=Simple|Redis|Generic|none
     Simple:基于ConcurrentHashMap实现的缓存,一般开发用
     Redis:不解释,还需要引入Redis的依赖
     Generic:用户自定义,需要实现org.springframework.cache.CacheManager
     none:关闭缓存
    
  • 3.注解打开缓存

     @EnableCaching
    
  • 4.注解

     @Cacheable,作用于method,触发读取操作
     @CacheEvict,作用于method,触发失效操作
     @CachePut,作用于method,触发更新操作
     @Cache,作用于method,综合上面几种操作
     @CacheConfig,作用于类,配置公共设置
    
  • 5.key生成器(KeyGenerator类)

     原则:
       只有一个参数,参数就是key。
       没有参数,返回SimpleKey.EMPTY
       多个key,则返回包含多个参数的SimpleKey
    

第十五章:session

一.配置

  • 1.spring boot默认使用tomcat的session。

  • 2.修改session管理方式:修改配置项,spring.session.store-type=Redis|JDBC|Hazelcast|none

  • 3.选用Redis 引入依赖:

       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-redis</artifactId>
    
       <groupId>org.springframework.session</groupId>
       <artifactId>spring-session-data-redis</artifactId>
    
  • 4.nginx配置

     upstream backend {
     server 127.0.0.1:9001;
     server 127.0.0.1:9002
     }
    

第十六章:Zookeeper

一.安装

  • 1.解压即用
  • 2.配置文件./conf/zoo.cfg

二.Sping boot集成zk

  • 1.加入依赖

     <groupId>org.apache.curator</groupId>
     <artifactId>curator-recipes</artifactId>
     <version>版本选择需要根据安装的zk版本来选择</version>
    
  • 2.代码 创建一个Configuration类,创建一个CuratorFramework

  • 3.API

     create:创建节点
     delete:删除节点
     checkExists:检查节点是否存在
     getData:获取节点数据
     setData:设置节点数据
     getChildren:获取子节点
    
  • 4.分布式锁

     Curator提供了InterProcessMutex实现分布式锁,acquire方法获取锁,release释放锁(需要在finally代码里执行)
    
  • 5.服务注册 加入依赖

     <groupId>org.apache.curator</groupId>
     <artifactId>curator-x-discovery</artifactId>
     <version>版本需要跟着安装的zk版本选择</version>
    

第十七章:监控

一.安装Acutator

  • 1.加入依赖

     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
    
  • 2.配置打开监控

     endpoints.actuator.enabled=true
    

二.监控项

  • 1.health
  • 2.http跟踪
  • 3.loggers
  • 4.threaddump
  • 5.heapdump
  • 6.mapping
  • 7.bean
  • 8.metrics
  • 9.env
  • 10.configprops
  • 11.conditions

三.自定义监控

  • 1.继承HealthIndicator,实现health方法,返回一个Health对象。
  • 2.@Endpoint注解来自定义监控类,@ReadOperation显示监控指标,@WriteOpeartion动态显示监控指标

转载于:https://my.oschina.net/u/128964/blog/1839090

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值