<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>org.nacos-cloudgroupId>
<artifactId>nacos-cloud-testartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.7version>
<relativePath/>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-contextartifactId>
<version>3.1.0version>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>2.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bootstrapartifactId>
<version>3.0.4version>
dependency>
dependencies>
project>
<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">
<parent>
<artifactId>nacos-cloud-testartifactId>
<groupId>org.nacos-cloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>org.nacos-client01groupId>
<artifactId>nacos-client01artifactId>
project>
//启动类
package com.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author Liu
* @version 1.0
* @date 2022/6/21 14:01
*/
@EnableDiscoveryClient
@SpringBootApplication
public class ClientApplication01 {
public static void main(String[] args) {
SpringApplication.run(ClientApplication01.class);
}
}
//TestController
package com.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
package com.nacos.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Liu
* @version 1.0
* @date 2022/6/21 14:02
*/
@RestController
public class TestController {
@Value("${server.port}")
private String port;
@RequestMapping("/test")
public String test(){
return "当前访问项目端口:"+port;
}
}
在这不得不说一下这个dataId的命名规则了
正常来说项目中配置寻找配置文件时需要通过dataId 而他的组成默认是通过 ${prefix}-${spring.profiles.active}.${file-extension} 组成的 prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置,spring.profiles.active 即为当前环境对应的 profile,详情可以参考 Spring Boot文档。
注意:当 spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}
file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型
//bootstrap.properties内容
spring.profiles.active=dev
//bootstrap-dev.properties内容
nacos.server.addr=127.0.0.1:8848
nacos.server.namespace=b8bae8ff-7885-44f3-9a22-fcf3f4f28e13
nacos.server.group=DEFAULT_GROUP
spring.cloud.nacos.config.prefix=nacos.client01
spring.application.name=nacosclient
spring.cloud.nacos.config.namespace=${nacos.server.namespace}
spring.cloud.nacos.config.server-addr=${nacos.server.addr}
spring.cloud.nacos.config.encode=UTF-8
spring.cloud.nacos.config.group=${nacos.server.group}
spring.cloud.nacos.discovery.server-addr=${nacos.server.addr}
spring.cloud.nacos.discovery.namespace=b8bae8ff-7885-44f3-9a22-fcf3f4f28e13
spring.cloud.nacos.config.timeout=3600
nacos.server.addr-配置为nacos地址
nacos.server.namespace-为nacos中命名空间
nacos.server.group-为nacos中的组名
spring.cloud.nacos.config.prefix是我们在nacos中创建配置文件的dataId为nacos.client01-dev.properties所以在此为nacos.client01
spring.application.name-区分服务
第二个服务和第一个服务基本一致,需要修改第二个服务的配置文件spring.cloud.nacos.config.prefix为nacos.client02,启动之后在nacos中查看
到此说明已经成功一大半了!!!
//OrderController
package com.nacos.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* @author Liu
* @version 1.0
* @date 2022/6/21 14:02
*/
@RestController
@RequestMapping("order")
public class OrderController {
@Resource
private RestTemplate restTemplate;
@GetMapping("/get")
public String get(){
// 使用服务名即可发起调用
return restTemplate.getForObject("http://nacosclient/test", String.class);
}
}
//启动类 OrderApplication
package com.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author Liu
* @version 1.0
* @date 2022/6/21 14:50
*/
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
/**
* 服务内部调用配置类
* @return
*/
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
//bootstrap.properties
spring.profiles.active=dev
//bootstrap-dev.properties
nacos.server.addr=127.0.0.1:8848
nacos.server.namespace=b8bae8ff-7885-44f3-9a22-fcf3f4f28e13
nacos.server.group=DEFAULT_GROUP
spring.cloud.nacos.config.prefix=nacos.order
spring.application.name=nacosorder
spring.cloud.nacos.config.namespace=${nacos.server.namespace}
spring.cloud.nacos.config.server-addr=${nacos.server.addr}
spring.cloud.nacos.config.encode=UTF-8
spring.cloud.nacos.config.group=${nacos.server.group}
spring.cloud.nacos.discovery.server-addr=${nacos.server.addr}
spring.cloud.nacos.discovery.namespace=b8bae8ff-7885-44f3-9a22-fcf3f4f28e13
spring.cloud.nacos.config.timeout=3600
与上面配置文件的区别是修改了spring.cloud.nacos.config.prefix和spring.application.name 不要忘记修改
浏览器访问:http://localhost:9000/order/get即可
多刷新几次可以发现8001端口的服务和8002端口的服务是轮巡的 这是因为nacos中内置了Ribbon负载均衡组件。
代码在此:https://github.com/979780632/nacos-cloud-test.git
GitHub进不去的进Gitee:https://gitee.com/lost_nd_eyes/nacos-cloud-test.git
该文章为spring-cloud使用nacos注册中心测试案例
如果未安装nacos可参考文章nacos安装配置