尚硅谷—Cloud—Nacos 服务注册 和 配置中心(96~110)

目录

一:Nacos 简介

二:安装并运行 Nacos

三:Nacos 作为 服务注册中心 演示

四:Nacos 作为 服务配置中心 演示

五:Nacos 集群 和 持久化配置(重要)


一:Nacos 简介

              1)为什么叫 Nacos:
                           a:前四个字母,分别为 Naming 和 Configuration,最后的字母为 service。

              2)是什么:
                           a:一个 更易于构建 云原生应用 的 动态服务发现,配置管理 和 服务管理平台。
                           b:一句话:配置中心 + 注册中心 的组合:Nacos = Eureka + Config + Bus

              3)能干嘛:
                           a:替代 Eureka 做服务注册中心。
                           b:替代 Config + Bus 做服务配置中心

              4)去哪下:
                           a:官网:
                           b:

              5)各 注册中心比较:


 

二:安装并运行 Nacos

              1)本地 Java 8 + Maven 环境已经 OK:
              2)先从官网 下载 Nacos:
              3)解压 安装包,直接运行 bin 目录下的 startup.cmd:(单机版本启动:sh startup.sh -m standalone
              4)命令运行成功后:(直接访问:http://114.215.173.88:8848/nacos/):
              5)结果页面:



 

三:Nacos 作为 服务注册中心 演示

              1)官网文档:
                           a:
                           b:
              2)基于 Nacos 的服务提供者:
                           a:新建 Module:cloud-alibaba-provider-payment-9001
                           b:POM:
                                    - 父 pom:

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

                                    - 本模块 pom:

    <dependencies>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
        </dependency>
        <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>

</project>



                           c:YML:(来自官网)

server:
  port: 9001

spring:
  application:
    name: cloud-alibaba-provider-payment

  cloud:
    nacos:
      discovery:
        server-addr: 114.215.173.88:8848

management:
  endpoints:
    web:
      exposure:
        include: '*'


                           d:主启动:

@SpringBootApplication
@EnableDiscoveryClient
public class AlibabaProviderPayment9001 {

    public static void main(String[] args) {
        SpringApplication.run(AlibabaProviderPayment9001.class, args);
    }
    
}


                           e:业务类:

@RestController
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/getPortPayment")
    public CommonResult getPortPayment() {
        return new CommonResult(400, serverPort);
    }

}


                           f:测试:启动 nacos、启动 9001



                          g:为了下一章节演示 nacos 的负载均衡,参照 9001 ,创建 9002.





              3)基于 Nacos 的服务发现者:
                           a:新建 Module :cloud-alibaba-consumer-nacos-order-83

                           b:POM:和 9001 一样:为什么 Nacos 支持 负载均衡-Rabbin

                           c:YML:

server:
  port: 83

spring:
  application:
    name: cloud-alibaba-consumer-nacos-order-83
  cloud:
    nacos:
      discovery:
        server-addr: 114.215.173.88:8848

# 消费者 要去访问 的 微服务名称(注册成功 进 nacos 的微服务提供者)
service-url:
  nacos-user-service: http://cloud-alibaba-provider-payment


                           d:主启动:和 9001 一样:

                           e:业务类:

@Component
public class MyrestTemplate {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}
@RestController
public class OrderNacosController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String URL;

    @GetMapping(value = "/orderNacos")
    public CommonResult orderNacos() {

        CommonResult commonResult = restTemplate.getForObject(URL + "/getPortPayment", CommonResult.class);

        return commonResult;
    }

}



                           f:测试:(访问:http://127.0.0.1:83/orderNacos)(9001~9002 循环访问)


              4)服务注册中心 对比:
                           a:老版本对比:

                           b:新版本对比:Nacos 可以 AP,也可以 CP、支持切换







 

四:Nacos 作为 服务配置中心 演示


              1)Nacos 作为配置中心 - 基础配置:
                           a:新建 Module:cloud-alibaba-config-nacos-client-3377
                           b:POM:nacos-config

    <dependencies>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

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

</project>


                           c:YML:
                                     -  为什么配置两个:

server:
  port: 3377

spring:
  application:
    name: cloud-alibaba-config-nacos-client-3377

  cloud:
    nacos:
      discovery:
        server-addr: 114.215.173.88:8848

      config:
        server-addr: 114.215.173.88:8848
        file-extension: yaml
#        namespace: 793140f8-2d70-498e-864c-f2215e79ce59
#        group: DEFAULT_GROUP
# application.yml
spring:
  profiles:
    active: dev


                           d:主启动:
                           e:业务类:@RefreshScope

