微服务,SpringBoot开发

1.微服务

特点:每个功能元素的服务都是一个可以替换的可独立升级的软件代码。

缺点:给部署和运维提供了难度

模块化,功能化

用户,支付,签到,用户多,要管理负载均衡问题

整体项目模块化,动态分配资源

微服务问题?网络不可靠,需要解决这4个问题,万变不离其宗,一通百通

  1. 服务多,客户端如何访问?--网关,服务路由

  2. 服务多,服务之间如何通信?rpc

  3. 服务多,如何治理? 服务注册与发现,高可用

  4. 服务多,服务挂了,如何办? 熔断机制,服务降级

    解决方案:springcloud是一种生态,来解决分布式架构的4个问题

    • 1.spring cloud netfix 解决方案:2018年宣布无限期停止维护
      - api 网关 zuul组件
      - feign httpclient http通信问题
      - 服务注册和发现 eureka
      - 熔断机制 hystrix
    • apache dubbo zookeper 第2套解决方案
      • api: 没有
      • dubbo RPC框架
      • 服务注册和发现 zookeeper
      • 熔断机制 没有
    • springcloud alibaba 一站式解决方案
    • 服务网格
      • 下一代微服务标准:server mesh
      • istio

2.springboot

可以快速的构建一个应用:

client(browser,mobile,Iot)---->API gateway--->breaker dashboard,config dashboard---->microservices1,microservices2--->service registry---->database,message brokers

特点:自动装配

软实力:聊天+举止+谈吐+见解 4378k---13k

面试,聊天,谈话:

你主导,别人参入,还是别人主导,你是参入着

www.bootschool.net/

resouces:

banner.txt

3.原理探讨

3.1pom.xml

  • spring-boot-dependencies:核心依赖在父工程中!
  • 我们在写或者引入一些springboot依赖的时候,不需要指定版本,因为有这些版本仓库

3.2启动器

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

3.3main主程序

  • 启动springboot应用

注解:

  • @SpringBootApplication 标注这个类是一个springboot应用
  • @SpringBootConfiguration springboot的配置
    • @Configuartion:spring 配置类
    • @Component: 说明是一个spring组件
  • @EnableAutoConfiguration 自动装配

run方法

1.启动时判断是Java应用还是javaweb应用

2.自动装配,configution

配置文件读取顺序

  • 1.file: ./config/
  • 2.file:./
  • 3.classpath:/config/
  • 4.classpath:/

yml

server:

port: 8081

spring:

profiles:

​ active: dev # ---


server:

port: 8082


server:

port: 8083

debug: true 查询那些配置生效了,那些没有生效

ocam录屏

4.自动装配原理精髓

    1. springboot启动会加载大量的自动配置类
    2. 我们看我们需要的功能书法在springboot 默认的自动配置类中,在就不需要手动配置
    3. 给容器中自动配置类添加组件的时候,会从properties类中获取默些属性,我们只需要在配置文件中指定这些属性的值即可
    4. xxxAutoConfiguration 自动配置类,给容器中添加组件
    5. xxxProperties:封装配置文件中的相关属性

5. SpringBoot WEB开发 1

1.静态资源

在springboot,我们可以使用以下方式处理静态资源

  • webjars : localhost:8080/webjar/1.js
  • resources/1.js static/1.js public/1.js
  • 优先级: resources > static > public

bootstrap后台模板

dashboard.html

bootcss.com

x-admin

2.首页如何定制:index.html, 图标 favicon.ico

public/index.html

public/favicon.ico

resources/index.html

static/index.html

thymeleaf 模板及语法

th:{}

3.页面国际花 i18n zh_CH,en_US

中英文按钮切换:定义一个组件LocaleResolver

将自己写的组件配置到spring容器@Bean

{}

4.登录+拦截器

修改springboot的默认配置

5.增删改查功能

6.404.html文件,500.html文件

7.前端

  • 模板:别人写好的,我们拿来改为自己的
  • 框架:组件:自己手动组合拼接,bootstrap,layui,emelmentui,semantic-ui,x-admin

总结

  • springboot是什么
  • 微服务是什么
  • helloworld
  • 探究源码,自动装配原理
  • 配置yaml
  • 多文档环境切换
  • 静态资源映射
  • Thymeleaf th:xxx th:{}
  • SpringBoot 如何扩展MVC Javaconfig
  • 如何修改springboot的默认配置
  • CRUD
  • 国际化 en_US,zh_CN
  • 拦截器
  • 定制首页,错误也

6.SpringBoot web开发2

  • JDBC: JDBCTemplate,serverTimezont=UTC

​ -- Druid 阿里数据库连接池,加入了日志监控

​ -- Hikari springboot 2.0以上默认的连接池,最快的数据源

​ - 增删改查

  • Mybatis

    1. 导入包

    2. 配置文件

    3. mybatis配置:mapper 接口 + mapper.xml 编写sql

    4. 编写sql

    5. service业务层调用dao层

    6. controller层调用service层

      • mybatis-spring-boot-strater
      • spring.datasource.usrname=root
      • spring.datasource.password=123456
      • spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
      • spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
      • pojo
      • mapper 接口(@mapper,@repository) + mapper.xml
  • Druid

  • Shiro: 安全 框架,认证,授权,功能权限,访问权限,菜单权限

  • Spring Security: 安全

    • group: com.xxx
    • atrifact: springboot-02-security
    • package:com.xx
  • 异步任务,邮件发送,定时任务

  • Swagger

  • Dubbo +Zookeeper

7.zookeeper:

  • 提供这提供服务
  • 消费者消费服务
posted @ 2024-04-14 23:08  大树2  阅读(11)  评论(0编辑  收藏  举报