目录
一、下载nacos并运行
1.1、创建9001微服务作为提供者
1.2、创建80微服务作为消费者
二、naocs配置中心规则与动态刷新
2.1、创建3377微服务
2.2、在nacos中创建配置文件
三、nacos数据模型之Namespace-Group-Datald
3.1、DatalD方案
3.2、Group方案
3.3、Namespace方案
一、下载nacos并运行
下载官网:Nacos官网 | Nacos 官方社区 | Nacos 下载 | Nacos
运行并测试访问:startup.cmd -m standalone
1.1、创建9001微服务作为提供者
导入相关pom依赖并创建配置yml文件
<dependencies><!--nacos-discovery--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--loadbalancer--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><!--web + actuator--><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><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
server:port: 9001spring:application:name: nacos-payment-providercloud:nacos:discovery:server-addr: localhost:8848 #配置Nacos地址
测试代码:
@SpringBootApplication
@EnableDiscoveryClient
public class Main9001 {public static void main(String[] args) {SpringApplication.run(Main9001.class,args);}
}
@RestController
public class PayAlibabaController {@Value("${server.port}")private String serverPort;@GetMapping(value = "/pay/nacos/{id}")public String getPayInfo(@PathVariable("id") Integer id){return "nacos registry, serverPort: "+ serverPort+"\t id"+id;}
}
1.2、创建80微服务作为消费者
导入相关pom依赖并创建配置yml文件
<dependencies><!--nacos-discovery--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--loadbalancer--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><!--web + actuator--><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><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
server:port: 80spring:application:name: nacos-order-consumercloud:nacos:discovery:server-addr: localhost:8848
#消费者将要去访问的微服务名称(nacos微服务提供者叫什么你写什么)
service-url:nacos-user-service: http://nacos-payment-provider
测试代码:
@SpringBootApplication
@EnableDiscoveryClient
public class Main80 {public static void main(String[] args) {SpringApplication.run(Main80.class,args);}
}
@Configuration
public class RestTemplateConfig {@Bean@LoadBalanced //赋予RestTemplate负载均衡的能力public RestTemplate restTemplate(){return new RestTemplate();}
}
@RestController
public class OrderNacosController {@Resourceprivate RestTemplate restTemplate;@Value("${service-url.nacos-user-service}")private String serverURL;@GetMapping("/consumer/pay/nacos/{id}")public String paymentInfo(@PathVariable("id") Integer id){String result = restTemplate.getForObject(serverURL + "/pay/nacos/" + id, String.class);return result+"\t"+" 我是OrderNacosController80调用者。。。。。。";}
}
二、naocs配置中心规则与动态刷新
2.1、创建3377微服务
<dependencies><!--bootstrap--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><!--nacos-config--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!--nacos-discovery--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--web + actuator--><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><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
创建配置文件bootstrap.yml
# nacos配置
spring:application:name: nacos-config-clientcloud:nacos:discovery:server-addr: localhost:8848 #Nacos服务注册中心地址config:server-addr: localhost:8848 #Nacos作为配置中心地址file-extension: yaml #指定yaml格式的配置# nacos端配置文件DataId的命名规则是:
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# 本案例的DataID是:nacos-config-client-dev.yaml
创建配置文件application.yml
server:port: 3377spring:profiles:active: dev # 表示开发环境#active: prod # 表示生产环境#active: test # 表示测试环境
测试代码:
@SpringBootApplication
@EnableDiscoveryClient
public class Main3377 {public static void main(String[] args) {SpringApplication.run(Main3377.class,args);}
}
@RestController
@RefreshScope //动态刷新
public class NacosConfigClientController {@Value("${config.info}")private String configInfo;@GetMapping("/config/info")public String getConfigInfo() {return configInfo;}
}
2.2、在nacos中创建配置文件
测试读取测试文件内容
三、nacos数据模型之Namespace-Group-Datald
3.1、DatalD方案
只需要创建不同 Data id配置即可
修改application.yml配置文件
3.2、Group方案
修改bootstrap.yml文件
3.3、Namespace方案
创建自己的命名空间
选择自己创建的命名空间进行创建配置