nacos下载地址 Release 1.4.0 (Nov 2, 2020) · alibaba/nacos · GitHub
nacos默认是AP 高可用模型
可以通过下面命令切换为CP 高一致性模型
curl -X PUT '$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=CP'
nacos启动,win版nacos解压出的文件夹下直接点击 startup.cmd
启动完毕后访问http://localhost:8848/nacos
可以看到如下界面
然后构造9001 9002 服务提供方 , 82 服务消费方
9001 9002 依赖如下
cloud2021 com.liuxu 1.0-SNAPSHOT 4.0.0 cloud-nacos-provider9001com.liuxu cloud-commons1.0-SNAPSHOT org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatorcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
cloud2021 com.liuxu 1.0-SNAPSHOT 4.0.0 cloud-nacos-provider9002com.liuxu cloud-commons1.0-SNAPSHOT org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatorcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
主启动类上添加注解
@EnableDiscoveryClient
package liuxu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderMain {
public static void main(String[] args) {
SpringApplication.run(NacosProviderMain.class, args);
}
}
yml添加nacos注册中心配置,自行根据需要修改port 端口号
server:
port: 9002
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
#Actuator 服务监控有关内容
management:
endpoints:
web:
exposure:
include: '*'
启动 9001 9002 可以看到 两个同名实例已经注册
再新建一个消费方83
依赖
cloud2021 com.liuxu 1.0-SNAPSHOT 4.0.0 cloud-nacos-consumer83com.liuxu cloud-commons1.0-SNAPSHOT org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-openfeignorg.springframework.boot spring-boot-starter-actuatorcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
主启动
package com.liuxu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerMain {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerMain.class, args);
}
}
server:
port: 83
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
management:
endpoints:
web:
exposure:
include: '*'
配置类,给RestTemplate 配置负载均衡
package com.liuxu.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContext {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
controller 层可以通过 restTemlate 调用服务,也可以通过OpenFeign调用服务
Feign接口层
package com.liuxu.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("nacos-payment-provider")
public interface NacosProviders {
@GetMapping(value = "/echo/{String}")
public String echo(@PathVariable("String") String string);
}
controller启动83 注册中心 现在又一个消费方 83 俩个服务提供方 9001 9002 注册完成
http://localhost:83/consumer/feign/1
http://localhost:83/consumer/nacos/1
均可以调用服务提供方 OpenFeign和restTemlate同样适用
nacos同样是注册中心
下面做注册中心的演示
在 83 pom中加入注册中心的依赖
cloud2021 com.liuxu 1.0-SNAPSHOT 4.0.0 cloud-nacos-consumer83com.liuxu cloud-commons1.0-SNAPSHOT org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-openfeignorg.springframework.boot spring-boot-starter-actuatorcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoverycom.alibaba.cloud spring-cloud-starter-alibaba-nacos-config
yaml文件中 加入配置中心地址
server:
port: 83
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848 #Nacos作为配置中心地址
file-extension: yaml #指定yaml格式配置
management:
endpoints:
web:
exposure:
include: '*'
新建 bootstrap.yml 来指定 考入上述配置
原application.yml用来指定运行环境,内容为
spring:
profiles:
#active: info
active: dev #表示开发环境
#active: test
controller中添加读取配置文件的接口 并开启动态刷新
package com.liuxu.controller;
import com.liuxu.feign.NacosProviders;
import org.springframework.beans.factory.annotation.Autowired;
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.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RefreshScope //支持动态刷新
public class NacosController {
@Autowired
RestTemplate restTemplate;
private String serverURL = "http://nacos-payment-provider";
@Autowired
NacosProviders nacosProviders;
@GetMapping("consumer/nacos/{id}")
public String payInfo(@PathVariable String id){
return restTemplate.getForObject(serverURL+"/echo/"+id,String.class);
}
@GetMapping("consumer/feign/{id}")
public String getFeign(@PathVariable String id){
return nacosProviders.echo(id);
}
@Value("${config.info}")
private String configInfo;
@Value("${config.data}")
private String data;
@GetMapping("/config/info")
public String getConfigInfo(){
return "configInfo:"+configInfo+"data:"+data;
}
}
nacos中添加配置
group 选择 DEFAULT_GROUP 命名空间为public 这两个都是默认读取的位置
新建文件格式为yaml,文件名为nacos-consumer-dev.yaml 配置中心会根据这个匹配相应的配置文件,
配置完成 发布,访问http://localhost:83/config/info 可以读取到配置文件中地址
配置中心还会有Data Id Group 和 命名空间的区分
首先可以命名空间和Group配置的情况下 读取的是public 命名空间下 DEFAULT_GROUP
组下配置文件
如果新增命名空间 会有命名空间的id
新增Group会有Group名字
只需要在配置文件中指定相应的命名空间 id 和Group组即可
server:
port: 83
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848 #Nacos作为配置中心地址
file-extension: yaml #指定yaml格式配置
namespace: 0f45b872-07aa-4608-a01a-d9f9bee7940e # 命名空间id
group: DEV_GROUP # 分组group
#配置文件命名UI自
#${spring.application.yaml.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
management:
endpoints:
web:
exposure:
include: '*'
对应配置中心
寻找配置文件的过程为 对应命名空间id(或者默认的public空间)
对应group , 对应环境下的文件名
nacos作为注册中心和配置中心的



