个人的动力节点视频学习笔记 视频地址:https://www.bilibili.com/video/BV1f94y1U7AB
Nacos 致力于发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速 实现动态服务发现、服务配置、服务元数据及流量管理。 Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心 的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。
NacosServer 相当于 EurekaServer,只不过 eurekaServer 使我们自己搭建的一个项目, 而 NacosServer 别人已经提供好了
https://github.com/alibaba/nacos/releases/tag/2.0.3
bin:可执行文件夹目录,包含:启动、停止命令等等
conf:配置文件目录
target:存放 naocs-server.jar
LICENSE:授权信息,Nacos 使用 Apache License Version 2.0 授权
NOTICE:公告信息
Nacos 默认使用嵌入式数据库实现数据的存储,并不方便观察数据存储基本情况,这里面 我们修改为使用 Mysql 数据库做数据的存储,方便我们观察数据的结构。
在配置文件末尾添加如下配置:
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos?characterEncoding=utf8&connectTimeout=1 000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=bcy123
在 config 目录下找到对应的 sql 脚本,提示:Nacos 建议使用 5.7 Mysql 的数据库,版本 较低或者较高可能存储兼容性问题
可以直接 startup.cmd -m standalone 启动单击版本
上面工作都完成后,现在我们来启动一个单机版的 Nacos 服务器。
进入到${Nacos}/bin 目录里面:
使用 nodepad++打开 startup.cmd 修改默认参数
将 set MODE=”cluster”修改为 standalone
双击 startup.cmd 文件,完成 nacosServer 的启动。
Nacos 默认用户名和密码都是 nacos。 如果想修改密码,可以直接修改数据库的 user 表,密码可以使用BcryptPasswordEncoder
加密
注意这个id
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<spring-boot.version>2.3.12.RELEASEspring-boot.version>
<spring-cloud.version>Hoxton.SR12spring-cloud.version>
<spring-cloud-alibaba.version>2.2.7.RELEASEspring-cloud-alibaba.version>
properties>
<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>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring-cloud-alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
server:
port: 8081
spring:
application:
name: nacos-client-b
cloud:
nacos:
server-addr: localhost:8848
username: nacos
password: nacos
discovery: # 这里是和注册相关的配置
namespace: 400329f6-d7a5-488d-81ad-4e13114f1532
group: A_GROUP # 往哪个组注册
service: user-service # 这个才是注册列表的名字 如果不写 默认是取${spring.application.name}
@SpringBootApplication
@EnableDiscoveryClient
public class NacosClientBApplication {
public static void main(String[] args) {
SpringApplication.run(NacosClientBApplication.class, args);
}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BController {
@GetMapping("info")
public String info() {
return "年薪20w";
}
}
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<spring-boot.version>2.3.12.RELEASEspring-boot.version>
<spring-cloud.version>Hoxton.SR12spring-cloud.version>
<spring-cloud-alibaba.version>2.2.7.RELEASEspring-cloud-alibaba.version>
properties>
<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>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring-cloud-alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
server:
port: 8080
spring:
application:
name: nacos-client-a
cloud:
nacos: # 如果不指定命名空间会默认注册到public里面去 如果没有指定分组 会注册到DEFAULT_GROUP
server-addr: localhost:8848 # 往这个地址去注册自己
username: nacos
password: nacos
discovery:
namespace: 400329f6-d7a5-488d-81ad-4e13114f1532
group: A_GROUP
my:
data: '#{@abc}'
package edu.bcy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableDiscoveryClient // 开启服务发现客户端
@EnableFeignClients
public class NacosClientAApplication {
public static void main(String[] args) {
SpringApplication.run(NacosClientAApplication.class, args);
}
@Bean
public String abc(){
return "abc";
}
}
package edu.bcy.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "user-service")
public interface TestFeign {
@GetMapping("info")
public String info();
}
package edu.bcy.controller;
import edu.bcy.feign.TestFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestController {
@Autowired
public DiscoveryClient discoveryClient;
// # 读ioc容器的值
// $ 读配置文件的值
// 如果直接写字符串 那么就是一个简单赋值操作
@Value("aaaa")
public String data;
@Autowired
public TestFeign testFeign;
@GetMapping("test")
public String test() {
//这是
List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
System.out.println(instances);
return testFeign.info();
}
}
写data id的时候推荐把后缀写上:比如config-client-a.yml
<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>07-nacosartifactId>
<groupId>edu.bcygroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>04-nacos-config-aartifactId>
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<spring-boot.version>2.3.12.RELEASEspring-boot.version>
<spring-cloud-alibaba.version>2.2.7.RELEASEspring-cloud-alibaba.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring-cloud-alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>2.3.7.RELEASEversion>
<configuration>
<mainClass>com.powernode.NacosConfigAApplicationmainClass>
configuration>
<executions>
<execution>
<id>repackageid>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
server:
port: 8081
spring:
application:
name: nacos-config-a
# 项目在启动的时候去哪里找它对应的配置文件呢??
cloud:
nacos:
config:
server-addr: localhost:8848
username: nacos
password: nacos
prefix: config-client-a # 读哪个配置文件 默认用的是应用名称 是可以修改的
file-extension: yml # 文件类型
profiles:
active: dev
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@RefreshScope // 给这个类上 添加一个刷新的作用域
public class Hero {
@Value("${hero.name}")
private String name;
@Value("${hero.age}")
private Integer age;
@Value("${hero.address}")
private String address;
}
修改yml配置,加载多个nacos配置源
extension-configs: # 可以读多个配置文件 需要在同一个命名空间下面 可以是不同的组
- dataId: user-center-dev.yml
group: A_GROUP
refresh: true
- dataId: member-center-dev.yml
group: B_GROUP
refresh: false # 不去动态刷新配置文件
shared-configs: # 共享配置文件
# - application-dev.yml # 这里可以写共享的文件名称 只能在DEFAULT_GROUP
- dataId: application-dev.yml # 这两种写法都可以
group: C_GROUP
refresh: true