文件上传
类型名和参数名都要注意,
@GetMapping("/download")
public void download(String name, HttpServletResponse reponse)
{
try {
FileInputStream fileInputStream=new FileInputStream(new File(basePath+name));
ServletOutputStream outputStream= reponse.getOutputStream();
reponse.setContentType("image/jpeg");
int len=0;
byte[]bytes=new byte[1024];
while((len=fileInputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
outputStream.flush();
}
outputStream.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public R<String> upload(MultipartFile file)
{
log.info(file.toString());
String originalFilename = file.getOriginalFilename();
String suffix=originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName= UUID.randomUUID().toString();
fileName+=suffix;
File dir=new File(basePath);
if(!dir.exists())
{
dir.mkdirs();
}
try {
file.transferTo(new File(basePath+fileName));
}catch(IOException ex)
{
ex.printStackTrace();
}
return R.success(fileName);
}
背景介绍
写操作使用主库,读操作使用从库
MySql主从复制
使用show slave status \G;可以整理好对应的信息。
此时Slave_IO_Running是No,
因此需要修改uuid
参考
不要忘记restart了
读写分离案例
项目实现读写分离
server:
port: 8080
spring:
application:
name: reggie_take_out
shardingsphere:
datasource:
names: master,slave
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.195.100:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
slave:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.195.101:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
masterslave:
load-balance-algorithm-type: round_robin
name: dataSource
master-data-source-name: master
slave-data-source-names: slave
props:
sql:
show: true
main:
allow-bean-definition-overriding: true
redis:
host: 192.168.195.100
port: 6379
password: 123456
database: 0
cache:
redis:
time-to-live: 1800000
mybatis-plus:
configuration:
#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: ASSIGN_ID
reggie:
path: D:\瑞吉外卖\img\
nginx
使用tree命令可直观看文件
配置环境变量便于操作。
nginx配置文件结构
nginx具体应用部署静态资源
反向代理
前后端分离开发
YAPI
@EnableSwagger2
@EnableKnife4j
@Bean
public Docket createRestApi()
{
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.itheima.reggie.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo()
{
return new ApiInfoBuilder()
.title("瑞吉外卖")
.version("1.0")
.description("瑞吉外卖接口文档")
.build();
}
}
项目部署