13 Nacos

SpringCloud Alibaba入门简介

Netfilx 进入维护模式

Spring Alibaba简介

Spring官网:https://spring.io/projects/spring-cloud-alibaba
GitHub:https://github.com/alibaba/spring-cloud-alibaba
GitHub中文文档:https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md
Spring Cloud Alibaba参考文档:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html

功能

Nacos


官网:https://nacos.io/zh-cn/
GitHub:https://github.com/alibaba/Nacos

安装 nacos

windows 下 的 nacos ,在 bin 目录下 打开 cmd 输入startup.cmd

在浏览器输入 http://localhost:8848/nacos/

有 nacos 的页面 输入账户密码 都是 nacos

Nacos作为服务注册中心演示

官方文档:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html

基于Nacos的服务提供者

  1. 新建模块cloudalibaba-provider-payment9001
  2. pom
  <dependencies>
        <!--SpringCloud Alibaba nacos-->
        <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>
  1. yml
server:
  port: 9001


spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848  #配置的Nacos地址(本机的写localhost:8848,服务器的写IP地址)

# 暴露要监控的
management:
  endpoints:
    web:
      exposure:
        include: '*'
  1. 主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain9001.class,args);
    }
}
  1. controller层
@RestController
public class PaymentController {

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

    @GetMapping("/payment/nacos/{id}")
    public String getPayment(@PathVariable("id")Integer id){
        return "nacos registry,serverport:"+serverPort +"\t"+"id:"+"\t"+id;
    }
}

  1. 测试 启动9001

参照9001新建9002,建立提供者集群。

基于Nacos的服务消费者

  1. 新建模块cloudalibaba-consumer-nacos-order83
  2. pom(nacos集成了ribbon,实现负载均衡)
<dependencies>
    <!--SpringCloud Alibaba nacos-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
    <dependency>
        <groupId>com.angenin.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>

  1. yml
server:
  port: 83


spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 10.211.55.17:8848  #配置的Nacos地址(本机的写localhost:8848,服务器的写IP地址)


#消费者要访问的微服务名称(成功注册进nacos的服务提供者)
service-url:
  nacos-user-service: http://nacos-payment-provider
  1. 主启动类
@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosMain83 {

    public static void main(String[] args) {
        SpringApplication.run(OrderNacosMain83.class, args);
    }
}
  1. config
@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
  1. controller
@RestController
@Slf4j
public class OrderNacosController {

    @Resource
    private RestTemplate restTemplate;

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

    @GetMapping("/consumer/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id")Integer id){
        // 返回的是 serverURL+"/payment/nacos/  这个微服务路径下的 加上路径返回的语句
        return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);
    }
}

测试
启动9001,9002,83
在浏览器输入:http://localhost:83/consumer/payment/nacos/1

服务注册中心对比

Nacos 可以AP和CP的切换

A:可用性
C:一致性
P:分区容错性

Nacos作为服务配置中心演示

Nacos作为配置中心——基础配置

  1. 新建模块cloudalibaba-config-nacos-client3377
  2. pom
 <dependencies>
        <!-- nacos config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--SpringCloud Alibaba nacos-->
        <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>

  1. yaml

bootstrap.yml:

server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址(本机的写localhost)
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址(本机的写localhost)
        file-extension: yaml #读取指定yml格式配置

# ${spring.application.name}-${spring.profile.active}.${file-extension}
# nacos-config-client-dev.yaml


application.yml

spring:
  profiles:
    active: dev #表示开发环境  和 bootstrap 结合起来看  去
    #去 8848 上面 找 xxx-dev.yaml  xxx-test.yaml

  1. 主启动类
@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClientMain3377 {

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

}

  1. controller
@RefreshScope   //支持Nacos的动态刷新功能
@RestController
public class ConfigClientController {

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


    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }

}

  1. 在Nacos中添加配置信息

https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

# ${spring.application.name}-${spring.profile.active}.${file-extension}
# nacos-config-client-dev.yaml

在 nacos 配置服务列表

测试
启动3377
浏览器输入:http://localhost:3377/config/info

在 nacos 修改 文件中的 数据 再次刷新 也成功

Nacos作为配置中心——分类配置


Namespace+Group+DataID三者的关系



新建DataId(test)

  1. 修改3377的application.yml的active为test
spring:
  profiles:
    active: dev #表示开发环境  和 bootstrap 结合起来看
#    active: test
  1. 启动3377。
    http://localhost:3377/config/info
Group方案

Group默认DEFAULT_GROUP。

  1. 新建配置nacos-config-client-info.yml(DEV_GROUP)
  2. 新建配置时nacos-config-client-info.yml(TEST_GROUP)

**bootstrap.yml的config下新增group: TEST_GROUP**`

server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址(本机的写localhost)
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址(本机的写localhost)
        file-extension: yaml #读取指定yml格式配置
        group: DEV_GROUP

# ${spring.application.name}-${spring.profile.active}.${file-extension}
# nacos-config-client-dev.yaml
# nacos-config-client-test.yaml
  1. 修改application.yml的active为active: info
  2. 重启3377。
    http://localhost:3377/config/info
  3. 修改bootstrap.yml的group为DEV_GROUP
  4. 重启3377。
Namespace方案

  1. 在命名空间中再创建两个命名空间

  2. 给dev命名空间新增3个DataId,分三个Group。

  3. **在bootstrap.yml的config里添加namespace: 流水号**

server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址(本机的写localhost)
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址(本机的写localhost)
        file-extension: yaml #读取指定yml格式配置
        group: DEV_GROUP
        namespace: 5d9ff56c-56df-4dfa-afd6-ee115419d1bf

重启3377。
http://localhost:3377/config/info

也能看到 nacos 文件下的数据

posted @   flypiggg  阅读(28)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示