// 通过 spring cloud 原生注解,
// @RefreshScope 实现配置自动更新
@RefreshScope
@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping(value = "/configClient")
    public String configClient() {

        return configInfo;
    }

}


                           f:在 Nacos 中,添加配置信息:Nacos 中配置规则
                                     - 理论:Nacos 中的 Data id 的组成格式,以及与 SpringBoot 配置文件中的 匹配规则
                                                  官网:( ${prefix} - ${spring.profiles.active} . ${file-extension} )(例:项目名称-dev.yaml)


                                     - 实操:
                                 
             1.Nacos 配置新增:(cloud-alibaba-config-nacos-client-3377-dev.yaml)
                                               2.Nacos 界面 配置对应:



                                               3.历史配置:
                           g:测试:(访问:http://127.0.0.1:3377/configClient)(返回:from nacos config center,config.info.version.1
                           h:自带动态刷新:(因为  @RefreshScope 支持 Nacos 动态刷新功能)



              2)Nacos 作为配置中心 - 分类配置:
                           a:问题:多环境、多项目管理:


                           b:Nacos 的图形化管理界面:



                           c:NameSpace + Group + Data ID 三者关系?为什么这么设计?(中国-北京-朝阳区)




                           d:Case:三种方案加载机制:
                           
        - Data Id 方案:(指定不同的环境,加载不同的配置文件)




                                    - Group 方案:(根据指定不同的 分组,去 此分组中 加载 不同 配置文件)(dev/test 加载方式,仍然遵循 data id 方式)

server:
  port: 3377

spring:
  application:
    name: cloud-alibaba-config-nacos-client-3377

  cloud:
    nacos:
      discovery:
        server-addr: 114.215.173.88:8848

      config:
        server-addr: 114.215.173.88:8848
        file-extension: yaml
        group: TEST_GROUP


                                    - NameSpace 方案:(根据指定不同的 命名空间,去不同的分组下,去 不同的 命名空间 加载 配置文件)

server:
  port: 3377

spring:
  application:
    name: cloud-alibaba-config-nacos-client-3377

  cloud:
    nacos:
      discovery:
        server-addr: 114.215.173.88:8848

      config:
        server-addr: 114.215.173.88:8848
        file-extension: yaml
        namespace: 4001b5d0-05d2-4210-93b8-44e409caf501
        group: TEST_GROUP





 

五:Nacos 集群 和 持久化配置(重要)

              1)官网说明:
                           a:官网:
                           b:官网架构图:(https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html



                           c:上图 官网翻译,真实情况:


                           d:说明:



              2)Nacos 持久化 配置解释:
                           a:Nacos 默认自带 的是 嵌入式数据库 derby:


                           b:derby 到 mysql 切换配置 步骤:
                                   
 - nacos-server/nacos/conf 目录找到 sql 脚本:nacos-mysql.sql ,执行脚本,创建数据库。
                                     - nacos-server/nacos/conf 目录下 找到 application.properties ,进行数据库的配置:

##################################################################################
spring.datasource.platform=mysql

### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://114.215.173.88:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=123456

##################################################################################


                           c:启动 Nacos,可以看到 是个全新的空记录界面,以前的记录在 derby,现在记录存储到 mysql 中:


              3)Linux 版本 Nacos + Mysql 生产环境配置:

                           a:预计需要: 1 个Nginx(集群) + 3 个 Nacos 注册中心 + 1个Mysql(一主一从);
                           b:Nacos 下载 Linux 版;(官网下载)
                           c:集群配置步骤;
                                   -  1.Linux 服务器上 Mysql 数据库配置;(用 sql 脚本在 数据库创建 新的库,然后通过脚本,创建表结构)
                                   -  2.application.properties 配置;(先备份)
                                   -  3.cluster.conf:集群配置( 复制:cp cluster.conf.example cluster.conf )

# ip:port
192.168.43.44:3333
192.168.43.208:4444
192.168.43.69:5555


                                   -  4.编辑 startup.sh,使他能够接受不同的启动端口:(可不配置)(备份:cp startup.sh startup.sh.bk)







                                   -  5.Nginx 的配置,由他作为负载均衡器;
                                          修改 Nginx 配置文件,Nginx.conf,按照指定启动;



                                   -  6.截止到此处,1个 Nginx + 3个nacos + 1个 mysql

                                         sh startup.sh -p 3333 /4444 /5555

                           d:后语:如果多台服务器,就不用在启动文件中,配置指定端口启动;直接 sh 启动。nginx 也都是 8848 端口。

                           d:测试;
                                     
 - (http://192.168.43.44:1111/nacos/(增加一个配置,所有服务都增加配置)
                                       - 微服务 9002 启动,注册进 nacos 集群。(server-addr: 192.168.43.44:1111 )
                           e:高可用小总结;
                                 

                                  - 
                                  - 








 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值