作者:cui115588 | 来源:互联网 | 2023-09-16 10:18
1. 技术选型
springcloud管理的微服务架构众多,为了不用在项目后期解决环境冲突问题,请严格按照官网给出的 boot 与cloud 对应关系进行选型。
SpringCloud官网地址
-
- 版本对应图:根据自己的需求 选择对应的版本(参考对应的文档 Reference Doc.)
2. 项目创建
- 2.1 创建父项目
- 2.1.1 创建maven 项目删除 src 文件夹, 只保留POM 文件
- 2.1.2 修改Pom 文件
# 1. 打包类型标签
- <packaging>pom</packaging>
dependencyManagement 只声明依赖&#xff0c;并不实现引入&#xff0c;所以子项目还需要写要引入的依赖。如果不在子项目中声明依赖&#xff0c;是不会从服项目中继承下来的&#xff0c;且子项目中不明确版本号才会从父工程继承&#xff08;version和scope&#xff09;。后面如果需要添加需要管理的依赖&#xff0c;则分别在dependencyManagement标签和properties标签中补充即可。
好处&#xff1a; 父工程中版本号 一处修改&#xff0c;所有子模块都生效&#xff0c;统一管理。
<groupId>com.xiaoming</groupId><artifactId>spring-cloud-xiaomingwang</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.cloud-version>2020.0.3</spring.cloud-version><spring.boot-version>2.4.6</spring.boot-version></properties><!--dependencyManagement 是只管理版本&#xff0c;不引入依赖版本号写在 properties 标签中子module不用写 groupId 和 version--><dependencyManagement><!--spring boot 版本控制 2.4.6--><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring.boot-version}</version><type>pom</type><scope>import</scope></dependency><!--spring cloud 版本管理 2020.0.3 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring.cloud-version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
springcloud 依赖拉不下来使用指令
mvn -U idea:idea
redis 缓存配置 依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- redis 连接池 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>
配置&#xff1a;
&#64;Configuration
public class RedisConfig {&#64;Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> redisTemplate &#61; new RedisTemplate<>();redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());redisTemplate.setConnectionFactory(redisConnectionFactory);return redisTemplate;}&#64;Beanpublic RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {RedisCacheWriter redisCacheWriter &#61; RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());RedisCacheConfiguration redisCacheConfiguration &#61; RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(30)).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getKeySerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);}}