关于Nacos
一、nacos的介绍
1、什么是nacos?
na代表Naming
co代表Configuration
s代表Service
nacos是alibaba的一个配置中心和注册中心
2、nacos的启动器
spring-cloud-starter-alibaba-nacos-discovery
spring-cloud-starter-alibaba-nacos-config
二、nacos的安装和启动
1、下载
自行百度搜索
2、上传并解压
/usr/upload //上传至指定目录
cd /usr/upload //跳转至指定目录
tar -zxvf nacos-server-1.4.1.tar.gz -C /usr/local //解压至指定目录
3、启动和关闭
cd /usr/local/nacos/bin //跳转
启动:
./startup.sh -m standalone
关闭:
./shutdown.sh
4、测试:
http://192.168.233.128:8848/nacos
默认的用户名和密码:nacos/nacos
三、nacos注册中心
public class User {
private Integer id;
private String name;
private Integer age;
public User() {
}
public User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
1、提供者(nacos_provider)
UserService
public interface UserService {
User getUserById(Integer id);
}
UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Override
public User getUserById(Integer id) {
return new User(id,"王粪堆",18);
}
}
ProviderController
@RestController
@RequestMapping("/provider")
public class ProviderController {
@Autowired
private UserService userService;
@RequestMapping("/getUserById/{id}")
public User getUserById(@PathVariable Integer id){
return userService.getUserById(id);
}
}
pom.xml
spring-boot-starter-web
spring-cloud-starter-alibaba-nacos-discovery
application.yml
server:
port: 8090
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.233.128:8848 #nacos注册中心的地址
application:
name: nacos-provider #注册到nacos的服务名
NacosProviderApp.Class
@SpringBootApplication
@EnableDiscoveryClient //允许注册自己并发现其他服务
public class NacosProviderApp {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApp.class, args);
}
}
2、消费者(nacos_consumer)
pom.xml
spring-boot-starter-web
spring-cloud-starter-alibaba-nacos-discovery
application.yml
server:
port: 80
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.58.129:8848 #nacos注册中心的地址
application:
name: nacos-provider #注册到nacos的服务名
NacosConumserApp.Class
@SpringBootApplication
@EnableDiscoveryClient //允许注册自己并发现其他服务
public class NacosConumserApp {
public static void main(String[] args) {
SpringApplication.run(NacosConumserApp.class, args);
}
}
BeanConfig
@Configuration
public class BeanConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
ConsumerController
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
//springcloud提供的一个工具类:发现服务
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/getUserById/{id}")
public User getUserById(@PathVariable Integer id){
//问题:1、ip和port硬编码 2、不支持负载均衡 --->Map<nacos-provider, List<127.0.0.1:8090>>
//String url = "http://127.0.0.1:8090/provider/getUserById/"+id;
ServiceInstance instance = discoveryClient.getInstances("nacos-provider").get(0);
System.out.println("ip:"+instance.getHost()+", "+"port:"+instance.getPort());
//问题: 2、不支持负载均衡 --->Map<nacos-provider, List<127.0.0.1:8090>>
String url = "http://"+instance.getHost()+":"+instance.getPort()+"/provider/getUserById/"+id;
return restTemplate.getForObject(url, User.class);
}
}
四、nacos的配置中心(nacos_config)
1、集中管理配置文件
pom.xml
spring-cloud-starter-alibaba-nacos-config
spring-cloud-starter-alibaba-nacos-discovery
spring-boot-starter-web
bootstrap.yml
spring:
cloud:
nacos:
config:
server-addr: 192.168.233.128:8848 #nacos配置中心的地址
prefix: nacos-config #前缀,默认值是${spring.application.name}
file-extension: yaml #后缀,默认值是properties
NacosConfigApp
@SpringBootApplication
@EnableDiscoveryClient //把自己注册到nacos,并从nacos中发现其他服务
public class NacosConfigApp {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApp.class, args);
}
}
在http://192.168.233.128:8848/nacos配置中心添加
Data ID: nacos-config.yaml
prefix: nacos-config #前缀,默认值是${spring.application.name}
file-extension: yaml #后缀,默认值是properties
格式:YAML
内容:
server:
port: 8090
spring:
cloud:
nacos:
discovery:
server-addr: http://192.168.58.129 #nacos注册中心的地址
namespace: dev
group: springcloud_parent
application:
name: nacos-config #注册到nacos的服务名
2、区分环境
配置管理模型
namespace ---------- 环境:dev、test、prod
group ---------- 项目:springcloud_parent、jd_parent
配置隔离
①bootstrap.yml
spring:
cloud:
nacos:
config:
server-addr: 192.168.58.129:8848
namespace: dev #开发环境:dev、test、prod
group: springcloud_parent #项目名:jd_parent
prefix: nacos-config
file-extension: yaml
②nacos
新建namespace:dev
创建配置文件,并且group命名为:springcloud_parent
3、实时更新配置文件
ConfigController
@RestController
@RefreshScope //重新从BeanFactory获取一个新的实例(该实例使用新的配置)
public class ConfigController {
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.type}")
private String type;
@GetMapping("/config/info")
public String getConfigInfo() {
System.out.println(this);
String configInfo = driverClassName+"<br>"+url+"<br>"+username+"<br>"
+password+"<br>"+type;
return configInfo;
}
}
测试实时更新最新配置文件
注意:logback.xml
原理:nacos监听md5
五、nacos的持久化
1、为什么要持久化?
nacos有自带的嵌入式数据库derby,如果搭建nacos集群则每台的配置文件都不一样
mysql安装
1、下载rpm文件
wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
2、安装rpm
rpm -ivh mysql57-community-release-el7-10.noarch.rpm
3、安装mysql
yum -y install mysql-community-server --nogpgcheck
4、测试
systemctl start mysqld
systemctl status mysqld
systemctl enable mysqld
systemctl restart mysqld
systemctl stop mysqld
5、查找密码
grep "password" /var/log/mysqld.log
6、登录mysql
mysql -u root -p
7、修改密码策略
mysql> SET GLOBAL validate_password_policy=LOW;
mysql> SET GLOBAL validate_password_length=4;
8、修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '1111';
9、开启远程登录权限
mysql> use mysql;
mysql> select User,Host from user;
mysql> update user set host ="%" where user = "root";
mysql> flush privileges;
2、持久化
切换数据库
vim /usr/local/nacos/conf/application.properties:
### If use MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://192.168.58.128:3306/nacos?characterEncoding=utf8&connectTimeout=10000&socketTimeout=30000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=1111
建库建表
①新建数据库:nacos
②建表:
找到/usr/local/nacos/conf/nacos-mysql.sql并在Navicat执行
测试
①重启nacos
②上传配置文件,观察是否持久化到mysql
六、nacos的集群搭建
1、问题
1)为什么是3台?投票选举leader
2)为什么要选leader?同步数据
3)如何实现负载均衡?nginx
2、搭建步骤
1)修改nacos的cluster.conf,配置3台nacos的通讯地址
cd /usr/local/nacos/conf
cp cluster.conf.example cluster.conf
vim cluster.conf:
192.168.58.129:8850
192.168.58.129:8851
192.168.58.129:8852
2)复制3台nacos
cd /usr/local
mkdir nacos_cluster
cp -r nacos nacos_cluster/nacos_8850
cp -r nacos nacos_cluster/nacos_8851
cp -r nacos nacos_cluster/nacos_8852
3)修改3台nacos的ip
vim /usr/local/nacos_cluster/nacos_8850/conf/application.properties:
server.port=8850
vim /usr/local/nacos_cluster/nacos_8851/conf/application.properties:
server.port=8851
vim /usr/local/nacos_cluster/nacos_8852/conf/application.properties:
server.port=8852
4)启动nacos集群
cd /usr/local/nacos_cluster/nacos_8850/bin
./startup.sh
cd /usr/local/nacos_cluster/nacos_8851/bin
./startup.sh
cd /usr/local/nacos_cluster/nacos_8852/bin
./startup.sh
5)测试
注册微服务到8850,观察8851和8852是否同步数据
3、搭建nginx
1)安装c语言的环境
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
2)下载nginx
cd /usr/upload
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
3)解压
tar -zxvf nginx-1.12.0.tar.gz
4)配置安装路径
cd nginx-1.12.0
./configure --prefix=/usr/local/nginx
5)编译并安装
make && make install
6)开启和关闭
cd /usr/local/nginx/sbin
开启:
./nginx
关闭:
./nginx -s stop
重新加载配置文件:
./nginx -s reload
7)配置nginx代理nacos集群
upstream myNacos{
server 192.168.58.129:8850;
server 192.168.58.129:8851;
server 192.168.58.129:8852;
}
server {
#请求的映射规则,/代表任意url
location / {
proxy_pass http://myNacos;
}
}
七、nacos的开机自启
1、添加开机自启脚本文件
vim /lib/systemd/system/nacos.service:
[Unit]
Description=nacos
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nacos/bin/startup.sh -m standalone
ExecReload=/usr/local/nacos/bin/shutdown.sh
ExecStop=/usr/local/nacos/bin/shutdown.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2、修改nacos的starup.sh
vim /usr/local/nacos/bin/startup.sh:
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/local/jdk1.8.0_191
#[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java
#[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/opt/taobao/java
#[ ! -e "$JAVA_HOME/bin/java" ] && unset JAVA_HOME
3、设置开机执行nacos.service
systemctl daemon-reload
systemctl start nacos.service
systemctl status nacos.service
systemctl enable nacos.service
systemctl stop nacos.service