创建springcloud应用
创建SpringCloud应用
- 创建工程:New > New Project
md
1. Spring lnitializr
2. Name:fine-example
3. Type:Maven
4. Group:cn.finemap
5. Artifact:fine-example
6. Package name:cn.finemap.example
7. JDK:17
8. JAVA:17
9. Packaging:Jar- 删除工程中的.mvn,mvnw,mvnw.cmd
- 添加.gitignore
cmd
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### 授权 ###
/src/main/resources/fine-parent.lic
### VS Code ###
.vscode/配置fine-parent依赖
依赖
- pom.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 继承fine-parent -->
<parent>
<groupId>cn.finemap</groupId>
<artifactId>fine-parent</artifactId>
<version>1.0-RELEASE</version>
</parent>
<groupId>cn.finemap</groupId>
<artifactId>fine-example</artifactId>
<version>1.0-RELEASE</version>
<name>fine-example</name>
<description>案例工程</description>
<dependencies>
<!--fine-cloud-->
<dependency>
<groupId>cn.finemap</groupId>
<artifactId>fine-cloud</artifactId>
<version>${fine.version}</version>
</dependency>
<!-- fine-commons -->
<dependency>
<groupId>cn.finemap</groupId>
<artifactId>fine-commons</artifactId>
<version>${fine.version}</version>
</dependency>
<!-- fine-sql -->
<dependency>
<groupId>cn.finemap</groupId>
<artifactId>fine-sql</artifactId>
<version>${fine.version}</version>
</dependency>
<!-- fine-logs -->
<dependency>
<groupId>cn.finemap</groupId>
<artifactId>fine-logs</artifactId>
<version>${fine.version}</version>
</dependency>
<!-- fine-auth -->
<dependency>
<groupId>cn.finemap</groupId>
<artifactId>fine-auth</artifactId>
<version>${fine.version}</version>
</dependency>
<!-- fine-doc -->
<dependency>
<groupId>cn.finemap</groupId>
<artifactId>fine-doc</artifactId>
<version>${fine.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>配置
- 配置优先级(越小优先级越高)
| 格式\配置 | Nacos | profile | include | application | bootstrap |
|---|---|---|---|---|---|
properties | 1 | 2 | 5 | 8 | 11 |
yml | - | 3 | 6 | 9 | 12 |
yaml | 1 | 4 | 7 | 10 | 13 |
- Nacos优先级(越小优先级越高)
${prefix}默认为spring.application.name的值
| dataId | 优先级 |
|---|---|
${prefix}-${spring.profiles.active}.${file-extension} | 1 |
${prefix}-${spring.profiles.include}.${file-extension} | 2 |
${prefix}.${file-extension} | 3 |
${prefix} | 4 |
- jar启动设置配置文件
| 名称 | 作用 |
|---|---|
-Dspring.cloud.bootstrap.location | 指定 Bootstrap 配置文件的位置 |
-Dspring.config.additional-location | 在标准的 application.properties 或 application.yml 文件之外添加额外的配置文件位置 |
- src/main/resources/application.yml
yaml
spring:
profiles:
active: dev #开发环境:dev
include: sql,doc,auth
application:
name: fine-example # 服务名称
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
username: fine
password: finemap@cn
url: jdbc:mysql://ip:port/fine_example?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true
data:
redis:
port: 6379
host: 127.0.0.1
password:
database: 0
timeout: 10s
lettuce:
pool:
# 连接池最大连接数
max-active: 200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
# 连接池中的最大空闲连接
max-idle: 10
# 连接池中的最小空闲连接
min-idle: 0
server:
port: 80 # 端口
servlet:
context-path: /fine-example # 服务名称
# springdoc-openapi项目配置
springdoc:
# openapi分组
group-configs:
- group: '权限管理'
paths-to-match: '/**'
packages-to-scan: cn.finemap.auth
- group: 'fine服务'
# 要匹配组的路径列表
paths-to-match: '/**'
# 要扫描的包列表(逗号分隔)
packages-to-scan: cn.finemap.example
# 配置文档信息
info:
title: 案例工程
description: 案例工程-API文档
version: 1.0.0
contact:
name: 凡图科技
url: http://www.finemap.cn
knife4j:
# 开启生产环境屏蔽
production: false- src/main/resources/bootstrap.yaml
yaml
spring:
profiles:
active: dev #开发环境:dev
application:
name: fine-example # 服务名称
cloud:
nacos:
server-addr: http://127.0.0.1:8848/ # Nacos地址
config:
file-extension: yaml # 文件后缀名配置启动类
java
@Slf4j
@SpringBootApplication
@ComponentScan(basePackages = {"cn.finemap"})
public class FineExampleApplication {
// xxx:启动程序(应用授权)
}配置Nacos管理
忽略`Nacos`管理
如果配置不存在,可直接读取application.yml配置
在部署中不建议这么做,不符合微服务、分布式概念
- 在默认群组
DEFAULT_GROUP和默认命名空间public中创建fine-example-dev.yaml,配置为application.yml内容 - 部署时注意群组和命名空间配置
授权
框架授权
- 使用
fine-parent框架需要添加授权文件
获取授权
- 将启动日志中的如下信息,并附上用户名和手机号,发给管理员,获取授权文件
----------------------------------------------------------
凡图框架: fine-parent 框架授权
项目名称: 项目名称
机器码: xx-xx-xx-xx-xx-xx
创建日期:
授权日期:
用户信息-用户名:附自己名称
用户信息-手机号:附自己手机号
----------------------------------------------------------- 将管理员提供的授权文件
fine-parent.lic(不能修改名称),存放到项目工程/src/main/resources/fine-parent.lic路径中
应用授权
- 使用
fine-example应用,用户需要申请添加授权文件,才可以部署启动
工程打包
- 程序打包:打成
jar包
install
- 程序启动:依赖jar环境(该启动程序在应用中存在)
bash
# 直接启动
java -jar xxx.jar
# 指定配置文件启动:application
java -Dfile.encoding=utf-8 -Dspring.config.location=.\conf\application.yaml -Dsun.java.command=xxx.jar -jar .\target\xxx.jar
# 指定配置文件启动:bootstrap
java -Dfile.encoding=utf-8 -Dspring.cloud.bootstrap.location=.\conf\bootstrap.yaml -Dsun.java.command=xxx.jar -jar .\target\xxx.jar- 创建SpringCloud应用及部署
参考官网:springcloud应用