1.年初的时候看过nacos的相关知识,一直忘记总结了,现在简单梳理下。GitHub地址(https://github.com/alibaba/nacos/releases/download/1.1.0/nacos-server-1.1.0.zip),版本忘记是哪个了。也可以去云盘下载(链接: https://pan.baidu.com/s/1nhdpNT3N4QENaKFcw2_mpg 提取码: brg7 )
2.下载到本地,直接解压就好了,点击bin目录下的标红脚本即可启动nacos。启动成功效果如下:
3.访问localhost:8848/nacos,输入默认账号密码:nacos,nacos 即可登录。登录成功如下图
4.配置:
可以自定义账号密码,并将账号密码存储进我们自己的数据库,需要修改/nacos/conf/application.properties文件,nacos默认使用的数据库为内嵌的cmdb
创建数据库nacos,并设置字符集utf8,执行/nacos/conf/nacos-mysql.sql中的语句,创建表,并插入数据
5.测试
之前有写过本地的demo,但是具体的想不起来了。。。当初创建的好像是个聚合工程,一些配置跟代码如下。(父项目)
启动类
pom依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.3.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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> <!-- nacos 依赖 --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>nacos-config-spring-boot-starter</artifactId> <version>0.2.1</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${nacos.version}</version> <type>pom</type> <scope>import</scope> </dependency> --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- spring-boot-maven-plugin插件就是打包spring boot应用的 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <modules> <module>Test_child_nacos</module> </modules>
子项目:
启动类:
@SpringBootApplication @EnableDiscoveryClient public class ConsumerApp { public static void main( String[] args ){ SpringApplication.run(ConsumerApp.class, args); } @Slf4j @RestController static class TestController { //Spring Cloud Common接口来挑选服务实例信息。然后从挑选出的实例信息中获取可访问的URI,拼接上服务提供方的接口规则来发起调用。 @Autowired LoadBalancerClient loadBalancerClient; @GetMapping("/test") public String test() { // 通过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用 ServiceInstance serviceInstance = loadBalancerClient.choose("provider"); String url = serviceInstance.getUri() + "/hello?name=" + "laodi"; RestTemplate restTemplate = new RestTemplate(); String result = restTemplate.getForObject(url, String.class); return "Invoke : " + url + ", return : " + result; } } }
test类
package org.Test_child_nacos.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.nacos.api.config.annotation.NacosValue; import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource; /** * @author:smith.shi * @date:2020年11月18日 上午11:35:35 * @ClassName:Test.java */ @SpringBootApplication @RestController //dataId:这个属性是需要在Nacos中配置的Data Id;autoRefreshed:为true的话开启自动更新 //@NacosPropertySource(dataId = "shikai.test", autoRefreshed = true) public class Test { @NacosValue(value = "${test.properties:123}", autoRefreshed = true) private String testProperties; @GetMapping("/test1") public String test(){ return testProperties; } }
反正成功跑起来,在nacos界面可以看到自己注册成功的项目,上面的图应该可以看到,当初弄得时候主要就是配置文件那里,要注意的就是格式的问题。