nacos

作者: 码农GG | 来源:发表于2020-12-18 10:07 被阅读0次

    1.Nacos简介

    1.1概述

    前四个字母分别为Naming和Configuration的前两个字母,最后的s为Service
    一个更易于构建云原生应用的动态服务发现,配置管理和服务管理中心
    Nacos就是注册中心+配置中心的组合(Nacos = Eureka+Config+Bus)

    替代Eureka做服务注册中心
    替代Config做服务配置中心
    替代Bus做总线

    1.2下载

    地址: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_discovery

    1.3各注册中心比较

    image.png

    2.安装并运行Nacos

    先从官网下载Nacos
    https://github.com/alibaba/nacos/releases/tag/1.4
    解压安装包,直接运行bin目录下的startup.cmd
    命令运行成功后直接访问http://localhost:8848/nacos,默认账号密码都是nacos

    image.png

    3.Nacos作为服务注册中心演示

    3.1基于Nacos的服务提供者

    3.1.1新建Module
    cloudalibaba-provider-payment9001

    3.1.2POM依赖

    <!--spring cloud alibaba 2.1.0.RELEASE-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>2.1.0.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    
    <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><dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.62</version>
    </dependency>
     </dependencies>
     
    

    3.1.3YML配置

    server:
      port: 9001
    
    spring:
      application:
        name: nacos-payment-provider
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848 #配置Nacos地址
    #配置监控
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    

    3.1.4主启动

    @EnableDiscoveryClient //使用nacos
    @SpringBootApplication
    public class PaymentMain9001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain9001.class,args);
        }
    }
    

    3.1.5业务类

    @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;
        }
    }
    

    3.1.6测试

    http://lcoalhost:9001/payment/nacos/1

    image.png

    3.1.7 nacos自带负载均衡

    3.2基于Nacos的服务消费者

    3.2.1新建Module
    cloudalibaba-consumer-nacos-order83

    3.2.2POM依赖

    <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>
    

    3.2.3YML配置

    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
    

    3.2.4 主启动

    @EnableDiscoveryClient
    @SpringBootApplication
    public class OrderNacosMain83
    {
        public static void main(String[] args)
        {
            SpringApplication.run(OrderNacosMain83.class,args);
        }
    }
    

    3.2.5业务类

    @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);
        }
    }
    

    3.2.6测试

    image.png
    http://localhost:83/consumer/payment/nacos/13 轮询负载均衡访问9001,9002

    3.3服务注册中心对比

    3.3.1图示

    image.png image.png

    3.3.2Nacos支持AP和CP模式的切换

    image.png

    4.Nacos作为服务配置中心演示

    4.1Nacos作为配置中心-基础配置

    cloudalibaba-config-nacos-client3377

    4.1.2POM依赖

    <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>
    

    4.1.3YML配置
    why配置两个

    image.png

    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格式的配置
    

    application.yml

    spring:
      profiles:
        active: dev
    

    4.1.4主启动

    @EnableDiscoveryClient
    @SpringBootApplication
    public class NacosConfigClientMain3377
    {
        public static void main(String[] args) {
            SpringApplication.run(NacosConfigClientMain3377.class, args);
        }
    }
    

    4.1.5业务类

    @RestController
    @RefreshScope  //实现配置自动更新
    public class ConfigClientController
    {
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/config/info")
        public String getConfigInfo() {
            return configInfo;
        }
    }
    

    4.2在Nacos中添加配置信息

    4.2.1Nacos中的匹配规则
    https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

    image.png

    4.2.2新增配置

    image.png
    image.png

    4.2.3设置DataId

    ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
    prefix默认为spring.application.name的值
    spring.profile.active既为当前环境对应的profile,可以通过配置项spring.profile.active 来配置
    file-exetension为配置内容的数据格式,可以通过配置项spring.cloud.nacos.config.file-extension配置
    
    image.png

    4.2.4测试
    启动前需要在nacos客户端-配置管理-配置管理栏目下有没有对应的yaml配置文件
    运行cloud-config-nacos-client3377的主启动类
    调用接口查看配置信息http://localhost:3377/config/info
    自带动态刷新,修改下Nacos中的yaml配置文件,再次调用查看配置的接口,就会发现配置已经刷新

    4.3Nacos作为配置中心-分类配置

    image.png

    4.3.1配置管理

    image.png

    4.3.2命名空间

    image.png

    4.3.3Namespace+Group+Data ID三者关系?为什么这么设计?

    image.png
    image.png

    4.3.4DataID方案
    指定spring.profile.active和配置文件的DataID来使不同环境下读取不同的配置
    默认空间+默认分组+新建dev和test两个DataID
    通过spring.profile.active属性就能进行多环境下配置文件的读取

    image.png

    测试
    http://localhost:3377/config/info

    4.3.5Group方案
    通过Group实现环境区分

    在nacos图形界面控制台上面新建配置文件DataID

    image.png

    在config下增加一条group的配置即可。可配置为DEV_GROUP或TEST_GROUP

    image.png

    4.3.6Namespace方案
    新建dev/test的Namespace

    image.png

    回到服务管理-服务列表查看

    image.png

    按照域名配置填写

    image.png

    YML配置

    server:
      port: 3377
    
    spring:
      application:
        name: nacos-config-client
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.1.111:1111 #服务注册中心地址
          config:
            server-addr: 192.168.1.111:1111 #配置中心地址
            file-extension: yaml #指定yaml格式的配置
            group: TEST_GROUP
            namespace: 8bc3b5e4-1ebe-4237-aa89-138589155c48
    

    5.Nacos集群和持久化配置

    5.1官网

    https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
    https://nacos.io/zh-cn/docs/deployment.html

    5.1.2架构图

    image.png

    5.1.3高可用

    image.png

    5.2Nacos持久化配置

    Nacos默认自带的是嵌入式数据库derby
    https://github.com/alibaba/nacos/blob/develop/config/pom.xml
    derby到mysql切换配置步骤
    \nacos\conf\nacos-mysql.sql 目录下找到sql脚本
    \nacos\conf目录下找到application.properties

    spring.datasource.platform=mysql
     
    db.num=1
    db.url.0=jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
    db.user=root
    db.password=123456
    

    5.3Linux版Nacos+MySQL生产环境配置

    5.3.1.Linux服务器上mysql数据库配置

    image.png
    image.png

    5.3.2.application.properties配置

    \nacos\conf目录下找到application.properties

    spring.datasource.platform=mysql
     
    db.num=1
    db.url.0=jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
    db.user=root
    db.password=123456
    

    5.3.3.Linux服务器上nacos的集群配置cluster.conf

    image.png
    image.png

    这个IP不能写127.0.0.1,必须是Linux命令hostname -i能够识别的IP

    5.3.4.编辑Nacos的启动脚本startup.sh,使它能够接受不同的启动端

    5.3.5.Nginx的配置,由它作为负载均衡器

     #gzip  on;
        upstream cluster{
    
          server 127.0.0.1:8846;
          server 127.0.0.1:8847;
          server 127.0.0.1:8848;
        }
        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;
    
            }
    

    5.3.6.截止到此处,1个Nginx+3个nacos注册中心+1个mysql
    测试通过nginx访问nacos集群
    https://192.168.1.111:1111/nacos/#/login
    新建一个配置测试

    image.png

    linux服务器的mysql插入一条记录

    image.png

    5.4测试

    微服务cloudalibaba-provider-payment9002启动注册进nacos集群

    image.png
    image.png

    相关文章

      网友评论

          本文标题:nacos

          本文链接:https://www.haomeiwen.com/subject/vvvfiktx.html