写在最前面: (1). nacos官网:https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html (2). nacos github中文:https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md (3). nacos下载:https://github.com/alibaba/nacos/releases/tag/1.1.4
# nacos作为注册中心 server: port: 9001 spring: application: name: nacos-payment-provider cloud: nacos: discovery: server-addr: localhost:8848 #配置Nacos地址 management: endpoints: web: exposure: include: '*' # nacas作为消费者 server: port: 83 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 service-url: nacos-user-service: http://nacos-payment-provider # nacos作为配置中心 server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 #服务注册中心地址 config: server-addr: localhost:8848 #配置中心地址 file-extension: yaml #指定yaml格式的配置 group: DEV_GROUP # 分类配置之分组配置 namespace: 3f95a639-712b-4108-a03e-7bc3e95c3d46 #哪一个namespace下 #${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} #nacos-config-client-dev.yaml <!--nacos作为注册中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--nacos作为配置中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>1>.Nacos简介(了解)
①. 为什么叫Nacos (前四个字母分别为Naming和Configuration的前两个字母,最后的s为Service)
②. 是什么
一个更易于构建云原生应用的动态服务发现,配置管理和服务管理中心Nacos:Dynamic Naming and Configuration ServiceNacos就是注册中心+配置中心的组合(等价于Nacos = Eureka+Config+Bus) ③. 能干嘛 替代Eureka做服务注册中心替代Config做服务配置中心 ④. 去哪下(https://github.com/alibaba/Nacos) 官网文档: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_discovery2>. 安装并运行Nacos(了解)
①. 本地Java8+Maven环境已经OK
②. 先从官网下载Nacos https://github.com/alibaba/nacos/releases/tag/1.1.4
③. 解压安装包,直接运行bin目录下的startup.cmd
④. 命令运行成功后直接访问http://localhost:8848/nacos 默认账号密码都是nacos
⑤. 结果页面
3>.Nacos作为服务注册中心演示
①. 新建Module: cloudalibaba-provider-payment9001
②.pom
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </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.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> </dependencies> ③. YML server: port: 9001 spring: application: name: nacos-payment-provider cloud: nacos: discovery: server-addr: localhost:8848 #配置Nacos地址 management: endpoints: web: exposure: include: '*' ④. 主启动 @EnableDiscoveryClient @SpringBootApplication public class PaymentMain9001 { public static void main(String[] args) { SpringApplication.run(PaymentMain9001.class,args); } } ⑤. 业务类 package com.atguigu.springcloud.alibaba.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @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; } } ⑥. 测试 http://lcoalhost:9001/payment/nacos/1nacos服务注册中心+服务提供者9001都ok了nacos控制台为什么nacos支持负载均衡?
①. 新建Module:cloudalibaba-consumer-nacos-order83
②.POM
<dependencies> <!--SpringCloud ailibaba nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</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.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> </dependencies> ③. YML server: port: 83 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 service-url: nacos-user-service: http://nacos-payment-provider ④. 主启动 @EnableDiscoveryClient @SpringBootApplication public class OrderNacosMain83{ public static void main(String[] args){ SpringApplication.run(OrderNacosMain83.class,args); } } ⑤. 业务类 ApplicationContextBean:配置负载均衡OrderNacosController:获取到nacos中配置的文件信息 @Configuration public class ApplicationContextBean { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } } @SuppressWarnings("all") @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/13 (83访问9001/9002,轮询负载OK )nacos控制台4>. Nacos作为配置中心-基础配置 掌握
DataID 掌握 (1). Nacos中的dataid的组成格式与SpringBoot配置文件中的匹配规则 (2). 官网 (3). 实操 ①. 配置新增nacos-config-client-dev ②. Nacos界面配置对应 ③. 设置DataId 掌握
①. 新建:cloudalibaba-config-nacos-client3377
②. POM
<dependencies> <!--nacos-config--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--nacos-discovery--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--web + actuator--> <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.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> </dependencies> ③. YML Nacos同springcloud-config一样,在项目初始化时,要保证先从配置中心进行配置拉取,拉取配置之后,此能保证项目正常启动springboot中配置文件的加载是存在优先级顺序的,bootstrap优先级高于application (1).bootstrap.yml server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 #服务注册中心地址 config: server-addr: localhost:8848 #配置中心地址 file-extension: yaml #指定yaml格式的配置 (2).application.yml spring: profiles: active: dev ④. 主启动 @EnableDiscoveryClient @SpringBootApplication public class NacosConfigClientMain3377 { public static void main(String[] args) { SpringApplication.run(NacosConfigClientMain3377.class,args); } ⑤. 业务类 @RestController @RefreshScope//支持Nacos的动态刷新功能 //@RefreshScope:修改下nacos中的yaml配置文件,再次调用查看配置的接口,就会发现配置已经刷新 public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/config/info") public String getConfigInfo() { return configInfo; } } ⑥. 在Nacos中添加配置信息 Nacos中的dataid的组成格式与SpringBoot配置文件中的匹配规则官网 实操 (1). 配置新增nacos-config-client-dev (2). Nacos界面配置对应 (3). 设置DataId 掌握 ⑦. 测试5>. Nacos作为配置中心-分类配置 掌握
①. 指定spring.profile.active和配置文件的DataID来使不同环境下读取不同的配置
②.默认空间+默认分组+新建dev和test两个DataID
新建dev配置DataID新建test配置DataID③. 通过spring.profile.active属性就能进行多环境下配置文件的读取
④. 测试 http://localhost:3377/config/info 配置是什么就加载什么 test
①. 预计需要,1个nginx+3个nacos注册中心+1个mysql
②. 安装linux环境,安装Jdk1.8、Nginx1.8、Mysql5.7.24、nacos
③. Nacos下载linux版本 链接:https://pan.baidu.com/s/131QEa12Kc1OJa1NYJncZ-g 提取码:1234
将nacos-server-1.1.4.tar.gz上传到/opt下
在/opt下解压: tar -zxvf nacos-server-1.1.4.tar.gz
将解压好的nacos,复制到/usr/local下: cp -rv nacos /usr/local
①. Linux服务器上mysql数据库配置(nacos-mysql.sql | application.properties)
②. application.properties
spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://1.7.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=root③. Linux服务器上nacos的集群配置cluster.conf 梳理出3台nacos机器的不同服务端口号(3333、4444、5555)复制出cluster.conf 这个IP不能写127.0.0.1,必须是Linux命令hostname -i能够识别的IP ④. 编辑Nacos的启动脚本startup.sh,使它能够接受不同的启动端 /mynacos/nacos/bin目录下有startup.sh修改如下
执行方式如下 查看nacos有多少个节点(ps -ef | grep nacos | grep -v grep|wc -l) ④. Nginx的配置,由它作为负载均衡器 修改nginx的配置文件 nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; 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; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } 按照指定启动 ⑤. 截止到此处,1个Nginx+3个nacos注册中心+1个mysql 测试通过nginx访问nacos(https://写你自己虚拟机的ip:1111/nacos/#/login)新建一个配置测试 linux服务器的mysql插入一条记录 ⑥. 测试:输入192.168.68.129:1111/nacos/#/login
⑦. 高可用小总结