微服务-SpringBoot

Charliexiu Lv2

约定大于配置

微服务🛎️

MVC三层架构 MVVM微服务架构


模块化 -> http 通信 (rpc)

高内聚 低耦合

SpringBoot

Spring .zip 安装


安装地址

https://start.spring.io/

1.8JDK 配置8.0版本java

按图片配置 zip压缩包

产出springboot 压缩包 (generate)


Lombok thymeleaf web 插件导入

官网导入三个依赖


pom.xml 解读


继承依赖(父项目) org.springframework.boot 远程项目包

1
2
3
4
5
6
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

项目依赖(dependencies)

web 依赖 : tomcat , dispatcherServlet, xml

实现Http 接口

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

打包(jar)插件

1
2
3
4
5
6
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>

配置端口resources/application.properties

1
server.port = 8081

banner.txt(自制横幅)

在 resources 目录下创建 banner.txt

    前往 Spring Boot banner在线生成工具

Spring Boot banner在线生成工具,制作下载英文banner.txt,修改替换banner.txt文字实现自定义,个性化启动banner-bootschool.net 这个网站自定义自己的banner


.properties 后缀

.properties 后缀文件皆为自定组件,自定配置,所以在引入自定义配置后,查找相关组件配置的配置版本和信息,搜索 .properties


注解

@SpringBootConfiguration

configuration 构造 配置

@SpringBootConfiguration SpringBoot配置
@Configuration spring配置类
@Component 组件

springboot在启动的时候,从类路径下/META-INF/ spring.factories获取指定的


@SpringBootApplication

run() 方法的理解

SpringBootApplication 启动器

  • constructor 构造

  • init (初始化) ·run() 方法的理解

    • 推断应用类型是否为WEB

    • 加载所有可用初始化配置

    • 设置所有可用程序监听器

    • 推断并设置main方法的定义类


@Autowired

yaml 匹配

yaml配置文件自定义随机值

1
2
3
4
5
xiu:
name: hah${random.uuid}
age: ${random.int}
hello: ${xiu.world:world}_xiu
happy: true

hello: ${xiu.world:world}_xiu 表示如果xiu.world存在 则使用xiu.world 不存在用 冒号 (:)后面的值代替

xiu 有参构造函数:

1
2
3
4
5
6
public xiu(String name, Integer age, Boolean happy, String hello) {
this.name = name;
this.age = age;
this.happy = happy;
this.hello = hello;
}

它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作

编写构造方法,toSting()

@ConfigurationProperties(prefix = “xiu”)

configurationProperties 注解绑定yaml

1
2
3
4
5
6
@Autowired
private xiu xu;
@Test
void contextLoads() {
System.out.println(xu);
}

@PropertySource

指定配置文件

@PropertySource( value = “classpath:application.properties”)

.properties 后缀文件的用法


@Validated

校验注解 (JSR303)

  • @NotNull(message = “id 不能为 null”)

SpringBoot 全局配置文件

application

application 名称不能变

  • 程序可以同时存在 yaml 和 properties 两个配置文件

  • 但必须都是 application

  • 同时,优先级 yaml > properties

两种表示形式:

application.yaml

application.properties

  • properties:
1
server.port = 8081
  • yaml:
1
2
server:
port: 8081

彩蛋(Easter egg🥚): YAML

YAML是”YAML Ain’t a Markup Language”(YAML不是一种标记语言)的递归缩写。

(YAML仍是一门配置语言)


快捷键

无参构造和有参构造,toString(), getter&setter

Alt + INSERT

搜索文件,库,类,方法

Ctrl + Shift + F


双向绑定 名称

first-name === firstName


多文档模块(yaml)

文章中的版本为 SpringBoot 2.7.0

1
2
spring:
profies: test

替换为:

1
2
3
4
spring:
config:
activate:
on-profile: test

paalication.yaml 文件下

用 — (三条短横线)分割每一个模块


SpringBoot Web

静态资源

放置位置
classPath:/
resources
static
public
优先级由上到下

表示 重写可以在 yaml / properties 文件下 重写 @Override

1
spring.mvc.static-path-pattern=/hello/, classpath:/xiu/

页面跳转


自定义图标


模板引擎(Thymeleaf)

Thymeleaf 类似 vue

可以绑定任何元素

表达方式 : th:元素名称

1
<div th:text="${msg}"></div>

Lombok

数据引用,有参,无参

@Data

@AllArgsConstructor

@NoArgsConstructor


  • 标题: 微服务-SpringBoot
  • 作者: Charliexiu
  • 创建于: 2023-04-08 22:35:17
  • 更新于: 2023-04-08 22:36:19
  • 链接: https://ccharlie-xiu.github.io/2023/04/08/微服务/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。