文章目录
- 框架说明
- 1.IDEA新建springCloud项目
- 2.集成数据库驱动、连接池、mybatisplus、nacos,并统一版本
- 3.添加配置文件
- 4.集成nacos
- 参考文献
- github地址
框架说明
服务发现:nacos做注册中心和配置中心
Java WEB:springCloud+springBoot+mybatisPlush
数据库:mysql
1.IDEA新建springCloud项目
1.1新建最外层父工程
1.1.1新建
注意:这里虽然选择了springboot版本,但是太高了,稍后我会修改pom文件
1.1.2完成目录(手动删除src目录,删除pom文件中所有的dependency)
1.2搭建mould子项目
1.2.1新建
注意:这里是右键父目录
1.2.2选择初始化的依赖项
1.2.3关联父项目
- 将红色区域拷贝到子项目中parent区域
- 增加子项
1.2.4直接运行
2.集成数据库驱动、连接池、mybatisplus、nacos,并统一版本
2.1父pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hz</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>web</name>
<packaging>pom</packaging>
<description>nacos+Cloud微服务框架</description>
<properties>
<java.version>11</java.version>
<springboot.version>2.2.2.RELEASE</springboot.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
<mybatis.version>2.1.1</mybatis.version>
<mybatis-plus.version>3.3.0</mybatis-plus.version>
<druid.version>1.1.10</druid.version>
</properties>
<modules>
<!-- 系统模块 -->
<module>system</module>
</modules>
2.2子pom文件
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--简略get、set方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
<scope>runtime</scope>
</dependency>
<!--nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
<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>
</dependencies>
</dependencyManagement>
2.3.maven reload
3.添加配置文件
3.1修改配置文件名称
3.2执行sql
CREATE DATABASE `demo`;
use demo;
CREATE TABLE `sys_system` (
`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '子系统名称' COLLATE 'utf8_general_ci',
`path` VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'path' COLLATE 'utf8_general_ci',
`icon_url` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '子系统图标URL' COLLATE 'utf8_general_ci',
`category` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '子系统类别' COLLATE 'utf8_general_ci',
`status` VARCHAR(1) NOT NULL DEFAULT '0' COMMENT '子系统状态(0正常 1停用)' COLLATE 'utf8_general_ci',
`create_by` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '创建者' COLLATE 'utf8_general_ci',
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
`remark` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '备注' COLLATE 'utf8_general_ci',
`del_flag` VARCHAR(1) NOT NULL DEFAULT '0' COMMENT '删除标记(0正常 1删除)' COLLATE 'utf8_general_ci',
`owner` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '所有权' COLLATE 'utf8_general_ci',
`topic` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '主题名称' COLLATE 'utf8_general_ci',
`system_key` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '私钥' COLLATE 'utf8_general_ci',
PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2
;
3.3使用easycode生成centroller、service、dao、mapper文件(文章末尾有github地址,也可以留言邮箱看到后会发送项目邮件)
3.3.1启动类增加dao扫描并附上controller文件代码
@RestController
//@RefreshScope
@RequestMapping("test")
public class SystemController {
@Value("${useLocalCache}")
private String useLocalCache;
@Autowired
private SysSystemService sysSystemService;
@RequestMapping(value = "/config", method = RequestMethod.GET)
public String getNacosProperties() {
return useLocalCache;
}
@RequestMapping(value = "/get", method = RequestMethod.GET)
public String get() {
return "hello world";
}
@RequestMapping(value = "/system", method = RequestMethod.GET)
public SysSystem getSystem() {
return sysSystemService.queryById(1);
}
}
3.3.2配置文件
useLocalCache: 123
spring:
application:
name: hz-demo
datasource:
url: jdbc:mysql://192.168.1.202:3306/demo?useUnicode=true&characterEncoding=UTF8&nullCatalogMeansCurrent=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
server:
# 服务HTTP端口号
port: 8081
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.hz.system.dao.**
mybatis-plus:
type-aliases-package: com.hz.system.entity
global-config:
db-config:
id-type: auto
field-strategy: not_empty
#驼峰下划线转换
column-underline: true
#逻辑删除配置
logic-delete-value: 0
logic-not-delete-value: 1
db-type: mysql
refresh: false
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
cache-enabled: false
3.3.3完整目录
3.3.4测试
4.集成nacos
4.1安装nacos
官方提供的文档很清楚的写明了安装方式
https://nacos.io/zh-cn/docs/quick-start.html
4.2增加pom依赖
<!--nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
4.3nacos新建配置文件
注意:当前匹配规则为applicationName.yml(还可以设置开发版本比如dev、master详情参考一下nacos手册)
useLocalCache: 123
spring:
application:
name: hz-system
datasource:
url: jdbc:mysql://192.168.1.202:3306/demo?useUnicode=true&characterEncoding=UTF8&nullCatalogMeansCurrent=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
cloud:
nacos:
discovery:
server-addr: 192.168.1.202:8848
server:
# 服务HTTP端口号
port: 8081
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.hz.web.dao.**
mybatis-plus:
type-aliases-package: com.hz.web.entity
global-config:
db-config:
id-type: auto
field-strategy: not_empty
#驼峰下划线转换
column-underline: true
#逻辑删除配置
logic-delete-value: 0
logic-not-delete-value: 1
db-type: mysql
refresh: false
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
cache-enabled: false
4.4springboot修改配置文件
-
清空application配置文件
-
新增bootstrap.yml(优先加载此配置,比application优先级要高,nacos配置中心就在此让配置生效,相同配置nacos配置优先)
4.5重新启动项目
显示加载了新增加的nacos配置文件
4.6服务中也可以看到当前项目注册了进来
4.7验证配置更新
-
请求一下数据是多少
-
更新nacos上的配置
-
验证是否更新
参考文献
https://spring.io/projects/spring-cloud
https://nacos.io/zh-cn/docs/quick-start-spring-boot.html
https://www.bilibili.com/video/BV1VJ411X7xX?from=search&seid=8391427250147453041&spm_id_from=333.337.0.0