Nacos基础(2)——nacos的服务器和命名空间 & springBoot整合nacos & 多个nacos配置的情况
目录
引出nacos服务器和命名空间Nacos服务器命名空间
springBoot整合nacosspringcloud Alibaba 版本与springcloud对应关系引包配置maincontroller
报错以及解决【报错】错误:缺少服务名称报错:9848端口未开放
启动+测试引入多个nacos配置多个配置的情况没加.yaml的报错【报错】两个同名配置【细节】
nacos中新建配置(加后缀)配置文件(配置多个)controller
总结
引出
1.nacos是服务发现、配置管理和服务管理平台; 2.nacos的命名空间–>组–>服务; 3.springboot整合nacos,版本的对应; 4.引入多个nacos配置的方法;
nacos服务器和命名空间
Nacos服务器
用户
服务管理,命名空间,集群管理
命名空间
命名空间唯一id
自定义命名空间,公共命名空间
如何找到配置文件
新建配置
层级关系
springBoot整合nacos
springcloud Alibaba 版本与springcloud对应关系
引包
4.0.0
com.tianju.cloud
spring-boot-nacos
1.0-SNAPSHOT
8
8
UTF-8
org.springframework.boot
spring-boot-starter-parent
2.6.13
org.springframework.cloud
spring-cloud-dependencies
2021.0.5
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2021.0.4.0
pom
import
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
org.springframework.cloud
spring-cloud-starter-bootstrap
配置
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-da79c3aa7cc2
# 配置单个的方式
group: DEV
name: book-cofig
file-extension: yaml
application:
name: springcloud-nacos
main
package com.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 com.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.GrpcClient : 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 com.alibaba.nacos.shaded.io.grpc.stub.ClientCallsKaTeX parse error: Expected '}', got 'EOF' at end of input: ….ProtoLiteUtilsMessageMarshaller@55caeb35, responseMarshaller=com.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=com.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
success
[root@192 bin]# firewall-cmd --reload
success
[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配置的方法;