Nacos基础(2)——nacos的服务器和命名空间 & springBoot整合nacos & 多个nacos配置的情况

引出


1.nacos是服务发现、配置管理和服务管理平台;
2.nacos的命名空间–>组–>服务;
3.springboot整合nacos,版本的对应;
4.引入多个nacos配置的方法;

nacos服务器和命名空间

Nacos服务器

用户

服务管理,命名空间,集群管理

命名空间

命名空间唯一id

自定义命名空间,公共命名空间

如何找到配置文件

新建配置

层级关系

springBoot整合nacos

springcloud Alibaba 版本与springcloud对应关系

引包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>***.tianju.cloud</groupId>
    <artifactId>spring-boot-nacos</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.***piler.source>8</maven.***piler.source>
        <maven.***piler.target>8</maven.***piler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
    </parent>

<!--    依赖的管理-->
    <dependencyManagement>
        <dependencies>
            <!--整合springcloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--整合springcloud alibaba-->
            <dependency>
                <groupId>***.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.4.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>***.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>***.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>

</project>


配置

bootstrap.yml

spring:
  cloud:
    nacos:
      discovery:
        # 能够注册
        register-enabled: true
        server-addr: http://192.168.111.130:8848/
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        # namespace的id
        namespace: 57bca93f-1161-4649-aec3-da79c3aa7***2
        # 配置单个的方式
        group: DEV
        name: book-cofig
        file-extension: yaml


  application:
    name: springcloud-nacos

main

java">package ***.tianju.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

controller

package ***.tianju.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/nacos")
@RefreshScope
public class NacosController {

    @Value("${book.name}")
    private String bookName;

    @GetMapping("/hello")
    public Map hello(){
        Map map = new HashMap();
        map.put("hello", bookName);
        return map;
    }
}

报错以及解决【报错】

错误:缺少服务名称

java.lang.IllegalArgumentException: Param ‘serviceName’ is illegal, serviceName is blank

缺少服务名称

application:  
	name: springcloud-nacos

报错:9848端口未开放

2023-08-19 10:43:46.534 ERROR 14404 — [ main] c.a.n.c.remote.client.grpc.Grp***lient : Server check fail, please check server 192.168.111.130 ,port 9848 is available , error ={}

java.util.concurrent.TimeoutException: Waited 3000 milliseconds (plus 2 milliseconds, 26600 nanoseconds delay) for ***.alibaba.nacos.shaded.io.grpc.stub.ClientCallsKaTeX parse error: Expected '}', got 'EOF' at end of input: ….ProtoLiteUtilsMessageMarshaller@55caeb35, responseMarshaller=***.alibaba.nacos.shaded.io.grpc.protobuf.lite.ProtoLiteUtils M e s s a g e M a r s h a l l e r @ 6 d 868997 , s c h e m a D e s c r i p t o r = c o m . a l i b a b a . n a c o s . a p i . g r p c . a u t o . R e q u e s t G r p c MessageMarshaller@6d868997, schemaDescriptor=***.alibaba.nacos.api.grpc.auto.RequestGrpc MessageMarshaller@6d868997,schemaDescriptor=com.alibaba.nacos.api.grpc.auto.RequestGrpcRequestMethodDescriptorSupplier@2c383e33}}}}}]]

解决办法:开放端口

9848端口没有打开

[root@192 bin]# firewall-cmd --zone=public --add-port=9848/tcp --permanent
su***ess
[root@192 bin]# firewall-cmd --reload
su***ess
[root@192 bin]# firewall-cmd --zone=public --list-ports
3306/tcp 15672/tcp 5672/tcp 2375/tcp 8848/tcp 9848/tcp
[root@192 bin]# 

启动+测试

启动成功

编辑配置

进行配置的修改

修改后发布

后端监听搞修改,会自动刷新

更改前后访问对比

引入多个nacos配置

多个配置的情况

配置本质是java代码

没加.yaml的报错【报错】

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘providerController’: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder ‘book.name’ in value “${book.name}”

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘book.name’ in value “${book.name}”

两个同名配置【细节】

nacos中新建配置(加后缀)

两个配置文件

配置文件(配置多个)

spring:
  cloud:
    nacos:
      discovery:
        register-enabled: true
        server-addr: http://192.168.200.104:8848
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        namespace: 1aec5da8-c5af-46af-8302-f2bd7964f613   # nacos中namespace的id
        extension-configs:
          - data-id: movie_config.yaml
            group: DEV
            refresh: true
          - data-id: book_config.yaml
            group: DEV
            refresh: true
  application:
    name: springcloud-nacos

另一种写法

extension-configs[0]:
  data-id: movie_config.yaml
  group: DEV
  refresh: true
extension-configs[1]:
  data-id: book_config.yaml
  group: DEV
  refresh: true

controller


总结

1.nacos是服务发现、配置管理和服务管理平台;
2.nacos的命名空间–>组–>服务;
3.springboot整合nacos,版本的对应;
4.引入多个nacos配置的方法;

转载请说明出处内容投诉
CSS教程_站长资源网 » Nacos基础(2)——nacos的服务器和命名空间 & springBoot整合nacos & 多个nacos配置的情况

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买