1.nacos是服务发现、配置管理和服务管理平台;
2.nacos的命名空间–>组–>服务;
3.springboot整合nacos,版本的对应;
4.引入多个nacos配置的方法;
用户
服务管理,命名空间,集群管理
命名空间唯一id
自定义命名空间,公共命名空间
如何找到配置文件
新建配置
层级关系
<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.0modelVersion>
<groupId>com.tianju.cloudgroupId>
<artifactId>spring-boot-nacosartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.13version>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>2021.0.5version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2021.0.4.0version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bootstrapartifactId>
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-da79c3aa7cc2
# 配置单个的方式
group: DEV
name: book-cofig
file-extension: yaml
application:
name: springcloud-nacos
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);
}
}
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
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]#
启动成功
编辑配置
进行配置的修改
修改后发布
后端监听搞修改,会自动刷新
更改前后访问对比
配置本质是java代码
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}”
两个配置文件
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
1.nacos是服务发现、配置管理和服务管理平台;
2.nacos的命名空间–>组–>服务;
3.springboot整合nacos,版本的对应;
4.引入多个nacos配置的方法;