模块:
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的搭建
父工程
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qwy</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>EurekaServer7001</module>
<module>Common-API</module>
<module>EurekaServer7002</module>
<module>EurekaClientProvider8001</module>
<module>EurekaClientConsumer80</module>
<module>EurekaClientProvider8002</module>
<module>ZookeeperClientProvider8001</module>
<module>ZookeeperClientProvider8002</module>
<module>ZookeeperClientConsumer80</module>
<module>ConsulClientProvider8001</module>
<module>ConsulClientProvider8002</module>
<module>ConsulClientConsumer80</module>
<module>NacosClientProvider8001</module>
<module>NacosClientProvider8002</module>
<module>NacosClientConsumer80</module>
<module>NacosClientConifgProvider8001</module>
<module>NacosClientConfigConsumer80</module>
<module>NacosClusterConfigConsumer80</module>
</modules>
<!--表明是父工程-->
<packaging>pom</packaging>
<!-- 统一管理jar包版本 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>2.2.2</mybatis.spring.boot.version>
<mybatis.plus.boot.version>3.5.2</mybatis.plus.boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version -->
<dependencyManagement>
<dependencies>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>2.7.6</version>
</dependency>-->
<!--spring boot 2.7.6-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud 2021.0.5-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2021.0.1.0-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.20</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.boot.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
NacosClusterConfigConsumer80
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qwy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>NacosClusterConfigConsumer80</artifactId>
<dependencies>
<!--nacos-config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--cloud新版本默认将bootstrap移除了,所以需要添加如下依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!--nacos-client-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.qwy</groupId>
<artifactId>Common-API</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--热部署依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.20</version>
</dependency>
<!--注意新版必须加入负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>
application.yml
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了
bootstrap.yml
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);
}
}
controller类
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))
+"<br/>系统共享信息:"+commonInfo
+"<br/>用户共享信息:"+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的配置
三个nacos节点的地址:
节点 | ip | port |
---|---|---|
nacos1 | 127.0.0.1 | 8840 |
nacos2 | 127.0.0.1 | 8850 |
nacos3 | 127.0.0.1 | 8860 |
进入nacos的conf目录,修改配置文件cluster.conf.example,重命名为cluster.conf
在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
复制三份nacos
然后分别修改三个文件夹中的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
配置nginx反向代理
解压到任意非中文目录下
修改conf/nginx.conf文件,配置如下:
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了
根据bootstrap.yml配置
web浏览器访问:
http://localhost/config/now
查看数据库:
至此window上的nacos的集群与持久化配置完成。