B站视频:https://www.bilibili.com/video/BV18E411x7eT
SpringCloud NetFlix Projects Entering Maintenance Mode
是什么
官网https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md
能干嘛
官网https://spring.io/projects/spring-cloud-alibaba#overview
文档
https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html
前四个字母分别为Naming和Confiquration的前两个字母,最后的s为Service
一个更易于构建云原生应用的动态服务发现,配置管理和服务管理中心
Nacos就是注册中心+配置中心的组合
Nacos = Eureka+Config+ Bus
官网
https://nacos.io/zh-cn/index.html
https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_nacos_discovery
https://nacos.io/zh-cn/index.html
找到下载linux版本
移植到Linux的/opt上
tar -zxvf nacos-server-1.4.1.tar.gz
解压后
进入nacos文件夹的bin文件夹
可以看到两个脚本,启动和关闭
单机运行
sh startup.sh -m standalone
可以看到日志的文章
cat /opt/nacos/logs/start.out
防火墙开启8848端口
开启端口
firewall-cmd --zone=public --add-port=8848/tcp --permanent
查看开启的端口
firewall-cmd --list-ports
重启防火墙
firewall-cmd --reload
访问控制台
http://192.168.137.128:8848/nacos/index.html(注意地址是自己的)
默认账号密码为nacos
关闭nacos
sh shutdown.sh
mySpringCloudAlibaba
删除不必要的文件
pom
<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.yezhinaogroupId>
<artifactId>mySpringCloudAlibabaartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>cloudalibaba-provider-payment9001module>
modules>
<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>1.3.0mybatis.spring.boot.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.2.2.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
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>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>
<configuration>
<fork>truefork>
<addResources>trueaddResources>
configuration>
plugin>
plugins>
build>
project>
POM
<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>mySpringCloudAlibabaartifactId>
<groupId>com.yezhinaogroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloudalibaba-provider-payment9001artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.62version>
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.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>
dependencies>
project>
application.yml
server:
port: 9001
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: 192.168.137.128:8848 #配置Nacos地址
#暴露端口
management:
endpoints:
web:
exposure:
include: '*'
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain9001.class,args);
}
}
添加PaymentController
@RestController
public class PaymentController
{
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/nacos/{id}")
public String getPayment(@PathVariable("id") Integer id)
{
return "nacos registry, serverPort: "+ serverPort+"\t id"+id;
}
}
启动成功后可以看到注册信息
为了表现Nacos的负载均衡,新建一个cloudalibaba-provider-payment9002
只需要修改application.yml和启动类
POM
<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>mySpringCloudAlibabaartifactId>
<groupId>com.yezhinaogroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloudalibaba-consumer-nacos-order83artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
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.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>
dependencies>
project>
application.yml
server:
port: 83
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: 192.168.137.128:8848 #配置Nacos地址
service-url:
nacos-user-service: http://nacos-payment-provider
@SpringBootApplication
@EnableDiscoveryClient
public class OrderNacosMain83 {
public static void main(String[] args) {
SpringApplication.run(OrderNacosMain83.class,args);
}
}
@Configuration
public class ApplicationContextConfig {
//配置负载均衡
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
@Slf4j
public class OrderNacosController {
@Resource
private RestTemplate restTemplate;
@Value("${service-url.nacos-user-service}")
private String serverURL;
@GetMapping(value = "/consumer/payment/nacos/{id}")
public String paymentInfo(@PathVariable("id") Long id)
{
return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);
}
}
启动三个服务
访问http://localhost:83/consumer/payment/nacos/1
可以发现轮询调用提供者
Nacos全景图所示
Nacos和CAP
Nacos支持AP和CP模式的切换
POM
<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>mySpringCloudAlibabaartifactId>
<groupId>com.yezhinaogroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloudalibaba-config-nacos-client3377artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
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.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>
dependencies>
project>
配置两个yml
bootstrap.yml
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: 192.168.137.128:8848 #服务注册中心地址
config:
server-addr: 192.168.137.128:8848 #配置中心地址
file-extension: yaml #指定yaml格式的配置
#${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
#nacos-config-client-dev.yaml
application.yml
spring:
profiles:
active: dev #开发环境
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigClientMain3377 {
public static void main(String[] args) {
SpringApplication.run(NacosConfigClientMain3377.class,args);
}
}
@RestController
@RefreshScope
public class ConfigClientController
{
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo() {
return configInfo;
}
}
匹配规则
Nacos中的dataid的组成格式与SpringBoot配置文件中的匹配规则
官网
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
新增配置
设置Datald
DATA ID
nacos-config-client-dev.yaml
配置内容
config:
info: “config info for dev, version = 1”
dataid的配置规则
p r e f i x − {prefix}- prefix−{spring.profiles.active}.${file-extension}
因为prefix默认为spring.application.name的值,所以也可以是
file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension
s p r i n g . a p p l i c a t i o n . n a m e − {spring.application.name}- spring.application.name−{spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
注意:
因为file-extension: yaml,指定了yaml,即使dataid的后缀为yml也是不行的。
配置添加后,启动cloudalibaba-config-nacos-client3377
访问http://localhost:3377/config/info
修改配置
因为添加了@RefreshScope,实现配置自动更新
问题
由Namespace+DataId+Group确定一个配置文件
nacos-config-client-dev.yaml
config:
info: "nacos-config-client-dev.yaml,version = 7"
nacos-config-client-test.yaml
config:
info: "nacos-config-client-test.yaml,version = 7"
http://localhost:3377/config/info
test
dev
通过Group实现环境区分
config:
info: "nacos-config-client-test.yaml,TEST_GROUP,version = 7"
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O27mPG57-1616568677008)(https://gitee.com/yezhinao/picture/raw/master/image/20210323145228.png)]
application.yml为测试环境
http://localhost:3377/config/info
test空间
新建后可以在配置列表里查看到
config:
info: "nacos-config-client-test.yaml,TEST_GROUP,namespace:test,version = 7"
application.yml为测试环境
可以在命名空间里查看命名空间的ID
http://localhost:3377/config/info
配置中心的配置查找顺序:NameSapce->Group->DataID
https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
上图的解析
https://nacos.io/zh-cn/docs/deployment.html
Nacos持久化配置解释
Nacos默认自带的是嵌入式数据库derby
https://github.com/alibaba/nacos/blob/develop/config/pom.xml
从这一张图片来看,我们的Centos7,Nginx,而且nacos持久化支持Mysql数据库,所以还需要安装Mysql
安装参考链接
https://blog.csdn.net/EB_NUM/article/details/105425622
#下载MySQL 安装包
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
#安装mysql 安装源:
yum -y localinstall mysql57-community-release-el7-11.noarch.rpm
#在线安装MySQL
yum -y install mysql-community-server
#启动mysql 服务
systemctl start mysqld
#设置开机启动
systemctl enable mysqld
systemctl daemon-reload
vim /var/log/mysqld.log
mysql -u root -p
很不巧可能是因为我的临时密码携带!这个特殊字符不能进入
如果临时密码有效请直接查看④设置远程登录或参考文档
参考资料
https://blog.csdn.net/lydia88/article/details/104838461
#进入mysql配置文件
vim /etc/my.cnf
#加上这句话
skip-grant-tables
#重启服务
systemctl restart mysqld
mysql -u root -p
use mysql
#修改密码
update user set authentication_string=password('123456') where user='root';
flush privileges;
返回修改配置文件
vim /etc/my.cnf
重启服务
#重启服务
systemctl restart mysqld
#修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
#远程登录
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
vim /etc/my.cnf
character_set_server=utf8
init_connect='SET NAMES utf8'
#重启服务
systemctl restart mysqld
#查看编码
show variables like '%character%';
#防火墙开启端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
#重启防火墙
firewall-cmd --reload
#查看防火墙开启的端口
firewall-cmd --list-ports
远程测试连接
安装参考链接
https://www.jianshu.com/p/97cdbeebef96
启动参考链接
https://blog.csdn.net/qq_37345604/article/details/90034424
最后防火墙将80端口开启
nginx环境配置完成
找到nacos安装目录的nacos-mysql.sql
将其取出
新建一个数据库名为nacos_config
导入nacos-mysql.sql文件
找到application.properties的位置
添加配置
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=UTC
db.user=root
db.password=123456
尝试启动
./startup.sh
查看日志是否报错
cat /opt/nacos/logs/start.out
如果出现下面错误,仔细检查application.properties配置
找到cluster.conf.example,并且拷贝为cluster.conf
cluster.conf是集群生效的文件
#查看ip
hostname -I
假设使用3台机器做集群,则可以使用3个不同的端口做伪集群
编辑cluster.conf
#备份一份startup.sh
cp startup.sh startup.sh.bak
#修改startup.sh
vim startup.sh
nohup $JAVA -Dserver.port=${EMBEDDED_STORAGE} ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
修改后,可以使用指定端口启动
#使用指定端口启动
./startup.sh -p 3333
#查看启动日志
cat /opt/nacos/logs/start.out
修改成功,以指定端口的形式启动
接着模拟另外两台机器,以4444 和 5555端口启动
#使用指定端口启动
./startup.sh -p 4444
./startup.sh -p 4444
到这里就已经实现了nacos的集群配置了
下一步只需要套一层nginx实现负载均衡就可以了
#进入nginx的配置目录
cd /usr/local/nginx/conf
#备份nginx的配置文件
cp nginx.conf nginx.conf.bak
#修改配置文件
vim nginx.conf
upstream cluster{
server 127.0.0.1:3333;
server 127.0.0.1:4444;
server 127.0.0.1:5555;
}
server {
listen 1111;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://cluster;
}
....
}
启动nginx
./nginx -c /usr/local/nginx/conf/nginx.conf
防火墙打开1111端口
http://192.168.137.128:1111/nacos/index.html
完成集群部署
在配置中心配置一个yaml
mysql数据库将信息存入
微服务cloudalibaba-provider-payment9002启动注册进nacos集群
修改application.yml
运行后服务注册成功
代码:https://gitee.com/yezhinao/springcloud_alibaba/tree/master/mySpringCloudAlibaba(1)