我的命名空间是centos
效果图
可以是yml格式,名称不要变
springboot版本2.1.6
pom依赖
<?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>com.ip</groupId> <artifactId>nacos-ip</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> <skipTests>true</skipTests> <springcloud.version>Greenwich.RELEASE</springcloud.version> <alibaba.version>2.1.0.RELEASE</alibaba.version> </properties> <dependencyManagement> <dependencies> <!--spring cloud依赖管理,引入了Spring Cloud的版本--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${springcloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--SCA --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--SCA --> </dependencies> </dependencyManagement> <dependencies> <!-- Spring Boot Starter Web --> <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>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件application.properties
server.port=8081 #nacos注册 spring.application.name=nacos-service spring.cloud.nacos.discovery.server-addr=192.168.31.50:8850 spring.cloud.nacos.discovery.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e management.endpoints.web.exposure.include=* # 设置Nacos客户端日志级别为DEBUG logging.level.com.alibaba.nacos=DEBUG logging.level.com.ip=debug logging.level.web=debug
配置文件bootstrap.properties
server.port=8081 #nacos配置 #2024.3.1 13:56 add spring.cloud.nacos.config.server-addr=192.168.31.50:8850 spring.cloud.nacos.config.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e spring.cloud.nacos.config.group=DEFAULT_GROUP spring.cloud.nacos.config.file-extension=properties # 设置Nacos客户端日志级别为DEBUG logging.level.com.alibaba.nacos=DEBUG logging.level.com.ip=debug logging.level.web=debug
代码部分
package com.ip; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * @author yourheart * @Description * @create 2021-06-06 10:34 */ @SpringBootApplication @EnableDiscoveryClient public class CloudPingAnApplication { public static void main(String[] args) { SpringApplication.run(CloudPingAnApplication.class,args); } }
package com.ip.controller.front; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @author yourheart * @Description * @create 2021-06-06 10:59 */ @Controller @RequestMapping("/queryIp") @RefreshScope public class QueryIpController { @Value("${test}") private String test; /** * 根据ip查询ip信息 * @param ip * @return */ @RequestMapping("/queryIpCity/{ip}") @ResponseBody public String queryIpCity(@PathVariable String ip){ System.out.println("test="+test); return "success"; } }
注册中心配置关联的版本
springboot版本2.6.13
完整pom依赖
<?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>com.ip</groupId> <artifactId>nacos-ip</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.13</version> </parent> <properties> <java.version>1.8</java.version> <skipTests>true</skipTests> <springcloud.version>2021.0.5</springcloud.version> <alibaba.version>2021.0.5.0</alibaba.version> </properties> <dependencyManagement> <dependencies> <!--spring cloud依赖管理,引入了Spring Cloud的版本--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${springcloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--SCA --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--SCA --> </dependencies> </dependencyManagement> <dependencies> <!-- Spring Boot Starter Web --> <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>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件application.properties
server.port=8081 #nacos注册 spring.application.name=nacos-service spring.cloud.nacos.discovery.server-addr=192.168.31.50:8850 spring.cloud.nacos.discovery.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e management.endpoints.web.exposure.include=* # 设置Nacos客户端日志级别为DEBUG logging.level.com.alibaba.nacos=DEBUG logging.level.com.ip=debug logging.level.web=debug
配置文件bootstrap.properties
server.port=8081 #nacos配置 #2024.3.1 13:56 add spring.cloud.nacos.config.server-addr=192.168.31.50:8850 spring.cloud.nacos.config.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e spring.cloud.nacos.config.group=DEFAULT_GROUP spring.cloud.nacos.config.file-extension=properties # 设置Nacos客户端日志级别为DEBUG logging.level.com.alibaba.nacos=DEBUG logging.level.com.ip=debug logging.level.web=debug
请记得在bootstrap.properties文件中新增配置
spring.cloud.nacos.config.refreshable-dataids=nacos-service.properties,true
后面又找了找,找到了没过时的配置
三者搭配
# 配置支持共享的 Data Id spring.cloud.nacos.config.shared-configs[0].data-id=nacos-service.properties # 配置 Data Id 所在分组,缺省默认 DEFAULT_GROUP spring.cloud.nacos.config.shared-configs[0].group=DEFAULT_GROUP # 配置Data Id 在配置变更时,是否动态刷新,缺省默认 false spring.cloud.nacos.config.shared-configs[0].refresh=true
环境隔离
如果之前使用的是eureka,
使用
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
替换
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>就可以了
注解使用@EnableDiscoveryClient
消费者springboot使用2.4.2版本时,pom依赖加入
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
消费者springboot使用2.6.13版本,和上面引入一样的依赖
不引入报错如下
好了关于nacos注册中心和配置中心使用就暂时到这里了
标签:springboot,中心,spring,nacos,springframework,alibaba,org,cloud From: https://www.cnblogs.com/q202105271618/p/18059115