模块:
1.父工程(管理版本)
2.公共模块:Common-API
3.测试模块:NacosClusterConfigConsumer80
版本:
springboot:2.7.6
springcloud:2021.0.5
spring-cloud-alibaba-dependencies:2021.0.1.0
nacos:2.1.2
nginx反向代理:nginx-1.18.0
注:nacos官方建议使用linux来配置集群,这里只是用来说明简单的配置,所以此配置在windows上了,后期补充Linux的搭建
<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.0modelVersion>
<groupId>com.qwygroupId>
<artifactId>springcloudartifactId>
<version>1.0-SNAPSHOTversion>
<modules>
<module>EurekaServer7001module>
<module>Common-APImodule>
<module>EurekaServer7002module>
<module>EurekaClientProvider8001module>
<module>EurekaClientConsumer80module>
<module>EurekaClientProvider8002module>
<module>ZookeeperClientProvider8001module>
<module>ZookeeperClientProvider8002module>
<module>ZookeeperClientConsumer80module>
<module>ConsulClientProvider8001module>
<module>ConsulClientProvider8002module>
<module>ConsulClientConsumer80module>
<module>NacosClientProvider8001module>
<module>NacosClientProvider8002module>
<module>NacosClientConsumer80module>
<module>NacosClientConifgProvider8001module>
<module>NacosClientConfigConsumer80module>
<module>NacosClusterConfigConsumer80module>
modules>
<packaging>pompackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit.version>4.12junit.version>
<log4j.version>1.2.17log4j.version>
<lombok.version>1.16.18lombok.version>
<mysql.version>5.1.47mysql.version>
<druid.version>1.1.16druid.version>
<mybatis.spring.boot.version>2.2.2mybatis.spring.boot.version>
<mybatis.plus.boot.version>3.5.2mybatis.plus.boot.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.7.6version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>2021.0.5version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2021.0.1.0version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.20version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>${mybatis.plus.boot.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis.spring.boot.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
<optional>trueoptional>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
<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">
<parent>
<artifactId>springcloudartifactId>
<groupId>com.qwygroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>NacosClusterConfigConsumer80artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bootstrapartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.qwygroupId>
<artifactId>Common-APIartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.20version>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-loadbalancerartifactId>
dependency>
dependencies>
project>
server:
port: 80
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb2
username: root
password: admin
cloud:
nacos:
server-addr: localhost:8848 # nginx中的访问地址,本来应该为80端口,但是其他程序占用了,所以改为8848了
spring:
application:
name: nacos-client-config-consumer #应用程序的名称
cloud:
nacos:
config:
file-extension: yaml #配置文件的后缀
name: nacos-client-config-consumer-dev.yaml # 对应data-id(注:没必要和应用程序名一致,建议一致)
group: CONSUMER_CONFIG_GROUP # 配置文件所在的组
namespace: b077e355-cb5a-4bb8-a7a1-64a9d7121e61 #配置文件所在的命名空间
shared-configs: #配置共享配置信息
- data-id: application-common.yml
group: CONSUMER_CONFIG_GROUP
refresh: true #设置可以自动刷新
- data-id: application-custom.yml
group: CONSUMER_CONFIG_GROUP
refresh: false #设置不可以自动刷新
discovery: # 服务的注册与发现
cluster-name: JN # 应用程序所在的集群
namespace: b077e355-cb5a-4bb8-a7a1-64a9d7121e61 #服务所在的命名空间(注:没必要非得跟配置文件在同一个命名空间)
group: DEV_GROUP
profiles:
active: dev
package com.qwy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author:齐文勇
* @Package:com.qwy
* @Project:springcloud1
* @name:NacosClusterConfigConsumer80
* @Date:2022/12/17 1:52
* @Filename:NacosClusterConfigConsumer80
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosClusterConfigConsumer80 {
public static void main(String[] args) {
SpringApplication.run(NacosClusterConfigConsumer80.class,args);
}
}
package com.qwy.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @Author:齐文勇
* @Package:com.qwy.controller
* @Project:springcloud1
* @name:OrderController
* @Date:2022/12/11 4:15
* @Filename:OrderController
*/
@RestController
@RefreshScope //自动刷新
@RequestMapping("/config")
public class ConfigController {
@Value("${pattern.dateformat}")
private String dateformat;
@Value("${common.info}")
private String commonInfo;
@Value("${custom.info}")
private String customInfo;
@GetMapping("/now")
public String now(){
String message="时间:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat))
+"
系统共享信息:"+commonInfo
+"
用户共享信息:"+customInfo;
return message;
}
}
该SQL脚本在nacos安装的目录下
因为官方要求MySQL数据库的版本为5.6.+,所以我对SQL脚本进行了修改:
/*
* 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.
*/
select current_timestamp()
/******************************************/
/* 数据库全名 = 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 comment '创建时间',
`gmt_modified` datetime 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 comment '创建时间',
`gmt_modified` datetime 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 comment '创建时间',
`gmt_modified` datetime 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 comment '创建时间',
`gmt_modified` datetime 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 ,
`gmt_modified` datetime ,
`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 comment '创建时间',
`gmt_modified` datetime 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');
三个nacos节点的地址:
节点 | ip | port |
---|---|---|
nacos1 | 127.0.0.1 | 8840 |
nacos2 | 127.0.0.1 | 8850 |
nacos3 | 127.0.0.1 | 8860 |
在cluster.conf中添加如下内容
127.0.0.1:8840
127.0.0.1:8850
127.0.0.1:8860
然后修改application.properties文件,添加数据库配置:
输入:
### Specify local server's IP:
nacos.inetutils.ip-address=127.0.0.1
#*************** Config Module Related Configurations ***************#
### 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/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=admin
然后分别修改三个文件夹中的application.properties
nacos1:
server.port=8840
nacos2:
server.port=8850
nacos3:
server.port=8860
注:nacos配置集群式port不能式连续的数值
启动三个nacos
D:\Program Files\nacos-server-2.1.2\nacos1\bin>startup.cmd
D:\Program Files\nacos-server-2.1.2\nacos2\bin>startup.cmd
D:\Program Files\nacos-server-2.1.2\nacos3\bin>startup.cmd
解压到任意非中文目录下
upstream nacos-cluster {
server 127.0.0.1:8840;
server 127.0.0.1:8850;
server 127.0.0.1:8860;
}
server {
listen 8848;
server_name localhost;
location /nacos {
proxy_pass http://nacos-cluster;
}
}
启动nginx
D:\Program Files\nginx-1.18.0>start nginx.exe
web浏览器访问:
http://localhost:8848/nacos
代码中application.yml文件配置如下:
spring:
cloud:
nacos:
server-addr: localhost:8848 # nginx中的访问地址,本来应该为80端口,但是其他程序占用了,所以改为8848了
web浏览器访问:
http://localhost/config/now
查看数据库:
至此window上的nacos的集群与持久化配置完成。