image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
nacos的安装:
https://www.cnblogs.com/liubaihui/p/13569380.html

默认为集群模式,设置为单机模式后,进入登录界面 账号和密码都是nacos

nacos注册中心

image.png
注册中心成功后,调用和之前的feign的调用方法 没有区别
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html 官网参考

重要:版本说明
https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

  1. 添加依赖 ```java <?xml version=”1.0” encoding=”UTF-8”?>

    4.0.0 com.itheima nacos-client104 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-dependencies Greenwich.SR1 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.2.RELEASE pom import
  1. 2. application.yml配置nacos注册中心的地址
  2. ```yaml
  3. spring:
  4. application:
  5. name: nacos-client
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: localhost:8848 # 注册给服务端的地址
                                3. 启动文件
package com.itheima;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient // 启用服务注册发现
public class NacosClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosClientApplication.class,args);
    }

    @RestController
    public class TestController(){
        @GetMapping("/hello")
        public String hello(){
            return "hello";
        }
    }

}
     4. 在nacos管理界面中即可看到内容<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12589476/1632228055655-a5899f1a-add8-4fb9-913f-102ee7f94507.png#clientId=ua807797a-8c6c-4&from=paste&height=431&id=u6d218e97&margin=%5Bobject%20Object%5D&name=image.png&originHeight=431&originWidth=1116&originalType=binary&ratio=1&size=46952&status=done&style=none&taskId=u185b87b0-24ea-4f76-bb42-34c191ad306&width=1116)

nacao配置中心的使用

  1. 在之前项目基础上添加依赖

    <!-- nacos配置中心依赖包 -->
    <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    
  2. nacos-web工程添加配置文件bootstrap.yml 同时删除之前旧的application.yml ```yaml spring: application: name: nacos-client cloud: nacos: server-addr: localhost:8848 # 这个配置可以替换上面两个配置 discovery:

     server-addr: ${spring.cloud.nacos.server-addr} # 注册给服务端的地址
    

    config:

     server-addr: ${spring.cloud.nacos.server-addr} # 配置中心的地址
     file-extension: yaml  # 默认是使用properties文件作为后缀
    

3. 添加配置

![image.png](https://cdn.nlark.com/yuque/0/2021/png/12589476/1632231044062-1a371538-3852-496b-a132-5889c4592b99.png#clientId=ua807797a-8c6c-4&from=paste&height=476&id=u8303fe74&margin=%5Bobject%20Object%5D&name=image.png&originHeight=476&originWidth=1125&originalType=binary&ratio=1&size=48048&status=done&style=none&taskId=uff10200d-0719-4188-bae6-11c2d6f5bb7&width=1125)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12589476/1632231404052-8fc059a9-5d25-45dc-8645-017471d82fe0.png#clientId=ua807797a-8c6c-4&from=paste&height=444&id=u9808f249&margin=%5Bobject%20Object%5D&name=image.png&originHeight=444&originWidth=659&originalType=binary&ratio=1&size=36710&status=done&style=none&taskId=u06ccb4f0-f8b1-45bb-a453-e47106072da&width=659)

4. 启动类
```java
package com.itheima;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient // 启用服务注册发现
public class NacosClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosClientApplication.class,args);
    }

    @RestController
    @RefreshScope //刷新bean 刷新配置 
    public class TestController{
        @Value("${jdbc.url:123456}")
        private String url;

        @GetMapping("/hello")
        public String hello(){
            System.out.println("url:"+url);
            return "hello";
        }
    }
}

5、 启动测试
image.png
注意: @RefreshScope //刷新bean 刷新配置 该注解添加后,修改nacos配置中心的配置文件后,程序就可以动态使用配置文件中的数据了!

nacos配置中心进阶

image.png
image.png
image.png
image.png
image.png

3.3 新建命名空间
image.png
image.png
3.4 配置列表中,进入dev 新建配置文件
image.png

3.5 修改bootstrap.yml配置文件
image.png
总结: 通过命名空间和组,可以在客户端中指定具体那个配置文件生效,这样就可以区分不同的开发环境不同的项目使用不同的配置文件了(配置文件的名字相同,内容不同)!

nacos持久化配置

image.png
image.png
image.png
3.3 重启nacos服务器,这个时候可以看到配置文件都没有了,重新创建命名空间dev,添加新的配置文件
image.png
image.png
image.png

3.4 具体导入配置信息的方法 最好还是再百度一下
image.png

nacos集群搭建 具体内容百度

image.png
image.png
image.png

nacos的Sentinel 等等其他功能 自学
image.png