springboot学习

微服务阶段

一、SpringBoot

是什么

如何编写yaml

自动装配原理

集成web开发

集成数据库Druid

分布式开发:Dobbo+zookeeper

任务调度

springSecurity:Shiro

第一个SpringBoot程序

​ helloworld:打印当前时间

package com.zehui.sbdemo01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Classname HelloController
 * @Description TODO
 * @Date 2020/7/18 15:00
 * @Created by Nikla
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(value = "/time",method = RequestMethod.GET)
    public String sayHello(@RequestParam(required = false , defaultValue = "zehui") String name){

        ZonedDateTime dateTime = ZonedDateTime.now();
        String dataTimeString = dateTime.format( DateTimeFormatter.ofPattern("yyyy-M-d h:m:s"));
        return "hello, "+ name +" , now the time is "+ dataTimeString ;
    }
}

访问:http://localhost:8069/sbdemo1/hello/time?name=%E5%BC%A0%E4%B8%89

原理初探:

pom.xml的自动配置:

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.3.1.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>
  1. spring-boot-dependencies:核心依赖在父工程中
    1. 注意,如果是自己新增的maven工程,往往是没有则个父依赖的,所以要记得添加
  2. 我们在写或者引入springboot的时候,不需要指定版本,因为有这些版本仓库

启动器:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 启动器:说白了就是springboot的启动场景
  • 比如spring-boot-starter-web,就是自动导入web环境所有的依赖
  • springboot会将所有的功能场景,都变成一个个的启动器
  • 我们要试用什么功能,就只需要找到对应的启动器就可以了 starter

主程序

@SpringBootApplication  //标注这个类是一个springboot的应用
public class Sbdemo01Application {

   public static void main(String[] args) {
       //启动springboot
      SpringApplication.run(Sbdemo01Application.class, args);
   }

}

注解

@SpringBootConfiguration	//springboot 的配置
	@Configuration //spring配置类
		@Component	//这个说明也是一个spring组件

@EnableAutoConfiguration   //自动配置注解
	@AutoConfigurationPackage  //自动配置包
		@Import(AutoConfigurationPackages.Registrar.class)	//自动配置包注册
   	@Import(AutoConfigurationImportSelector.class)		//导入选择器



//获取所有的配置AutoConfigurationImportSelector
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
		List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
				getBeanClassLoader());
		Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
				+ "are using a custom packaging, make sure that file is correct.");
		return configurations;
	}

image-20200719094751026

//所有的资源加载到配置类中
Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
URL url = (URL)urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);

结论:springboot所有的自动配置都是在启动的时候扫描并加载,spring.factories所有的自动配置类都在这里面,但是不一定生效,要判断条件是否成立,只要导入对应的start,就有对应的启动器了,有了启动器,自动装配就会生效,然后配置成功

  1. 启动的时候,从类路径下加载META-INF/spring.factories获取指定的值
  2. 将这些自动配置的类导入容器,自动配置就会生效,帮我们做配置
  3. 以前需要配置的东西,springboot做了
  4. 整合javaEE,解决方案和自动配置的东西都在spring-boot-autoconfigure-2.3.1.RELEASE.jar这个包下面
  5. 把需要导入的组件,以类名的方式返回,这些组件就会被添加到容器

YML语法:

下面是application.yml的配置

server:
  servlet:
    context-path: /ks01
  ##port: 8088
muser:
  age: 18
  userName: 钟泽辉
## 这里可以配置 你需要使用的yml文件
spring:
  profiles:
    active: comp

下面是另外一个配置文件:application-comp.yml

### yaml配置bean属性,需要在bean加上一些注解
## @Component
## @ConfigurationProperties(prefix = "zuser")

zuser:
  userName: zehui
  age: 21
  ##list
  list-info:
    - zhong
    - ze
    - hui
    - list
  ## 这个是配置map的信息
  map-info: {m1: key1 , m2: key2}
server:
  port: 8077

多环境的yaml配置

springboot web开发

进行web开发

jar:webapp

自动装配?

springboot到底帮我们配置了什么东西,我们能不能进行修改,能修改哪些东西?能不能扩展?

  • xxxautoconfiguration。。向容器中自动配置组件
  • xxxproperties:自动配置类,装配配置文件中自定义的一些内容

要解决的问题

  • 导入静态资源。。。网页文件(js。css)
  • 首页
  • jsp,模板引擎thymeleaf
  • 增删改查
  • 拦截器
  • 国际化

静态资源

二、SpringCloud

微服务:

​ 是一种架构风格,要求我们开发应用的时候,这个应用必须构成一系列小服务的组合;可以通过http大的方式进行互通

SpringCloud入门

doRestFul风格:接口编写

Eureke:服务注册中心

负载均衡:Eibbon、Feign

HyStrix:服务容灾

Zuul路由网关

SpringCloudConfig:git集合

Linux

学习如何部署到Linux上面

三、遇到的问题

  1. 新增maven模块,为什么artifactId不能大写?

  2. maven父子工程错误
    报错信息:

    	<!-- maven父子工程install时候报错,原因某个模块中,relativePath需要像这样设置 -->
    	<parent>  
            <groupId>com.zehui</groupId>
            <artifactId>ksSpringBoot</artifactId>
            <version>1.0</version>
           <!-- 淦了,为什么没有设置,用默认值不行的了-->
            <relativePath>../pom.xml</relativePath>
        </parent>
    
  3. test

posted on 2020-11-02 00:26  长臂猿爱跳伞  阅读(129)  评论(0编辑  收藏  举报