1、安装java17
此步骤省略
安装完成后,查看
2、通过 idea 初始化向导创建springboot项目
然后点击Create即可创建成功
3、配置pom文件
<?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>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xgxz.boot</groupId>
<artifactId>spring-boot-3-nacos</artifactId>
<version>1.0</version>
<name>spring-boot-3-nacos</name>
<description>spring-boot-3-nacos</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
<spring.cloud.version>2022.0.3</spring.cloud.version>
<spring.cloud.alibaba.version>2022.0.0.0-RC2</spring.cloud.alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 使用bootstrap.yml必须依赖此类 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.1</version>
</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>
关于选择nacos版本,参考
地址:https://github.com/alibaba/spring-cloud-alibaba/blob/2022.x/README-zh.md
4、配置yml文件
server:
port: 8868
spring:
application:
name: spring-boot-nacox
data:
redis:
host: 192.168.56.10
port: 6379
database: 2
# password: 123456 #默认为空
timeout: 3000ms
lettuce:
pool:
max-active: 20 # 最大连接数,负值表示没有限制,默认8
max-wait: -1 # 最大阻塞等待时间,负值表示没限制,默认-1
max-idle: 8 # 最大空闲连接,默认8
min-idle: 0 # 最小空闲连接,默认0
cloud:
nacos:
discovery:
server-addr: 192.168.56.10:8848
5、配置主启动类
/**
* ClassName: SpringBootQuartzApplication
* Package: com.xgxz.boot
* Description:
*
* @Author: 习惯向左
* @Create: 2023/7/6 - 16:59
* @Version: v1.0
*/
@Slf4j
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootQuartzApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootQuartzApplication.class, args);
log.info("--------------启动完成----------------");
}
}
6、配置RedisConfig
/**
* ClassName: RedisConfig
* Package: com.xgxz.boot.config
* Description:
*
* @Author: 习惯向左
* @Create: 2023/7/6 - 17:10
* @Version: v1.0
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}
7、另外本人习惯在使用日期类时用 LocalDateTime,还需要额外配置一下
/**
* ClassName: User
* Package: com.xgxz.boot.entity
* Description:
*
* @Author: 习惯向左
* @Create: 2023/7/6 - 17:40
* @Version: v1.0
*/
@Data
public class User {
private Long id;
private String name;
private int age;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") //此注解用来接收字符串类型的参数封装成LocalDateTime类型
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8", shape = JsonFormat.Shape.STRING) //此注解将date类型数据转成字符串响应出去
@JsonDeserialize(using = LocalDateTimeDeserializer.class) // 反序列化
@JsonSerialize(using = LocalDateTimeSerializer.class) // 序列化
private LocalDateTime sendTime;
}
8、nacos需要额外多暴露两个端口号,8848就不多说了,还需要暴露9848和9849,下面使用docker来安装nacos
8.1、先查询一下nacos镜像
docker search nacos
8.2、拉取镜像
docker pull nacos/nacos-server
8.3、创建文件目录,为将容器内的文件挂载到外面来
mkdir -p /mydata/nacos/init.d /mydata/nacos/logs
8.4、执行启动命令
docker run -d -p 8848:8848 -p 9848:9848 -p 9849:9849 \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
-e JVM_XMS=256m -e JVM_XMX=256m \
-v /mydata/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties \
-v /mydata/nacos/logs:/home/nacos/logs \
--restart always \
--name nacos \
nacos/nacos-server
这里采用单机试用模式安装,也可参考Nacos官方文档 https://nacos.io/zh-cn/docs/deployment.html来安装
8.5、查看是否运行成功
docker ps
访问浏览器 ip:8848/nacos
默认用户名、密码都为 nacos, 输入即可登录成功
一切就绪,下面我们启动服务来进行测试
服务启动正常
也成功注册到nacos上
存入的数据是没有问题
查出来的数据也是我们想要的
至此,就成功了,是不是还挺容易的
习惯向左,感觉至上