https://nacos.io/zh-cn/
第一步:利用Gitee获取nacos在github上的代码到自己的gitee仓库中
https://github.com/alibaba/nacos.git
第二步:下载源码到本地。
第三步:使用maven编译代码。
# 先切换到master分支
git checkout master
# 使用如下命令编译源码
mvn -Prelease-nacos -Dmaven.test.skip=true -Drat.skip=true clean install -U
第四步:编译好的内容在distribution目录下
第五步:把zip文件放到其他目录中,并解压。
第六步:修改配置conf目录下的配置文件,添加secretkey。
nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
第七步:启动nacos F:\env\nacos\bin目录下
startup.cmd -m standalone
https://nacos.io/zh-cn/docs/v2/quickstart/quick-start.html
#服务注册
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
#服务发现
curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'
#发布配置
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"
#获取配置
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
https://nacos.io/zh-cn/
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
注意版本号的选取。
<packaging>pompackaging>
<properties>
<spring.cloud-version>Hoxton.SR10spring.cloud-version>
<springboot-version>2.2.9.RELEASEspringboot-version>
<spring.cloudalibaba-version>2.2.9.RELEASEspring.cloudalibaba-version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring.cloud-version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${springboot-version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring.cloudalibaba-version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
微服务模块中引入nacos依赖
>
>com.alibaba.cloud >
>spring-cloud-starter-alibaba-nacos-discovery >
>
yml配置
server:
port: 8070
spring:
application:
name: service-provider #服务名称
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos注册中心的地址
启动类NacosProviderApplication
@SpringBootApplication
@EnableDiscoveryClient // 注册到nacos中
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
}
@RestController
public class EchoController {
@Value("${server.port}")
private Integer port;
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery: "+port+"--->" + string;
}
}
pom文件与producer一致
server:
port: 8090
spring:
application:
name: service-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
启动类NacosConsumerApplication
@SpringBootApplication
@EnableDiscoveryClient // 注册到nacos中 不管是消费者还是提供者,都是这个注册到nacos中
public class NacosConsumerApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
}
@RestController
public class TestController {
private final RestTemplate restTemplate;
@Autowired
public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
public String echo(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
}
}
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
<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>
dependencies>
文件名为:bootstrap.yml
spring:
application:
name: config-demo
cloud:
nacos:
server-addr: 127.0.0.1:8848
config:
group: mygroup
file-extension: yaml
profiles:
active: dev
注意需要在bootstrap配置文件中声明nacos中使用的配置文件的扩展名
spring.cloud.nacos.config.file-extension=yaml
spring:
application:
name: config-demo #跟配置的前缀名字
cloud:
nacos:
server-addr: 127.0.0.1:8848
config:
file-extension: yaml
profiles:
active: test #跟配置的后缀 设备名匹配
如果需要指定配置文件的分组,如下
spring.cloud.nacos.config.group=dev
.cloud.nacos.config.file-extension=yaml
spring:
application:
name: config-demo #跟配置的前缀名字
cloud:
nacos:
server-addr: 127.0.0.1:8848
config:
file-extension: yaml
profiles:
active: test #跟配置的后缀 设备名匹配
如果需要指定配置文件的分组,如下
spring.cloud.nacos.config.group=dev