首页
归档
留言
友链
广告合作
壁纸
更多
美女主播
Search
1
博瑞GE车机升级/降级
5,610 阅读
2
Mac打印机设置黑白打印
4,951 阅读
3
修改elementUI中el-table树形结构图标
4,895 阅读
4
Mac客户端添加腾讯企业邮箱方法
4,674 阅读
5
intelliJ Idea 2022.2.X破解
4,357 阅读
后端开发
HarmonyOS Next
Web前端
微信开发
开发辅助
App开发
数据库
随笔日记
登录
/
注册
Search
标签搜索
Spring Boot
Java
Vue
Spring Cloud
Mac
MyBatis
WordPress
MacOS
asp.net
Element UI
Nacos
.Net
Spring Cloud Alibaba
MySQL
Mybatis-Plus
Typecho
jQuery
Java Script
IntelliJ IDEA
微信小程序
Laughing
累计撰写
627
篇文章
累计收到
1,421
条评论
首页
栏目
后端开发
HarmonyOS Next
Web前端
微信开发
开发辅助
App开发
数据库
随笔日记
页面
归档
留言
友链
广告合作
壁纸
美女主播
搜索到
103
篇与
的结果
2023-03-05
SprintBoot切面+Redis防止前端重复提交
最近项目上遇到重复提交的情况,虽然前端对按钮进行了禁用,但是不知道是什么原因,后端仍然接收到了多个请求,因为是分布式系统,所以不能简单的使用lock,最终考虑决定使用redis实现。一、环境准备MySql:测试数据库Redis:使用Redis实现Another Redis Desktop Manager:跟踪Redis信息ApiFox:模拟请求,单线程循环及多线程循环Spring Boot:2.7.4二、准备测试数据及接口2.1、创建表创建一个最简单的用户表,只包含id、name两列create table User ( id int null, name varchar(200) null );2.2、创建接口2.2.1、配置依赖及数据库、Redis连接信息项目依赖<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cc.lisen</groupId> <artifactId>RepeatSubmit</artifactId> <version>0.0.1-SNAPSHOT</version> <name>RepeatSubmit</name> <description>RepeatSubmit</description> <properties> <java.version>8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.16</version> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project> yaml文件配置数据库及Redis连接信息spring: redis: host: 192.168.236.2 port: 6379 password: datasource: #使用阿里的Druid type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.236.2/TestRepeatSubmit?serverTimezone=UTC username: root password: root2.2.2、创建实体@Data @TableName("User") public class User { private Long id; private String name; }2.2.3、创建数据访问层public interface UserMapper extends BaseMapper<User> { }2.2.4、创建异常处理类@Data @NoArgsConstructor @AllArgsConstructor public class ResultRet<T> { private Integer code; private String msg; private T data; //成功码 public static final Integer SUCCESS_CODE = 200; //成功消息 public static final String SUCCESS_MSG = "SUCCESS"; //失败 public static final Integer ERROR_CODE = 201; public static final String ERROR_MSG = "系统异常,请联系管理员"; //没有权限的响应码 public static final Integer NO_AUTH_COOD = 999; //执行成功 public static <T> ResultRet<T> success(T data){ return new ResultRet<>(SUCCESS_CODE,SUCCESS_MSG,data); } //执行失败 public static <T> ResultRet failed(String msg){ msg = StringUtils.isEmpty(msg)? ERROR_MSG : msg; return new ResultRet(ERROR_CODE,msg,""); } //传入错误码的方法 public static <T> ResultRet failed(int code,String msg){ msg = StringUtils.isEmpty(msg)? ERROR_MSG : msg; return new ResultRet(code,msg,""); } //传入错误码的数据 public static <T> ResultRet failed(int code,String msg,T data){ msg = StringUtils.isEmpty(msg)? ERROR_MSG : msg; return new ResultRet(code,msg,data); } }2.2.5、简单的全局异常@Slf4j @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(value = Throwable.class) public ResultRet handleException(Throwable throwable){ log.error("错误",throwable); return ResultRet.failed(500, throwable.getCause().getMessage()); } }2.2.6、配置模拟接口模拟一个get请求的接口,用户新增用户,orm框架使用mybatis-plus,使用最简单的插入@RestController @RequestMapping("/user") public class UserController { @Resource private UserMapper userMapper; @GetMapping("/add") public ResultRet<User> add() { User user = new User(); user.setId(1L); user.setName("张三"); userMapper.insert(user); return ResultRet.success(user); } }以上配置完成后,当我们访问/user/add接口时,肯定访问几次,数据库就会重复插入多少信息。三、改造接口,防止重复提交改造的原理起始很简单,我们前端访问接口时,首先在头部都会携带token信息,我们通过切面,拦截请求,获取到token及请求的url,拼接后作为redis的key值,通过redis锁的方式写入key值,如果写入成功,设置一个过期时间,在有效期时间内,多次请求,先判断redis中是否有对应的key,如果有,抛出异常,禁止再次写入。3.1、配置RedisTemplate@Configuration public class RedisConfig { @Bean @SuppressWarnings(value = { "unchecked", "rawtypes" }) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); // 使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(serializer); // Hash的key也采用StringRedisSerializer的序列化方式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(serializer); template.afterPropertiesSet(); return template; } }3.2、增加Redis工具类@Component @Slf4j public class RedisUtils { @Resource private StringRedisTemplate stringRedisTemplate; /** * Redis分布式锁 * * @return 加锁成功返回true,否则返回false */ public boolean tryLock(String key, String value, long timeout) { Boolean isSuccess = stringRedisTemplate.opsForValue().setIfAbsent(key, value); //设置过期时间,防止死锁 if (Boolean.TRUE.equals(isSuccess)) { stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS); } return Boolean.TRUE.equals(isSuccess); } /** * Redis 分布式锁释放 * * @param key * @param value */ public void unLock(String key, String value) { try { String currentValue = stringRedisTemplate.opsForValue().get(key); if (StringUtils.isNotEmpty(currentValue) && StringUtils.equals(currentValue, value)) { stringRedisTemplate.opsForValue().getOperations().delete(key); } } catch (Exception e) { //这个是我的自定义异常,你可以删了 log.info("报错了"); } } }3.3、添加注解@Target(ElementType.METHOD) // 注解只能用于方法 @Retention(RetentionPolicy.RUNTIME) // 修饰注解的生命周期 @Documented public @interface RepeatSubmitAnnotation { /** * 防重复操作过期时间,默认1s */ long expireTime() default 1; }3.4、添加切面@Slf4j @Component @Aspect public class RepeatSubmitAspect { @Resource private RedisUtils redisUtils; /** * 定义切点 */ @Pointcut("@annotation(cc.lisen.repeatsubmit.annotation.RepeatSubmitAnnotation)") public void repeatSubmit() { } @Around("repeatSubmit()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Method method = ((MethodSignature) joinPoint.getSignature()).getMethod(); // 获取防重复提交注解 RepeatSubmitAnnotation annotation = method.getAnnotation(RepeatSubmitAnnotation.class); // 获取token当做key,小编这里是新后端项目获取不到哈,先写死 String token = request.getHeader("token"); if (StringUtils.isBlank(token)) { throw new RuntimeException("token不存在,请登录!"); } String url = request.getRequestURI(); /** * 通过前缀 + url + token 来生成redis上的 key * 可以在加上用户id,小编这里没办法获取,大家可以在项目中加上 */ String redisKey = "repeat_submit_key:" .concat(url) .concat(token); log.info("==========redisKey ====== {}", redisKey); boolean lock = redisUtils.tryLock(redisKey, redisKey, annotation.expireTime()); if (lock) { log.info("获取分布式锁成功"); try { //正常执行方法并返回 return joinPoint.proceed(); } catch (Throwable throwable) { throw new Throwable(throwable); } finally { //释放锁 // redisUtils.unLock(redisKey, redisKey); // System.out.println("释放分布式锁成功"); } } else { // 抛出异常 throw new Throwable("请勿重复提交"); } } }3.5、接口添加注解这里为了方便演示,我们把提交间隔时间设置为30s。@RestController @RequestMapping("/user") public class UserController { @Resource private UserMapper userMapper; @GetMapping("/add") @RepeatSubmitAnnotation(expireTime = 30L) public ResultRet<User> add() { User user = new User(); user.setId(1L); user.setName("张三"); userMapper.insert(user); return ResultRet.success(user); } }至此,我们所有的配置都完成了,接下来使用ApiFox模拟一下接口访问。3.6、模拟测试我们先把数据库及Redis清空(本来其实就是空的)配置好自动化测试接口3.6.1、单线程测试先模拟单线程操作,循环50次查看Redis,查看有一个key打开数据库,可以看到只成功插入了一条3.6.3、模拟多线程先把数据库清空,Redis等待过期后自动删除再次模拟,10个线程,循环10次此时查看数据库,仍然只有一条插入成功了
2023年03月05日
1,137 阅读
1 评论
0 点赞
2022-11-24
Spring Cloud Alibaba笔记修订版-第三章Nacos Discovery--服务治理
一、什么是服务治理服务治理是微服务架构中最核心最基本的模块,用于实现各个微服务的自动化注册与发现。服务注册:在服务治理框架中,都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务的详细信息。并在注册中心形成一张服务清单,服务注册中心需要以心跳的方式去监测清单中的服务是否可用,若不可用,需要再服务清单中剔除不可用的服务。服务发现:服务调用方向服务注册中心咨询服务,保宁获取所有服务的实例清单,实现对具体服务实例的访问。通过上面的图会发现,除了微服务,还有一个组件是服务注册中心,它是微服务架构中非常重要的一个组件,在微服务架构里起到了一个协调者的作用。注册中心一般包含以下几个功能:服务发现服务注册:保存服务提供者和服务调用者信息服务订阅:服务调用者订阅服务提供者的信息,注册中心向订阅者推送提供者信息服务配置配置订阅:服务提供者和服务调用者订阅微服务相关配置配置下发:主动将配置推送给服务提供者和服务调用者服务健康检测检测服务提供者的健康状况,如果发现异常,执行服务剔除常见的服务注册中心包括:Zookeeper、Eureka、Consul、Nacos。Nacos是Spring Cloud Alibaba组件之一,负责服务注册发现和服务配置,因为我们使用Spring Cloud Alibaba,所以这里只介绍Nacos的使用。二、Nacos简介Nacos致力于帮助您发现、配置和管理微服务。Nacos提供了一组简单易用的特性及,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。三、搭建Nacos环境注意使用Nacos之前,需要先配置好Java环境变量。我这里使用的服务器环境是Ubuntu 20.04,以下Nacos安装使用均以此为准,目前Nacos最新版本是2.1.2Nacos下载地址:Releases · alibaba/nacos (github.com)这里只介绍Nacos的基本使用,具体集群等高级用法,可以自行查找相关资料。3.1、下载Nacos下载nacos-server-2.1.2.tar.gz后,加压到任意位置。3.2、Nacos数据库文件运行Nacos之前,需要将Nacos数据库配置文件导入,我这里使用的是MySql,我直接导入上面Demo里面的数据库了。MySql的语句如下/* * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /******************************************/ /* 数据库全名 = nacos_config */ /* 表名称 = config_info */ /******************************************/ CREATE TABLE `config_info` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `data_id` varchar(255) NOT NULL COMMENT 'data_id', `group_id` varchar(255) DEFAULT NULL, `content` longtext NOT NULL COMMENT 'content', `md5` varchar(32) DEFAULT NULL COMMENT 'md5', `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间', `src_user` text COMMENT 'source user', `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip', `app_name` varchar(128) DEFAULT NULL, `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段', `c_desc` varchar(256) DEFAULT NULL, `c_use` varchar(64) DEFAULT NULL, `effect` varchar(64) DEFAULT NULL, `type` varchar(64) DEFAULT NULL, `c_schema` text, `encrypted_data_key` text NOT NULL COMMENT '秘钥', PRIMARY KEY (`id`), UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info'; /******************************************/ /* 数据库全名 = nacos_config */ /* 表名称 = config_info_aggr */ /******************************************/ CREATE TABLE `config_info_aggr` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `data_id` varchar(255) NOT NULL COMMENT 'data_id', `group_id` varchar(255) NOT NULL COMMENT 'group_id', `datum_id` varchar(255) NOT NULL COMMENT 'datum_id', `content` longtext NOT NULL COMMENT '内容', `gmt_modified` datetime NOT NULL COMMENT '修改时间', `app_name` varchar(128) DEFAULT NULL, `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段', PRIMARY KEY (`id`), UNIQUE KEY `uk_configinfoaggr_datagrouptenantdatum` (`data_id`,`group_id`,`tenant_id`,`datum_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='增加租户字段'; /******************************************/ /* 数据库全名 = nacos_config */ /* 表名称 = config_info_beta */ /******************************************/ CREATE TABLE `config_info_beta` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `data_id` varchar(255) NOT NULL COMMENT 'data_id', `group_id` varchar(128) NOT NULL COMMENT 'group_id', `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name', `content` longtext NOT NULL COMMENT 'content', `beta_ips` varchar(1024) DEFAULT NULL COMMENT 'betaIps', `md5` varchar(32) DEFAULT NULL COMMENT 'md5', `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间', `src_user` text COMMENT 'source user', `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip', `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段', `encrypted_data_key` text NOT NULL COMMENT '秘钥', PRIMARY KEY (`id`), UNIQUE KEY `uk_configinfobeta_datagrouptenant` (`data_id`,`group_id`,`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_beta'; /******************************************/ /* 数据库全名 = nacos_config */ /* 表名称 = config_info_tag */ /******************************************/ CREATE TABLE `config_info_tag` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `data_id` varchar(255) NOT NULL COMMENT 'data_id', `group_id` varchar(128) NOT NULL COMMENT 'group_id', `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id', `tag_id` varchar(128) NOT NULL COMMENT 'tag_id', `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name', `content` longtext NOT NULL COMMENT 'content', `md5` varchar(32) DEFAULT NULL COMMENT 'md5', `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间', `src_user` text COMMENT 'source user', `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip', PRIMARY KEY (`id`), UNIQUE KEY `uk_configinfotag_datagrouptenanttag` (`data_id`,`group_id`,`tenant_id`,`tag_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_tag'; /******************************************/ /* 数据库全名 = nacos_config */ /* 表名称 = config_tags_relation */ /******************************************/ CREATE TABLE `config_tags_relation` ( `id` bigint(20) NOT NULL COMMENT 'id', `tag_name` varchar(128) NOT NULL COMMENT 'tag_name', `tag_type` varchar(64) DEFAULT NULL COMMENT 'tag_type', `data_id` varchar(255) NOT NULL COMMENT 'data_id', `group_id` varchar(128) NOT NULL COMMENT 'group_id', `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id', `nid` bigint(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`nid`), UNIQUE KEY `uk_configtagrelation_configidtag` (`id`,`tag_name`,`tag_type`), KEY `idx_tenant_id` (`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_tag_relation'; /******************************************/ /* 数据库全名 = nacos_config */ /* 表名称 = group_capacity */ /******************************************/ CREATE TABLE `group_capacity` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID', `group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID,空字符表示整个集群', `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值', `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量', `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值', `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数,,0表示使用默认值', `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值', `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量', `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间', PRIMARY KEY (`id`), UNIQUE KEY `uk_group_id` (`group_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='集群、各Group容量信息表'; /******************************************/ /* 数据库全名 = nacos_config */ /* 表名称 = his_config_info */ /******************************************/ CREATE TABLE `his_config_info` ( `id` bigint(20) unsigned NOT NULL, `nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `data_id` varchar(255) NOT NULL, `group_id` varchar(128) NOT NULL, `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name', `content` longtext NOT NULL, `md5` varchar(32) DEFAULT NULL, `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `src_user` text, `src_ip` varchar(50) DEFAULT NULL, `op_type` char(10) DEFAULT NULL, `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段', `encrypted_data_key` text NOT NULL COMMENT '秘钥', PRIMARY KEY (`nid`), KEY `idx_gmt_create` (`gmt_create`), KEY `idx_gmt_modified` (`gmt_modified`), KEY `idx_did` (`data_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='多租户改造'; /******************************************/ /* 数据库全名 = nacos_config */ /* 表名称 = tenant_capacity */ /******************************************/ CREATE TABLE `tenant_capacity` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID', `tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID', `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值', `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量', `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值', `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数', `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值', `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量', `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间', PRIMARY KEY (`id`), UNIQUE KEY `uk_tenant_id` (`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='租户容量信息表'; CREATE TABLE `tenant_info` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `kp` varchar(128) NOT NULL COMMENT 'kp', `tenant_id` varchar(128) default '' COMMENT 'tenant_id', `tenant_name` varchar(128) default '' COMMENT 'tenant_name', `tenant_desc` varchar(256) DEFAULT NULL COMMENT 'tenant_desc', `create_source` varchar(32) DEFAULT NULL COMMENT 'create_source', `gmt_create` bigint(20) NOT NULL COMMENT '创建时间', `gmt_modified` bigint(20) NOT NULL COMMENT '修改时间', PRIMARY KEY (`id`), UNIQUE KEY `uk_tenant_info_kptenantid` (`kp`,`tenant_id`), KEY `idx_tenant_id` (`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tenant_info'; CREATE TABLE `users` ( `username` varchar(50) NOT NULL PRIMARY KEY, `password` varchar(500) NOT NULL, `enabled` boolean NOT NULL ); CREATE TABLE `roles` ( `username` varchar(50) NOT NULL, `role` varchar(50) NOT NULL, UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE ); CREATE TABLE `permissions` ( `role` varchar(50) NOT NULL, `resource` varchar(255) NOT NULL, `action` varchar(8) NOT NULL, UNIQUE INDEX `uk_role_permission` (`role`,`resource`,`action`) USING BTREE ); INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE); INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');3.3、配置Nacos在conf文件夹下有一个application.properties,我们需要配置里面的数据库连接信息把大概34行往下的位置,取消注释并根据自己情况进行配置### If use MySQL as datasource: spring.datasource.platform=mysql ### Count of DB: db.num=1 ### Connect URL of DB: db.url.0=jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user.0=root db.password.0=root3.4、运行Nacos进入bin文件夹执行./startup.sh -m standaloneNacos启动后,浏览器输入localhost:8848/nacos默认用户名及密码都是nacos四、将商品微服务注册到Nacos我们改造商品微服务,以便支持Nacos4.1、修改配置文件增加Nacos依赖<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2021.0.4.0</version> </dependency>4.2、主类上注解@EnableDiscoveryClient@SpringBootApplication @EntityScan({"cc.lisen.shop.common.entity"}) @EnableDiscoveryClient public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); } }4.3、配置文件添加nacos服务的地址spring: cloud: nacos: discovery: server-addr: 192.168.236.2:8848修改后配置文件如下server: port: 8081 spring: cloud: nacos: discovery: server-addr: 192.168.236.2:8848 application: name: service-user datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.236.2/shop?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=true username: root password: root jpa: hibernate: #指定为update,每次启动项目检测表结构有变化的时候会新增字段,表不存在时会新建,如果指定create,则每次启动项目都会清空数据并删除表,再新建 ddl-auto: update naming: #指定jpa的自动表生成策略,驼峰自动映射为下划线格式 implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl # 默认false,在日志里显示执行的sql语句 show-sql: true properties: hibernate: dialect: org.hibernate.dialect.MySQL5Dialect database: mysql database-platform: org.hibernate.dialect.MySQL5Dialect4.4、查看服务是否注册成功重新启动product微服务服务名就是我们配置文件配置的应用名称。五、将订单微服务注册到Nacos我们改造订单微服务,以便支持Nacos5.1、修改配置文件增加Nacos依赖<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2021.0.4.0</version> </dependency>5.2、主类上注解@EnableDiscoveryClient@SpringBootApplication @EntityScan({"cc.lisen.shop.common.entity"}) @EnableDiscoveryClient public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } } 5.3、配置文件添加nacos服务的地址spring: cloud: nacos: discovery: server-addr: 192.168.236.2:8848修改后配置文件如下server: port: 8091 spring: cloud: nacos: discovery: server-addr: 192.168.236.2:8848 application: name: service-user datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.236.2/shop?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=true username: root password: root jpa: hibernate: #指定为update,每次启动项目检测表结构有变化的时候会新增字段,表不存在时会新建,如果指定create,则每次启动项目都会清空数据并删除表,再新建 ddl-auto: update naming: #指定jpa的自动表生成策略,驼峰自动映射为下划线格式 implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl # 默认false,在日志里显示执行的sql语句 show-sql: true properties: hibernate: dialect: org.hibernate.dialect.MySQL5Dialect database: mysql database-platform: org.hibernate.dialect.MySQL5Dialect5.4、查看服务是否注册成功重新启动product微服务服务名就是我们配置文件配置的应用名称。5.5、改造订单接口,实现微服务调用package cc.lisen.shop.order.controller; import lombok.extern.slf4j.Slf4j; import cc.lisen.shop.common.entity.Product; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.util.List; @RestController @RequestMapping("order") @Slf4j public class OrderController { @Resource private RestTemplate restTemplate; @Resource private DiscoveryClient discoveryClient; @GetMapping("product/{id}") public Product order(@PathVariable("id") Integer productID) { List<ServiceInstance> serviceInstanceLList = discoveryClient.getInstances("service-product"); log.info("获取到服务:" + serviceInstanceLList.size()); //忽略下面可能导致的错误 ServiceInstance serviceInstance = serviceInstanceLList.get(0); return restTemplate.getForObject(serviceInstance.getUri() + "/product/1", Product.class); } }浏览器访问订单接口六、负载均衡通俗的讲,负载均衡就是将负载(工作任务、访问请求)进行分摊到多个操作单元(服务器、组件)上进行执行。根据负载均衡发生的位置不同,一般分为服务端负载均衡和客户端负载均衡。服务端负载均衡指的是发生在服务提供者一方,比如nginx负载均衡客户端负载均衡指的是发生在服务请求一方,也就是在发送请求之前已经选好了由那个实例处理请求。微服务调用关系中一般会选择客户端负载均衡。6.1、准备负载均衡环境为了实现负载均衡,我们需要准备至少两个服务,这里以商品服务为例。在上面的代码中,我们pom.xml文件都没有加入打包的插件,为了启动两个服务,我们需要现将程序打包成jar包。在shop-user、shop-product、shop-order三个模块都加入打包插件。 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>6.2、准备商品微服务为了区分调用的服务,我们在商品服务输出一下端口号。package cc.lisen.shop.product.controller; import lombok.extern.slf4j.Slf4j; import cc.lisen.shop.common.entity.Product; import cc.lisen.shop.product.service.ProductService; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @Slf4j public class ProductController { @Resource private ProductService productService; @Resource private Environment environment; @GetMapping("/product/{id}") public Product product(@PathVariable("id") Integer ID) { log.error("当前端口号:" + environment.getProperty("local.server.port")); return productService.findByID(ID); } } 然后将shop-product打包成jar包,通过一下命令,启动两个服务java -jar shop-product-1.0-SNAPSHOT.jar --server.port=8081 java -jar shop-product-1.0-SNAPSHOT.jar --server.port=8082此时打开Nacos,可以看到shop-product有两个服务此时,我们运行订单工程,访问接口http://127.0.0.1:8091/order/product/1多次刷新之后,可以用看到服务全部都打到了8081端口上。8082一条都没有,可见并没有实现负载均衡。6.3、基于Ribbon实现负载均衡Ribbon是Spring Cloud的一个组件,它可以让我们使用一个注解就能轻松搞定负载均衡。nacos 2021版本已经没有自带Ribbon的整合,所以需要引入另一个支持的jar包 loadbalancer早shop-order中引入loadbalancer实现Ribbon支持<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>在RestTemplate上注解上@LoadBalanced即可。package cc.lisen.shop.order.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class OrderConfiguration { @LoadBalanced @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } } 改造接口,通过服务名访问package cc.lisen.shop.order.controller; import lombok.extern.slf4j.Slf4j; import cc.lisen.shop.common.entity.Product; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.util.List; @RestController @RequestMapping("order") @Slf4j public class OrderController { @Resource RestTemplate restTemplate; @Resource DiscoveryClient discoveryClient; @GetMapping("product/{id}") public Product order(@PathVariable("id") Integer productID) { String serviceName = "service-product"; return restTemplate.getForObject("http://"+serviceName + "/product/1", Product.class); } }此时,我们运行订单工程,访问接口http://127.0.0.1:8091/order/product/1多次刷新之后,可以用看到服务平均打到了8081和8082端口。Ribbon默认的均衡策略是轮训。Ribbon自带的负载均衡策略我们可以通过修改配置文件改变默认的负载均衡策略。Ribbon 已经在最新的Spring Cloud 版本中被废弃,Spring Cloud Loadbalancer 是官方正式推出的一款新负载均衡利器,在未来,LoadBalancer 很有可能取代Ribbon的地位成为新一代的负载均衡器6.4、基于Feign实现负载均衡Feign是Spring Cloud提供的一个声明式的伪HTTP客户端,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口并添加注解即可。Nacos很好的兼容了Feign,Feign默认集成了Ribbon,所以在Nacos下使用Feign默认就实现了负载均衡的效果。在进行一下代码之前,记得先移除6.3添加的Ribbon相关的代码我们改造shop-order工程,实现Feign的使用6.4.1、引入依赖<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-loadbalancer</artifactId> </dependency>6.4.2、在主类上添加Feign注解package cc.lisen.shop.order; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EntityScan({"cc.lisen.shop.common.entity"}) @EnableDiscoveryClient @EnableFeignClients public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } } 6.4.3、添加一个servicepackage cc.lisen.shop.order.Service; import cc.lisen.shop.common.entity.Product; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @FeignClient("service-product") public interface ProductService { @GetMapping("/product/{id}") Product findByID(@PathVariable("id") Integer id); }Feign调用服务的地址就是@FeignClient+@GetMapping(获取其他映射)的地址6.4.4、改造Controllerpackage cc.lisen.shop.order.controller; import lombok.extern.slf4j.Slf4j; import cc.lisen.shop.common.entity.Product; import cc.lisen.shop.order.Service.ProductService; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.util.List; @RestController @RequestMapping("order") @Slf4j public class OrderController { @Resource ProductService productService; @GetMapping("product/{id}") public Product order(@PathVariable("id") Integer productID) { return productService.findByID(productID); } } 此时,我们运行订单工程,访问接口http://127.0.0.1:8091/order/product/1多次刷新之后,可以用看到服务平均打到了8081和8082端口。6.4.5、修改轮训策略以前的Ribbon有多种负载均衡策略但LoadBalancer貌似只提供了两种负载均衡器,不指定的时候默认用的是轮询。RandomLoadBalancer 随机RoundRobinLoadBalancer 轮询添加LoadBalance配置类package cc.lisen.shop.order.config; import com.alibaba.cloud.nacos.NacosDiscoveryProperties; import com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer; import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer; import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; import javax.annotation.Resource; public class MyLoadBalancerConfig { @Resource private NacosDiscoveryProperties nacosDiscoveryProperties; //自定义loadBlancer负载均衡策略 @Bean public ReactorServiceInstanceLoadBalancer reactorServiceInstanceLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory) { String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); //返回随机轮询负载均衡方式 return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name); //返回加权随机轮询负载均衡方式 //return new RoundRobinLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name); //nacos服务注册中心权重的负载均衡策略 // return new NacosLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name, nacosDiscoveryProperties); } } 启动类上配置服务使用的负载均衡策略package cc.lisen.shop.order; import cc.lisen.shop.order.config.MyLoadBalancerConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EntityScan({"cc.lisen.shop.common.entity"}) @EnableDiscoveryClient @EnableFeignClients @LoadBalancerClients(value = @LoadBalancerClient(name = "service-product",configuration = MyLoadBalancerConfig.class)) public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }此时,我们运行订单工程,访问接口http://127.0.0.1:8091/order/product/1多次刷新之后,可以用看到服务随机打到了8081和8082端口。
2022年11月24日
812 阅读
0 评论
0 点赞
2022-11-23
Spring Cloud Alibaba笔记修订版-第二章微服务环境搭建
因为第一章都是一些概念性的东西,包括系统架构的演变、微服务架构的介绍(服务调度、服务治理、服务容错、链路追踪等等),大家感兴趣的可以阅读原文,我们这里直接从第二章微服务环境搭建开始。本次使用的电商项目中的商品、订单、用户为案例进行讲解。一、技术选型JDK :1.8maven :3.8.6数据库 :MySQL 8.0.31持久层 :SpringData Jpa其他 :Spring Cloud Alibaba 2021.0.4.0,截止到目前最新版本开发工具 :IntelliJ idea 2022.2## 二、模块设计springcloud-alibaba:父工程shop-common:公共模块【实体类】shop-user:用户微服务【端口:807x】shop-product:商品微服务【端口:808x】`shop-order:订单微服务【端口:809x】2.1、创建父工程打开idea,创建maven工程,选择【New Project】,输入Name、GroupId、ArtifactId,选择存储目录,JDK选择本机安装的1.8版本。点击CREATE完成项目创建。springcloud-alibaba只是作为父工程,我们不会写任何代码,所以直接把src文件夹整体删掉。然后在pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.11</version> <relativePath/> </parent> <groupId>cc.lisen</groupId> <artifactId>springcloud-alibaba</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <java.version>1.8</java.version> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version> <spring-cloud.version>2021.0.4</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <!-- Spring Cloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Cloud Alibaba依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud.version}</version> </dependency> </dependencies> </dependencyManagement> </project>注意一下是Spring Cloud Alibaba与Spring Cloud及Spring Boot的版本对应关系,一定要选择对应的版本。Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version2021.0.4.0*Spring Cloud 2021.0.42.6.112021.0.1.0Spring Cloud 2021.0.12.6.32021.1Spring Cloud 2020.0.12.4.22.2、创建shop-common模块2.2.1、创建模块在工程上右键,选择NEW→Module名称输入shop-common创建模块2.2.2、添加依赖因为我们使用JPA,因此需要引入JPA的依赖,为了减少代码量,同时使用了lombok及fastjson序列化,所以shop-common添加依赖以下依赖 <dependencies> <!-- jpa依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- lombok依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- fastjson依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.19</version> </dependency> <!-- MySQL依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.31</version> </dependency> </dependencies>2.2.3、创建包目前shop-common模块中,src文件夹还是空的,为了规范,我们把shop-common的包定义为cc.lisen.shop.common2.2.4、创建实体我们统一把实体放到entity下,创建三个实体:用户(User)、商品(Product)、订单(Order)package cc.lisen.shop.common.entity; import lombok.Data; import javax.persistence.*; /** * 用户 */ @Data @Entity @Table(name = "shop_user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer ID; /** * 用户名 */ private String name; /** * 密码 */ private String password; /** * 手机号码 */ private String telephone; } package cc.lisen.shop.common.entity; import lombok.Data; import javax.persistence.*; /** * 商品 */ @Data @Entity @Table(name = "shop_product") public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer ID; /** * 商品名 */ private String name; /** * 价格 */ private Double price; /** * 库存 */ private Integer stock; } package cc.lisen.shop.common.entity; import lombok.Data; import javax.persistence.*; /** * 订单 */ @Data @Entity @Table(name = "shop_order") public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer ID; /** * 用户ID */ private Integer uid; /** * 商品ID */ private Integer pid; } 2.3、创建用户微服务2.3.1、创建模块参考2.2.1创建用户模块shop-user2.3.2、添加依赖添加以下依赖即可。 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>cc.lisen</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>2.3.3、创建包目前shop-user模块中,src文件夹还是空的,为了规范,我们把shop-user的包定义为cc.lisen.shop.user2.3.4、编写主类shop-user作为一个微服务,必须是可独立运行的,因此必须创建一个主类UserApplication.java。package cc.lisen.shop.product; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EntityScan({"cc.lisen.shop.common.entity"}) public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } }2.3.5、创建配置文件在resources文件夹添加application.yaml配置文件server: port: 8071 spring: application: name: service-user datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.236.2/shop?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=true username: root password: root jpa: hibernate: #指定为update,每次启动项目检测表结构有变化的时候会新增字段,表不存在时会新建,如果指定create,则每次启动项目都会清空数据并删除表,再新建 ddl-auto: update naming: #指定jpa的自动表生成策略,驼峰自动映射为下划线格式 implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl # 默认false,在日志里显示执行的sql语句 show-sql: true properties: hibernate: dialect: org.hibernate.dialect.MySQL5Dialect database: mysql database-platform: org.hibernate.dialect.MySQL5Dialect2.3.6、验证设置完成后,shop-user整个结构如下启用UserApplication,此时控制台输入一下内容,说明启动成功2.4、创建商品微服务2.4.1、创建模块参考2.2.1创建商品模块shop-product2.4.2、添加依赖添加以下依赖即可。 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>cc.lisen</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>2.4.3、创建包目前shop-product模块中,src文件夹还是空的,为了规范,我们把shop-product的包定义为cc.lisen.shop.product2.4.4、编写主类shop-product作为一个微服务,必须是可独立运行的,因此必须创建一个主类ProductApplication.java。package cc.lisen.shop.user; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EntityScan({"cc.lisen.shop.common.entity"}) public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); } }2.4.5、创建配置文件在resources文件夹添加application.yaml配置文件server: port: 8081 spring: application: name: service-product datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.236.2/shop?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=true username: root password: root jpa: hibernate: #指定为update,每次启动项目检测表结构有变化的时候会新增字段,表不存在时会新建,如果指定create,则每次启动项目都会清空数据并删除表,再新建 ddl-auto: update naming: #指定jpa的自动表生成策略,驼峰自动映射为下划线格式 implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl # 默认false,在日志里显示执行的sql语句 show-sql: true properties: hibernate: dialect: org.hibernate.dialect.MySQL5Dialect database: mysql database-platform: org.hibernate.dialect.MySQL5Dialect2.4.6、验证设置完成后,shop-product整个结构如下启用ProductApplication,此时控制台输入一下内容,说明启动成功2.5、创建订单微服务2.5.1、创建模块参考2.2.1创建商品模块shop-order2.5.2、添加依赖添加以下依赖即可。 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>cc.lisen</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>2.5.3、创建包目前shop-order模块中,src文件夹还是空的,为了规范,我们把shop-product的包定义为cc.lisen.shop.order2.5.4、编写主类shop-order作为一个微服务,必须是可独立运行的,因此必须创建一个主类OrderApplication.java。package cc.lisen.shop.order; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EntityScan({"cc.lisen.shop.common.entity"}) public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }2.5.5、创建配置文件在resources文件夹添加application.yaml配置文件server: port: 8091 spring: application: name: service-order datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.236.2/shop?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=true username: root password: root jpa: hibernate: #指定为update,每次启动项目检测表结构有变化的时候会新增字段,表不存在时会新建,如果指定create,则每次启动项目都会清空数据并删除表,再新建 ddl-auto: update naming: #指定jpa的自动表生成策略,驼峰自动映射为下划线格式 implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl # 默认false,在日志里显示执行的sql语句 show-sql: true properties: hibernate: dialect: org.hibernate.dialect.MySQL5Dialect database: mysql database-platform: org.hibernate.dialect.MySQL5Dialect2.5.6、验证设置完成后,shop-order整个结构如下启用OrderApplication,此时控制台输入一下内容,说明启动成功三、原始服务调用3.1、创建测试数据为了方便后续演示,我们现在用户及商品表中创建几条测试数据。INSERT INTO shop_user (id, name, password, telephone) VALUES (1, '张三', '123456', '13333333333'); INSERT INTO shop_user (id, name, password, telephone) VALUES (2, '李四', '123456', '14444444444'); INSERT INTO shop_product (id, name, price, stock) VALUES (1, '小米', 1000, 5000); INSERT INTO shop_product (id, name, price, stock) VALUES (2, '华为', 2000, 5000); INSERT INTO shop_product (id, name, price, stock) VALUES (3, '苹果', 3000, 5000); INSERT INTO shop_product (id, name, price, stock) VALUES (4, '一加', 4000, 5000); 3.2、商品提供查询服务3.2.1、数据访问层创建商品数据访问层一个空的接口,JPA默认会提供查询方法package cc.lisen.shop.product.dao; import cc.lisen.shop.common.entity.Product; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; /** * 商品数据访问层 */ @Repository public interface ProductDao extends JpaRepository<Product, Integer> { } 3.2.2、服务层创建ProductService及接口实现类package cc.lisen.shop.product.service; import cc.lisen.shop.common.entity.Product; public interface ProductService { /** * 根据ID查询商品 * * @param ID 商品ID * @return 商品 */ Product findByID(Integer ID); } package cc.lisen.shop.product.service.impl; import cc.lisen.shop.common.entity.Product; import cc.lisen.shop.product.dao.ProductDao; import cc.lisen.shop.product.service.ProductService; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class ProductServiceImpl implements ProductService { @Resource private ProductDao productDao; /** * 根据ID查询商品 * * @param ID 商品ID * @return 商品 */ @Override public Product findByID(Integer ID) { return productDao.findById(ID).get(); } } 3.2.3、创建Rest接口package cc.lisen.shop.product.controller; import lombok.extern.slf4j.Slf4j; import cc.lisen.shop.common.entity.Product; import cc.lisen.shop.product.service.ProductService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @Slf4j public class ProductController { @Resource private ProductService productService; @GetMapping("/product/{id}") public Product product(@PathVariable("id") Integer ID) { return productService.findByID(ID); } } 运行程序,现在浏览器测试一下3.3、订单查询商品3.3.1、创建配置文件我们这里通过RestTemplate访问商品,因此增加一个配置文件。package cc.lisen.shop.order.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class OrderConfiguration { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } } 3.3.1、创建Rest接口这里简单模拟一下订单查询商品信息,只简单创建一个订单的controllerpackage cc.lisen.shop.order.controller; import lombok.extern.slf4j.Slf4j; import cc.lisen.shop.common.entity.Product; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController @RequestMapping("order") @Slf4j public class OrderController { @Resource private RestTemplate restTemplate; @GetMapping("product/{id}") public Product order(@PathVariable("id") Integer productID){ return restTemplate.getForObject("http://localhost:8081/product/1", Product.class); } } 简单测试一下订单接口接口也是能够正常访问的。四、传统服务调用的一些弊端通过上面的代码,我们虽然实现了接口的调用,但是我们把服务提供者的网络地址(ip、端口)等硬编码到了代码中,这种做法存在很多问题:一旦服务提供者地址发生变化,就需要手工修改代码(当然可以做成配置或者放到配置文件中)一旦是多个服务提供者,无法实现负载均衡功能一旦服务变得越来越多,人工维护调用关系变得非常困难为了解决上述问题,我们就引出了微服务架构中的服务治理,也就是通过注册中心等方式,实现服务的自动注册与发现。
2022年11月23日
939 阅读
0 评论
1 点赞
2022-11-22
Spring Cloud Alibaba笔记修订版-序言
一、为什么会有Spring Cloud Alibaba笔记修订版一系列的文章1、加强个人学习很无意间看到的“一本书”,之所以打上双引号,是一位这不是完整意义上的一本书,其实如标题说的一样,是一本笔记。【笔记】其实更像是个人学习的一个总结,所以书中内容可能会针对个人有深有浅,对于旁人来说,就是左一榔头、有一棒槌,云里雾里,不知所踪。为了个人的学习加深,也为了将别人的东西消化成自己的东西,因此在阅读这本书的时候,有意的做一下记录,形成一套完整的适合大多数人学习的Spring Cloud Alibaba笔记。2、完善代码书中错误【马虎】的代码比较多其实从一开始阅读,我就发发现笔记中有不少“错误”或者说是马虎的粘贴导致的错误,比如用户的微服务叫service-product,其实这是商品的微服务。笔记内代码相对比较古老,好多依赖都已经存在已知的漏洞Spring Cloud Alibaba笔记写的比较早,所以使用的Spring Cloud Alibaba还是比较早的版本,这并不是说是啥问题,只是随着时间的发展,新的版本替代老的版本是必然的趋势,因此我在阅读这个笔记的时候,特意验证了Spring Cloud Alibaba最新的版本,并且基于最新的版本进行代码的验证。二、Spring Cloud Alibaba笔记的改动点勘误其实就是对不合适或者马虎粘贴的代码进行一些改正。升级版本Spring Cloud Alibaba 升级到2021.0.1.0Spring Boot升级到2.6.3Spring Cloud升级到2021.0.1
2022年11月22日
977 阅读
0 评论
0 点赞
2022-11-14
微信小程序获取用户头像后上传到七牛云
一、事情起因【油耗笔记OilNote】小程序好久没有升级了,最近打算对代码进行一些优化,但是新版本突然发现无法获取到用户微信头像及微信昵称了。查阅官方文档才知道,官方有对getUserProfile接口进行调整了。自 2022 年 10 月 25 日 24 时后(以下统称 “生效期” ),用户头像昵称获取规则将进行如下调整:自生效期起,小程序 wx.getUserProfile 接口将被收回:生效期后发布的小程序新版本,通过 wx.getUserProfile 接口获取用户头像将统一返回默认灰色头像,昵称将统一返回 “微信用户”。生效期前发布的小程序版本不受影响,但如果要进行版本更新则需要进行适配。自生效期起,插件通过 wx.getUserInfo 接口获取用户昵称头像将被收回:生效期后发布的插件新版本,通过 wx.getUserInfo 接口获取用户头像将统一返回默认灰色头像,昵称将统一返回 “微信用户”。生效期前发布的插件版本不受影响,但如果要进行版本更新则需要进行适配。通过 wx.login 与 wx.getUserInfo 接口获取 openId、unionId 能力不受影响。「头像昵称填写能力」支持获取用户头像昵称:如业务需获取用户头像昵称,可以使用「头像昵称填写能力」(基础库 2.21.2 版本开始支持,覆盖iOS与安卓微信 8.0.16 以上版本),具体实践可见下方《最佳实践》。小程序 wx.getUserProfile 与插件 wx.getUserInfo 接口兼容基础库 2.27.1 以下版本的头像昵称获取需求:对于来自低版本的基础库与微信客户端的访问,小程序通过 wx.getUserProfile 接口将正常返回用户头像昵称,插件通过 wx.getUserInfo 接口将正常返回用户头像昵称,开发者可继续使用以上能力做向下兼容。现在只要是发布的新版本,默认都需要调整,不然就显示下面灰色头像,已经发布的版本不受影响。既然官方调整了,那么我们也只有被动接受的份。二、油耗笔记的开发框架油耗笔记OilNote不是直接使用微信开发者工具开发的,而是使用UniApp开发的,后端是SpringBoot。三、改进思路3.1、获取用户头像由于官方指导意见是,使用button组件 open-type 的值设置为 chooseAvatar,当用户选择需要使用的头像之后,可以通过 bindchooseavatar 事件回调获取到头像信息的临时路径。从官方的指导我们可以看到,微信并没有给我们返回一个具体的路径,只是返回了一个临时的路径,因此我们就必须自己获取到这个临时的文件,然后存储起来。3.2、使用七牛云由于使用的腾讯云的低配服务器,带宽、存储都比较捉襟见肘,所以我打算把头像都存储到七牛云上,既能减轻带宽压力也能节省服务器空间。四、具体改进4.1、UniApp页面改进当用户通过微信登录时,此时获取用户头像信息,如果头像存在,登录之后跳转到首页,否则跳转到个人信息界面,让用户维护头像及昵称,此方法适用于新用户,同时也适用于老用户重新登录。//微信授权登录 getUserInfo(e) { let that = this; var p = this.getSetting(); p.then(function(isAuth) { console.log('是否已经授权', isAuth); if (isAuth) { console.log('用户信息,加密数据', e); //eData 包括//微信头像//微信名称 还有加密的数据. // let eData = JSON.parse(e.detail.rawData); uni.getUserProfile({ desc: 'Wexin', // 这个参数是必须的 success: function(infoRes) { //接下来就是访问接口. that.$request( 'wechat/authCode2Session?code=' + that.weChatCode, 'POST' ).then(function(res) { if (res.code == 200) { //将接口返回的数据保存在全局变量中. let userInfo = {} // 用户id userInfo.id = res.data.id userInfo.username = res.data.username userInfo.tel = res.data.tel userInfo.email = res.data.email userInfo.wechatOpenId = res.data.wechatOpenId userInfo.nickName = res.data.nickName userInfo.avatarUrl = res.data.avatarUrl ? res.data .avatarUrl : infoRes.userInfo.avatarUrl userInfo.gender = infoRes.userInfo.gender userInfo.password = '' if (!userInfo.province) { userInfo.province = uni.getStorageSync('province') } if (!userInfo.city) { userInfo.city = uni.getStorageSync('city') } uni.setStorageSync('userInfo', userInfo); if (!res.data.avatarUrl) { //没有头像时,跳转到用户信息维护界面 uni.redirectTo({ url: '/pages/profile/profile' }) } else { uni.redirectTo({ url: '/pages/index/index' }) } } }, function(err) { uni.showToast({ title: '授权登录失败!', mask: true, icon: 'none' }) } ) } }); } else { uni.showToast({ title: '授权失败,请确认授权已开启', mask: true, icon: 'none' }) } }); },4.1.1、登录界面改造4.1.2、个人信息界面改造油耗笔记之前有一个【个人信息】页面,因此我打算把修改头像的功能发放到里面。在合适的位置放入选择头像的按钮<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"> <view class="cu-avatar xl round margin-center" :style="{backgroundImage:'url('+userInfo.avatarUrl+')'}"></view> </button>增加回调方法用户选择微信头像之后,会回调chooseavatar方法,因此我们增加一个chooseavatar用于用户选择头像之后上传到服务器(进一步上传到七牛)//选择头像回调 onChooseAvatar(e) { const that = this; this.$set(this.userInfo, "avatarUrl", e.detail.avatarUrl); uni.uploadFile({ url: operate.api + 'user/uploadAvatar/', //上传接口 header: { token: that.userInfo.id ? that.userInfo.id : '', }, formData: { 'userInfo': JSON.stringify(that.userInfo) }, filePath: e.detail.avatarUrl, name: 'file', success: (uploadFileRes) => { uni.hideLoading(); const back = JSON.parse(uploadFileRes.data); if (back.code == 200) { that.$set(that.userInfo, 'avatarUrl', back.data.avatarUrl) } else { uni.showToast(back.msg) } }, fail: (error) => { uni.hideLoading(); uni.showToast("图片上传失败,请联系开发!") }, complete: function() { uni.hideLoading(); } }); }4.2、后端改造我们首先在后端增加一个方法,用于接受前端传递的附件及其他参数(我这里主要传递的是用户信息,用户更新用户表,记录头像地址)。4.2.1、Controller增加接受用户上传头像的Api,具体的实现我们稍后在说。/** * 上传头像 * * @param multipartFile 文件信息 * @return 用户信息 */ @PostMapping("uploadAvatar") public AjaxResult uploadAvatar(@RequestParam("file") MultipartFile multipartFile,@RequestParam("userInfo") String oilUser) { return AjaxResult.success(oilUserService.uploadAvatar(multipartFile)); }4.2.2、增加七牛云依赖经过上面改造,我们已经可以将用户头像上传到我们的后端了,接下来的任务就是将头像上传到我们的七牛云了。现在pom.xml中增加七牛云的依赖<!-- 七牛云--> <dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>7.2.28</version> </dependency>4.2.3、增加七牛云配置为了方便使用,我们将七牛云的一些配置信息放入yaml文件中,方便维护。# ========================== ↓↓↓↓↓↓ 七牛云配置 ↓↓↓↓↓↓ ========================== qiniu: accessKey: XXX # Key secretKey: XXX # 密钥 bucket: XXX # 空间名称 domain: XXX # 访问域名 dir: XXX/ # 目录参数说明:accessKey:AK,在七牛云,个人中心,密钥管理中可以看到secretKey:SK,在七牛云,个人中心,密钥管理中可以看到bucket:空间名称,根据自己创建的空间填写domain:访问域名,根据控件绑定的域名实际填写dir:存储路径,因为七牛云默认是直接存储到根目录,为了方便管理,我们可以创建子目录,比如avatar,可以填写avatar/4.2.4、增加配置类为了方便使用,我们将yaml的值,映射到配置类上。/** * 七牛云实体 */ @Component @ConfigurationProperties(prefix = "qiniu") public class QiNiuConfig { /** * Key */ private static String accessKey; /** * 密钥 */ private static String secretKey; /** * 空间名称 */ private static String bucket; /** * 访问域名 */ private static String domain; /** * 目录 */ private static String dir; public static String getAccessKey() { return accessKey; } public void setAccessKey(String accessKey) { QiNiuConfig.accessKey = accessKey; } public static String getSecretKey() { return secretKey; } public void setSecretKey(String secretKey) { QiNiuConfig.secretKey = secretKey; } public static String getBucket() { return bucket; } public void setBucket(String bucket) { QiNiuConfig.bucket = bucket; } public static String getDomain() { return domain; } public void setDomain(String domain) { QiNiuConfig.domain = domain; } public static String getDir() { return dir; } public void setDir(String dir) { QiNiuConfig.dir = dir; } } 4.2.5、封装公共方法为了方便调用,我们将上传、删除等方法封装到单独的服务中。接口/** * 七牛接口 */ public interface IQiNiuService { /** * 以文件的形式上传 * * @param file * @param fileName: * @return: java.lang.String */ String uploadFile(File file, String fileName) throws QiniuException; /** * 以流的形式上传 * * @param inputStream * @param fileName: * @return: java.lang.String */ String uploadFile(InputStream inputStream, String fileName) throws QiniuException; /** * 删除文件 * * @param key: * @return: java.lang.String */ String delete(String key) throws QiniuException; } 实现@Service public class QiNiuServiceImpl implements IQiNiuService, InitializingBean { // 七牛文件上传管理器 private final Configuration cfg; private final Auth auth; public QiNiuServiceImpl() { // //构造一个带指定 Region 对象的配置类 cfg = new Configuration(Region.huadong()); auth = Auth.create(QiNiuConfig.getAccessKey(), QiNiuConfig.getSecretKey()); } /** * 定义七牛云上传的相关策略 */ private StringMap putPolicy; @Override public String uploadFile(File file, String fileName) throws QiniuException { if (!StringUtils.isEmpty(QiNiuConfig.getDir())) { fileName = QiNiuConfig.getDir() + fileName; } UploadManager uploadManager = new UploadManager(cfg); Response response = uploadManager.put(file, fileName, getUploadToken()); int retry = 0; while (response.needRetry() && retry < 3) { response = uploadManager.put(file, fileName, getUploadToken()); retry++; } if (response.statusCode == 200) { return "http://" + QiNiuConfig.getDomain() + "/" + fileName; } return "上传失败!"; } @Override public String uploadFile(InputStream inputStream, String fileName) throws QiniuException { if (!StringUtils.isEmpty(QiNiuConfig.getDir())) { fileName = QiNiuConfig.getDir() + fileName; } UploadManager uploadManager = new UploadManager(cfg); Response response = uploadManager.put(inputStream, fileName, getUploadToken(), null, null); int retry = 0; while (response.needRetry() && retry < 3) { response = uploadManager.put(inputStream, fileName, getUploadToken(), null, null); retry++; } if (response.statusCode == 200) { return "http://" + QiNiuConfig.getDomain() + "/" + fileName; } return "上传失败!"; } @Override public String delete(String key) throws QiniuException { BucketManager bucketManager = new BucketManager(auth, cfg); Response response = bucketManager.delete(QiNiuConfig.getBucket(), key); int retry = 0; while (response.needRetry() && retry++ < 3) { response = bucketManager.delete(QiNiuConfig.getBucket(), key); } return response.statusCode == 200 ? "删除成功!" : "删除失败!"; } @Override public void afterPropertiesSet() throws Exception { this.putPolicy = new StringMap(); putPolicy.put("insertOnly", 0); } /** * 获取上传凭证 */ private String getUploadToken() { return this.auth.uploadToken(QiNiuConfig.getBucket(), null, 3600, putPolicy); } } 有几个需要注意的点:在构造函数中,构造Configuration时,需要指定区域,因为我是华东区域的,因此使用的是Region.huadong(),如果使用的其他区域的,需要根据自己实际区域指定。在指定策略时,因为我一个用户只允许一个头像,因此上传时,如果存在我们直接覆盖的,所以在afterPropertiesSet方法中,设置上传策略时,直接指定的putPolicy.put("insertOnly", 0);,即如果存在就覆盖,如果不想覆盖,可以设置putPolicy.put("insertOnly", 1);,但是此时需要注意,如果上传重名文件,会返回异常。4.2.6、完善后用户上传头像方法用户上传头像后,更新用户实体(但是此时不更新数据库),将更新后的实体返回到前端,点击保存时,再更新数据库。 @Override public OilUser uploadAvatar(MultipartFile multipartFile, OilUser oilUser) throws IOException { String originalFilename = multipartFile.getOriginalFilename(); if (originalFilename == null || !originalFilename.contains(".")) { throw new CustomException("文件名不正确"); } String fileName = "avatar" + oilUser.getId() + originalFilename.substring(originalFilename.lastIndexOf(".")); String avatarUrl = qiNiuService.uploadFile(multipartFile.getInputStream(), fileName); oilUser.setAvatarUrl(avatarUrl); // LambdaUpdateWrapper<OilUser> userUpdateWrapper = new LambdaUpdateWrapper<>(); // userUpdateWrapper.set(OilUser::getAvatarUrl, oilUser.getAvatarUrl()); // userUpdateWrapper.eq(OilUser::getId, oilUser.getId()); // oilUserMapper.update(null, userUpdateWrapper); return oilUser; }4.2.7、用户保存方法分改造用户保存方法主要增加userUpdateWrapper.set(OilUser::getAvatarUrl, user.getAvatarUrl());,当用户有头像时,同步更新用户的头像信息。/** * 新增或保存用户 * * @param user 用户 * @return 结果 */ public OilUser saveUser(OilUser user) { if (user == null) { throw new CustomException("用户信息不能为空"); } if (user.getId() == null) { user.setId(""); } if (checkUserNameExist(user)) { throw new CustomException("用户名已存在"); } if (StringUtils.isEmpty(user.getId())) { user.setId(UUID.randomUUID().toString()); if (!StringUtils.isEmpty(user.getPassword())) { user.setPassword(passwordEncoder.encode(user.getPassword())); } oilUserMapper.insert(user); } else { LambdaUpdateWrapper<OilUser> userUpdateWrapper = new LambdaUpdateWrapper<>(); userUpdateWrapper.set(OilUser::getUsername, user.getUsername()); userUpdateWrapper.set(OilUser::getNickName, user.getNickName()); userUpdateWrapper.set(OilUser::getTel, user.getTel()); userUpdateWrapper.set(OilUser::getEmail, user.getEmail()); if (!StringUtils.isEmpty(user.getPassword())) { userUpdateWrapper.set(OilUser::getPassword, passwordEncoder.encode(user.getPassword())); } if (!StringUtils.isEmpty(user.getProvince())) { userUpdateWrapper.set(OilUser::getProvince, user.getProvince()); } if (!StringUtils.isEmpty(user.getCity())) { userUpdateWrapper.set(OilUser::getCity, user.getCity()); } if (!StringUtils.isEmpty(user.getAvatarUrl())) { userUpdateWrapper.set(OilUser::getAvatarUrl, user.getAvatarUrl()); } userUpdateWrapper.eq(OilUser::getId, user.getId()); oilUserMapper.update(null, userUpdateWrapper); user.setPassword(""); } return user; }五、效果微信用户登录后,如果没有上传过头像,会自动跳转到【个人信息】页面在个人信息上传头像后,自动跳转到首页。六、其他注意事项七牛云域名需要配置HTTPS小程序域名白名单uploadFile合法域名需要配置后台上传附件的域名。
2022年11月14日
2,023 阅读
0 评论
3 点赞
2022-11-06
Jackson反序列化时忽略某些属性的方法
在开发接口时,出于某些目的(比如有些字段我要设置默认值,不能受入参的影响),我们在入参字符串序列化成实体时,可能需要忽略某些属性。我们要达到的目的非常简单,一个用户类User.java,我们要达到的目的有两个:(1)控制序列化时,忽略name属性。(2)控制反序列化是,忽略name属性。一、@JsonIgnore注解当我们想控制是一个实体在序列化时,忽略某些属性,我们第一个想到的也许就是@JsonIgnore注解了,@JsonIgnore注解能控制实体在序列化时,忽略某些属性,但是无法控制实体在反序列化时忽略的属性。先来看一下我们测试用的User.java类。public class User implements Serializable { private String code; @JsonIgnore private String name; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } }1.1、序列化我们先试一下序列化,看看name属性能否在序列化时自动忽略掉。测试方法也非常简单ObjectMapper objectMapper = new ObjectMapper(); User user = new User(); user.setCode("code123"); user.setName("name123"); System.out.println(objectMapper.writeValueAsString(user));可以看到,name属性确实忽略了,也就是这个注解针对序列化是生效的。1.2、反序列化序列化时候没问题,那么反序列化时候是否也没问题呢,我们接着往下看。String userStr = "{\"code\":\"code123\",\"name\":\"name123\"}"; User user1 = objectMapper.readValue(userStr, User.class);这个时候我们不能在序列化输出显示了,我们调试看一下。我们可以看到,也是能够正常忽略掉的。但是,今天在项目开发时,我使用了同样的方法,发现@JsonIgnore注解没有起作用,但是实体类是这么样的@Data @NoArgsConstructor public class User implements Serializable { private String code; @JsonProperty(value = "name") @JsonIgnore private String name; @JsonProperty(value = "billCateProp") @JsonIgnore @JSONField(deserialize = false) private String billCateProp; @JsonFormat(pattern = "yyyy-MM-dd") private Date date; }怀疑了Data注解,也怀疑了@JsonProperty与@JsonIgnore注解冲突,甚至是属性Camel命名等,但是同样的代码,我拿到别的地方也有没有再现问题,所以终究也不知道是什么原因导致的。2、@JsonProperty@JsonIgnore的问题没有解决,只能另辟蹊径,最终发现@JsonIgnore注解有个access属性,通过access属性解决了问题,在反序列化时,不会再读取忽略的字段,遂将代码调整如下 @JsonProperty(value = "billCateProp",access = JsonProperty.Access.READ_ONLY) // @JsonIgnore @JSONField(deserialize = false) private String billCateProp;JsonProperty.Access.READ_ONLY代表反序列化是忽略字段。JsonProperty.Access.WRITE_ONLY代表序列化时忽略字段。@JsonIgnore失效的问题,最终也没找到答案,有知道的童鞋不妨留言说明一下。
2022年11月06日
1,533 阅读
0 评论
1 点赞
2022-05-21
SpringBoot事务提交后执行异步代码
一般情况下,我们在使用事务时,都是在方法上添加一个@Transactional注解 @Transactional(rollbackFor = Exception.class) public void test1() { }但是有些时候,除了主要核心业务外,我们可能还需要推送消息,但是推送消息我们又需要使用我们核心业务的数据,比如我在核心业务代码中执行了插入,之后需要异步获取插入的数据,推送消息或者发送给异构系统,这个时候,我们可以使用Spring Boot提供的TransactionSynchronization接口,并实现afterCommit方法package com.example.demo.service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionSynchronizationAdapter; import org.springframework.transaction.support.TransactionSynchronizationManager; import javax.annotation.Resource; @Service public class Test123Service { @Resource Test2Service test2Service; @Transactional(rollbackFor = Exception.class) public void test1() { boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive(); if (synchronizationActive) { TransactionSynchronizationManager.registerSynchronization( new TransactionSynchronizationAdapter() { @Override public void afterCommit() { test2Service.test2(); } } ); } else { test2Service.test2(); } } } package com.example.demo.service; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class Test2Service { @Async public void test2(){ System.out.println("test2"); } }方法test1()不能调用同一给类中使用了@Async注解的方法test2()(此时@Async会失效)
2022年05月21日
2,700 阅读
1 评论
1 点赞
2022-01-05
Spring Boot另类前后端分离项目部署
对于前后端分离的项目,一般情况下,我们是把静态文件放到nginx中,然后通过端口转发,请求后端避免跨域的问题。我们知道,Spring Boot项目中,我们可以添加静态资源,根据约定大于配置的规则,默认静态文件位置位于以下位置classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources,servlet context:将前端文件放到Spring Boot静态资源中的好处是不存在跨域的问题。默认静态资源,如果我们放到resources中,这显然不是我们想看到的。我们希望的是,将静态文件放到jar包之外的某个特定位置,这样静态资源的修改,不会涉及jar包的修改。配置文件修改为了实现静态资源文件的配置,我们需要通过spring.web.resources.static-locations指定静态文件的位置web: spring: web: resources: static-locations: file:${web}之所以添加一个web,是为了方便我们在命令行动态指定位置。放置静态资源我这里演示将index.html放到--web=/Users/lisen/IdeaProjects/demo/web中进行访问。内容如下:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> </head> <body> 这是首页1 <div id="div1"></div> <script> $(document).ready(function(){ $.ajax({ url:"/book/get/1", type : "POST", //请求成功 success : function(result) { $("#div1").html(result); }, }) }); </script> </body> </html>启动命令java -jar /Users/lisen/IdeaProjects/demo/target/demo-0.0.1-SNAPSHOT.jar --web=/Users/lisen/IdeaProjects/demo/web访问前端页面可以看到,页面正确访问,并且没有出现跨域的问题。
2022年01月05日
931 阅读
0 评论
2 点赞
2021-12-18
SpringBoot JPA自动设置创建者、最后修改者
书接上文,SpringBoot JPA自动设置创建时间、修改时间,审计不可能只包含创建时间、最后修改时间,肯定得有人,也就是必须得有创建者、最后修改者。Spring Data Jpa设置创建者、最后修改者也非常简单。实体修改@Data @Entity @Table(name = "Book") @EntityListeners(AuditingEntityListener.class) public class Book { @Id private long id; private String name; private String author; private BigDecimal price; @CreatedDate private Timestamp createdDate; @CreatedBy private String createBy; @LastModifiedDate private Timestamp lastModifiedDate; @LastModifiedBy private String lastModifiedBy; }@CreatedBy注解用于标识创建者。@LastModifiedBy注解用于标识最后修改者。其他注解跟设置创建时间、最后修改时间一致,不再赘述,这里重点说一下,如何获取创建者、最后修改者。设置创建者、最后修改者值在Spring Data Jpa中,可以通过实现AuditorAware接口让程序知道当前审核程序的用户,实现逻辑根据项目实际情况编写。/** * 审计接口,获取当前人员 */ @Configuration public class JpaAuditWare implements AuditorAware<String> { @Override public Optional<String> getCurrentAuditor() { return Optional.of(UUID.randomUUID().toString()); } }验证创建验证 @GetMapping("save") public Book saveBook() { Book book = new Book(); book.setId(1L); book.setName("《山海经》"); book.setAuthor("佚名"); book.setPrice(new BigDecimal("500")); return bookService.saveBook(book); }修改验证然后我们修改一下数据,验证一下最后修改人、最后修改时间{message type="info" content="如果数据未发生改变,那么最后修改者、最后修改时间是不会发生改变的。"/}
2021年12月18日
1,767 阅读
0 评论
0 点赞
2021-12-18
SpringBoot JPA自动设置创建时间、修改时间
JPA提供了审计功能,用于设置创建者、创建时间、修改者、修改时间等参数。创建时间、修改时间很好理解,就是当前时间,但是创建者、修改者一般都是通过上下文信息获取的,由于我这边是接口里面使用,未使用创建者、修改者,所以先介绍一下创建时间、修改时间的使用。添加依赖那些巴拉巴拉的就不啰嗦。创建实体@Getter @Setter @ToString @Entity @Table(name = "ARAPDiscountRecord") @EntityListeners(AuditingEntityListener.class) public class ARAPDiscountRecordEntity implements Serializable { @CreatedDate @Column(name = "timestamp_createdon") private Timestamp timestampCreatedon; @LastModifiedDate @Column(name = "timestamp_lastchangedon") private Timestamp timestampLastchangedon; }@CreatedDate注解用于标识创建时间。@LastModifiedDate注解用于标识修改时间。实体类添加@EntityListeners(AuditingEntityListener.class)标识启动审计。启动审计再启用或者配置类上添加@EnableJpaAuditing启动审计。@EnableJpaAuditing @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }这样的话,每次创建数据,系统会自动赋值timestampCreatedon列,修改数据时,系统会自动赋值timestampLastchangedon字段。
2021年12月18日
1,527 阅读
0 评论
0 点赞
2021-12-12
Spring Boot中ThreadLocal踩坑
ThreadLocal作用ThreadLocal的作用:用来存当前线程的局部变量,不同线程间互不干扰。拿完数据记得需要移除数据,不然JVM不会将ThreadLocal回收(可能还会被引用),多了就会出现内存泄漏的情况。解析ThreadLocal类ThreadLocal包含几个方法:public T get() { } public void set(T value) { } public void remove() { } protected T initialValue() { }T get()get()方法是用来获取ThreadLocal在当前线程中保存的变量副本set(T value)set()用来设置当前线程中变量的副本void remove()remove()用来移除当前线程中变量的副本T initialValue()initialValue()是一个protected方法,一般是用来在使用时进行重写的,它是一个延迟加载方法。ThreadLocal变量污染ThreadLocal会在每个线程存储一个副本,但是如果我们使用的是比如Tomcat,Tomcat自身会维护一个线程池,线程结束后,并不会马上销毁,而是会重新进入线程池,下次有请求时,有可能会复用当前线程,如果我们每次使用ThreadLocal之前,没有进行Set(T value),那么就有可能导致不同线程之间变量污染,比如下面的代码@RestController @RequestMapping(value = "book") @Slf4j public class BookController { private static final ThreadLocal<String> THREAD_LOCAL_TEST = new ThreadLocal<>(); @Resource private IBookService bookService; /** * 构造函数 */ public BookController() { log.info("构造函数,此时bookService :" + bookService); } @PostConstruct public void init() { log.info("PostConstruct,此时bookService :" + bookService); } @PreDestroy public void destroy() { log.info("PreDestroy"); } @GetMapping(value = "set") public void set() { if (StringUtils.isEmpty(THREAD_LOCAL_TEST.get())) { THREAD_LOCAL_TEST.set(UUID.randomUUID().toString()); } } @GetMapping(value = "search") public List<Book> search() { log.error(THREAD_LOCAL_TEST.get()); return bookService.search(); } }使用jmeter进行请求,可以看到同一线程,输出的内容永远不会发生改变。可以在内次使用之前进行set(T value),但是set(T value)可能会导致内存无法释放。ThreadLocal可能导致的内存泄露ThreadLocal为了避免内存泄露,不仅使用了弱引用维护key,还会在每个操作上检查key是否被回收,进而再回收value。但是从中也可以看到,ThreadLocal并不能100%保证不发生内存泄漏。比如,很不幸的,你的get()方法总是访问固定几个一直存在的ThreadLocal,那么清理动作就不会执行,如果你没有机会调用set()和remove(),那么这个内存泄漏依然会发生。一个良好的习惯依然是:当你不需要这个ThreadLocal变量时,主动调用remove(),这样对整个系统是有好处的。@RestController @RequestMapping(value = "book") @Slf4j public class BookController { private static final ThreadLocal<String> THREAD_LOCAL_TEST = new ThreadLocal<>(); @Resource private IBookService bookService; /** * 构造函数 */ public BookController() { log.info("构造函数,此时bookService :" + bookService); } @PostConstruct public void init() { log.info("PostConstruct,此时bookService :" + bookService); } @PreDestroy public void destroy() { log.info("PreDestroy"); } @GetMapping(value = "set") public void set() { THREAD_LOCAL_TEST.set(UUID.randomUUID().toString()); } @GetMapping(value = "search") public List<Book> search() { log.error(THREAD_LOCAL_TEST.get()); THREAD_LOCAL_TEST.remove(); return bookService.search(); } }ThreadLocal与局部变量局部变量和ThreadLocal起到的作用是一样的,保证了并发环境下数据的安全性。那就是说,完全可以用局部变量来代替ThreadLocal咯?This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its {@code get} or {@code set} method) has its own, independently initialized copy of the variable. {@code ThreadLocal} instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).翻译过来 ThreadLocal提供的是一种线程局部变量。这些变量不同于其它变量的点在于每个线程在获取变量的时候,都拥有它自己相对独立的变量初始化拷贝。ThreadLocal的实例一般是私有静态的,可以做到与一个线程绑定某一种状态。所以就这段话而言,我们知道ThreadLocal不是为了满足多线程安全而开发出来的,因为局部变量已经足够安全。ThreadLocal是为了方便线程处理自己的某种状态。 可以看到ThreadLocal实例化所处的位置,是一个线程共有区域。好比一个银行和个人,我们可以把钱存在银行,也可以把钱存在家。存在家里的钱是局部变量,仅供个人使用;存在银行里的钱也不是说可以让别人随便使用,只有我们以个人身份去获取才能得到。所以说ThreadLocal封装的变量我们是在外面某个区域保存了处于我们个人的一个状态,只允许我们自己去访问和修改的状态。
2021年12月12日
1,891 阅读
0 评论
0 点赞
2021-12-05
@SpringBootApplication标注非引导类
一般情况下,@SpringBootApplication一般都是标注在项目引导类上。像下面这样:@Slf4j @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoConfiguration.class, args); } }但是@SpringBootApplication一定要标注到引导类上吗?答案是否定的。我们可以将@SpringBootApplication标注到任意的类上。比如我们增加以下类package cc.lisen.demo.config; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoConfiguration { }然后改造引导类@Slf4j public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoConfiguration.class, args); } }再次运行项目,可以发现项目可以正常运行但是如果我们访问我们的接口,会发现提示404注意我们DemoConfiguration所在的包cc.lisen.demo.config,@SpringBootApplication只会扫描当前包及下级包,所以,我们的接口它扫描不到,就提示404。如果希望我们其他的类被扫描到,我们就需要添加scanBasePackages属性。package cc.lisen.demo.config; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication(scanBasePackages = {"cc.lisen.demo"}) public class DemoConfiguration { }重启项目,在访问接口,发现能正常访问
2021年12月05日
978 阅读
0 评论
0 点赞
1
2
3
...
9