当前位置: 首页 > article >正文

Spring boot 集成 nacos、redis、mysql

1,准备好nacos环境,准备ncc.yml配置:

  在配置添加

test: haha 

 

2,添加依赖

在pom.xml 文件中添加Nacos 客户端的依赖,样例使用Spring Cloud Alibaba

版本使用2023.x 分支,详情可查看

版本发布说明-阿里云Spring Cloud Alibaba官网

  <dependencies>

    <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-config</artifactId>
    </dependency>

  </dependencies>

 
<!--新增依赖集中管理-->
  <dependencyManagement>
    <dependencies>
      <!--spring-boot-->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.4</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring-cloud-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>2023.0.1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring-alibaba-->
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2023.0.1.3</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

3,配置application.yml

spring:
  cloud:
    nacos:
      serverAddr: 127.0.0.1:8848
      username: nacos
      password: nacos@2024
      namespace: ncc
      group: dev
      config:
        file-extension: yaml
  config:
    import:
      - nacos:ncc.yml?refreshEnabled=true





4,配置启动类 NccApplication

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class NccApplication {

	@Value("${test}")
	private String test;

	public static void main(String[] args) {
		SpringApplication.run(NccApplication.class, args);
	}

	@GetMapping("/test")
	public String test() {
		return test;
	}

}

5,启动测试:

本地访问

http://localhost:8080/test

6,添加redis

pom.xml增加redis依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

在ncc.yml配置添加redis

spring:
  data:
    redis:
      host: 47.108.195.11
      port: 6379
      password: redis@2024
      database: 0

增加test依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

 创建测试类测试RedisTest

import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest(classes = NccApplication.class)
public class RedisTest {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    String redisKey="demo";

    @Test
    void redisTest() {
        stringRedisTemplate.opsForValue().set(redisKey,"test1",10, TimeUnit.MINUTES);
        String val = stringRedisTemplate.opsForValue().get(redisKey);
        System.out.println("val = " + val);

    }

}

 测试正常

7,添加Mysql依赖

    <dependency>
      <groupId>com.mysql</groupId>
      <artifactId>mysql-connector-j</artifactId>
      <version>8.0.33</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

添加配置:

spring:
  data:
    redis:
      host: 47.108.195.246
      port: 6379
      password: redis@2024
      database: 0
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://47.108.195.246:3306/nacos?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: root
    password: mysql@2024

创建测试类MysqlTest

mport java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;

@SpringBootTest(classes = NccApplication.class)
public class MysqlTest {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Test
	void redisTest() {
		List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM users");
		System.out.println("list = " + list);
	}

}

完成测试

8,以下是完整的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 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.2.4</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.demo</groupId>
  <artifactId>ncc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>ncc</name>
  <description>ncc</description>
  <properties>
    <java.version>17</java.version>
  </properties>
  <dependencies>

    <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-config</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>


    <dependency>
      <groupId>com.mysql</groupId>
      <artifactId>mysql-connector-j</artifactId>
      <version>8.0.33</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>

  </dependencies>

  <!--新增依赖集中管理-->
  <dependencyManagement>
    <dependencies>
      <!--spring-boot-->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.4</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring-cloud-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>2023.0.1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring-alibaba-->
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2023.0.1.3</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <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>


http://www.kler.cn/a/396643.html

相关文章:

  • Unity读取Json
  • flink cdc 应用
  • Java中 LinkedList<>,ArrayDeque<>的区别 || Queue和Deque的区别
  • Linux服务器定时执行jar重启命令
  • 大模型在蓝鲸运维体系应用——蓝鲸运维开发智能助手
  • 力扣-Mysql-3308- 寻找表现最佳的司机(中等)
  • 软件设计师-软件工程
  • tensorflow案例6--基于VGG16的猫狗识别(准确率99.8%+),以及tqdm、train_on_batch的简介
  • Spring——AOP
  • EasyExcel使用
  • Shell基础2
  • 嵌入式硬件电子电路设计(五)MOS管详解(NMOS、PMOS、三极管跟mos管的区别)
  • 反转链表
  • D3 可以加载的数据格式有哪些?(12种)
  • RabbitMQ介绍和快速上手案例
  • 深入解析 OpenHarmony 构建系统-3-GN 构建系统管理脚本
  • 我要成为算法高手-二分查找篇
  • 【大数据学习 | HBASE高级】hbase的API操作
  • C# 中Math.Round 和 SQL Server中decimal(18,2) 不想等的问题
  • 大数据实训室建设的必要性
  • asp应用-Access|IIS|
  • [含文档+PPT+源码等]精品大数据项目-python基于Spark实现的新闻推荐系统的设计与实现
  • Rust学习(五):泛型、trait
  • 网络游戏安全现状及相关应对方案
  • ODX架构开发流程
  • fpga 同步fifo