SpringCloud之Nacos(一)

SpringCloud之Nacos

安装Nacos

1.拉去Nacos镜像

docker pull nacos/nacos-server:latest

2.查看镜像

docker images

3.创建nacos的docker容器

docker run --name nacos-server -e PREFER_HOST_MODE=hostname -d -p 8848:8848 nacos/nacos-server:latest

4.查看docker容器

docker ps -a

SpringCloud集成Nacos

注意事项

nacos的命名规则:在 Nacos Spring Cloud 中,dataId 的完整格式如下:

p r e f i x − {prefix}- prefix{spring.profiles.active}.${file-extension}

${prefix}:表示服务名称

${spring.profiles.active}:表示激活的配置文件,如果未配置则去掉该配置以及前面的-。

${file-extension}:表示选用的文件类型,一般情况下是使用yaml或者properties

1.导入Maven依赖

 		<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

2.编写bootstrap.yml配置文件

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 124.70.105.237:8848
      config:
        server-addr: 124.70.105.237:8848
        refresh-enabled: true
        file-extension: yaml
        namespace: 3a22c228-8b7d-4e37-b68b-2014a5b3349f
      server-addr: 124.70.105.237:8848
  application:
    name: test
server:
  port: 8081

注意事项

  1. bootstrap的配置如果是使用的2.4之前默认是开启使用的,2.4.2之后版本的默认值为false需要添加以下依赖spring.cloud.bootstrap.enabled=true 我们这儿使用的springBoot的版本是2.3.4.RELEASE

  2. 如果是把创建好的nacos作为注册中心的话就需要注意文件的命名格式。比如本文所写的配置会发现的配置为test.yaml,注意结尾的必须与file-extension这个的类型一致,不然就会出现找不到配置文件的问题

3.作为注册中心

需要再启动类上加入@EnableDiscoveryClient注解才能发现

@SpringBootApplication
@EnableDiscoveryClient
public class NacosApplication {
    /**
     * 程序的入口点。
     * 使用SpringApplication.run方法启动Spring Boot应用程序。
     * 此方法接收两个参数:应用程序的主类NacosApplication和命令行参数args。
     * 它负责加载和运行Spring Boot应用程序。
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        SpringApplication.run(NacosApplication.class, args);
    }
}

启动服务

在这里插入图片描述

注册中心的主要作用将服务注册到nacos中,可供其他服务发现。也可以维护服务实例的健康状态,服务监控与统计。

4.作为配置中心

在Nacos页面中创建命名空间

在这里插入图片描述

在Nacos中添加配置信息

在这里插入图片描述

在这里插入图片描述

编写controller
@RestController
@RequestMapping(value ="/config")
@RefreshScope
public class DiscoveryController {
    @Value("${test.count}")
    private Integer count;

    @GetMapping
    public Integer getCount() {
        return count;
    }

}

@RefreshScope 动态加载配置信息,如果配置信息在yaml中写入,则不会动态加载配置信息,只是将配置类的信息重新加载一遍。
使用此注解可以nacos的配置信息的动态变化

  • 30
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud 是一个开源的框架,用于在分布式系统中构建微服务应用程序。Nacos 是一个开源的注册中心和配置中心,可以作为微服务架构中的基础设施,提供服务发现、服务注册、配置管理、动态路由等功能。Spring CloudNacos 的整合可以帮助开发者更方便地构建微服务应用程序。 以下是 Spring Cloud 整合 Nacos 的基本步骤: 1. 添加依赖 在 pom.xml 文件中添加 Spring CloudNacos 的依赖。例如,可以添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.5.RELEASE</version> </dependency> ``` 2. 配置 Nacos 服务器地址 在 application.properties 或 application.yml 文件中配置 Nacos 服务器地址。例如: ``` spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 ``` 3. 配置微服务的注册信息 在 application.properties 或 application.yml 文件中配置微服务的注册信息。例如: ``` spring.application.name=my-service spring.cloud.nacos.discovery.group=DEFAULT_GROUP spring.cloud.nacos.discovery.port=8080 spring.cloud.nacos.discovery.instance-id=${spring.application.name}-${spring.cloud.client.ip-address}:${spring.cloud.client.server-port} ``` 4. 启动微服务 在微服务中添加 @EnableDiscoveryClient 注解,启动微服务。例如: ``` @SpringBootApplication @EnableDiscoveryClient public class MyServiceApplication { public static void main(String[] args) { SpringApplication.run(MyServiceApplication.class, args); } } ``` 5. 验证 启动微服务后,可以通过 Nacos 控制台查看微服务的注册信息。例如,可以在 Nacos 控制台中查看 my-service 的实例信息。 以上是 Spring Cloud 整合 Nacos 的基本步骤。除了服务注册和发现外,Nacos 还支持配置管理、动态路由等功能,可以进一步提高微服务架构的可靠性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值