Nacos

用于替代已经停止维护的 Eureka 。

服务端

AlibabaNacos的里程碑中选择版本进行下载。

下载解压后,在 bin 目录下打开终端,输入指令启动。Windows 运行cmd文件,Linux运行sh文件

1
./startup.cmd -m standalone

默认服务端口为8848,可在 conf 目录下配置文件 application.properties 中更改。

客户端

在父工程的pom文件中添加依赖管理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    <properties>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<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>
<!-- 包含nacos的项目依赖管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

在子工程(各微服务对应的spring boot项目)pom文件中添加 nacos 依赖。

1
2
3
4
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

在子工程配置文件yml中配置服务端地址

1
2
3
4
5
6
7
8
spring:
application:
name: UserService
cloud:
nacos:
server-addr: localhost:8848
server:
port: 39940

服务多级存储模型

微服务分集群部署。分集群部署,服务不容易同时挂掉。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://www.kahvia.cn:3306/Blogs?serverTimezone=GMT%2B8
username: root
password: xxxxxx
application:
name: UserService
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: YC #集群名称,常按地名划分,YC为宜昌
server:
port: 39940

负载均衡

局部策略(在某个微服务中调用指定微服务的负载均衡策略)

1
2
3
UserService: #为某个微服务配置负载均衡的规则
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule #该策略优先远程调用同集群的微服务,同集群挂掉会访问其它集群

权重

在nacos管理页面中,可以为同一微服务同一集群的不同实例设置权重。权重越大,负载均衡选中的几率越大。

可以通过将权重置零来进行服务的升级,升级完成后设置较小权重进行服务测试。

环境隔离

不同环境(不同命名空间)的服务无法互相调用。可用于开发环境与上线后的环境隔离,大概?

在nacos管理页面中,可新建命名空间,获取它的id后,在需要配置环境隔离的服务项目的 application.yml 中,如下配置。

1
2
3
4
5
6
7
8
9
spring:
application:
name: AchatWs
cloud:
nacos:
server-addr: localhost:8848 #nacos服务端地址
discovery:
cluster-name: YC #集群模拟,宜昌
namespace: id #命名空间的id

Nacos配置管理

客户端引入nacos配置管理的依赖。

1
2
3
4
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

需要进行配置管理的项目新建 bootstrap.yml 文件,在其中写入与nacos有关的属性,如开发环境、服务端地址、微服务名称等。这些信息在 application.yml 中就可以不写了。

1
2
3
4
5
6
7
8
9
10
11
12
13
#bootstrap.yml
spring:
application:
name: UserService
profiles:
active: dev #开发环境
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: YC
config:
file-extension: yaml #要获取的配置文件后缀名

项目启动的时候,会优先读取 bootstrap.yml 中的配置信息,再根据其中的信息去nacos服务端获取nacos管理的配置,将这个获取到的配置与之后读取的 application.yml 作合并,最后才完整地启动项目。

nacos服务端新建配置,ID由“服务名-环境.后缀”组成最好。

自定义配置

Nacos配置管理的热更新

方式一

哪里使用就为那里的类添加热更新注释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RefreshScope //加这个注解才能实现配置的热更新
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserDao userDao;
@Value("${pattern.dateformat}")
private String format;

@GetMapping("/time")
String getTime(){
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
}
}

官方对@RefreshScope的解释是

Convenience annotation to put a @Bean definition in refresh scope. Beans annotated this way can be refreshed at runtime and any components that are using them will get a new instance on the next method call, fully initialized and injected with all dependencies.

方式二(推荐)

自定义配置类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.kahvia.userservice.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "pattern")//prefix是前缀的意思,前缀和属性名拼接之后的属性若是在配置文件中,就可以实现自动注入
@Component
public class PropertiesFromNacos {
private String dateformat;//属性名。

public String getDateformat() {
return dateformat;
}

public void setDateformat(String dateformat) {
this.dateformat = dateformat;
}
}

@ConfigurationProperties(prefix = “xxx”)源码注释

The prefix of the properties that are valid to bind to this object. Synonym for prefix(). A valid prefix is defined by one or more words separated with dots (e.g. “acme.system.feature”).
Returns: the prefix of the properties to bind

使用该注释,需要引入依赖

You can easily generate your own configuration metadata file from items annotated with @ConfigurationProperties by using the spring-boot-configuration-processor jar. The jar includes a Java annotation processor which is invoked as your project is compiled.

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

多环境共享配置

在nacos管理页面创建配置文件的名字,只需要用服务的名字就可以了。比如,UserService,而不是UserService-dev.

项目启动时,会同时从nacos读取上述两种配置,后者的优先级大于前者,前者大于本地。同名属性则覆盖。