一、Nacos注册中心
参见上一篇https://blog.csdn.net/qingwufeiyang_530/article/details/158705571?spm=1011.2124.3001.6209
二、Nacos配置管理
2.1 统一配置管理
2.1.1 配置更改热更新

在Nacos中添加配置信息:

在弹出表单中填写配置信息

发布后,可以保存配置文件

配置获取的步骤如下:

1.引入Nacos的配置管理客户端依赖:
XML
<!-- Nacos的配置管理依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
- 在userService的resource目录中添加一个bootstrap.yml文件,这个文件是引导文件,优先级高于application.yml:
XML
spring:
application:
name: userService
profiles:
active: dev
cloud:
nacos:
server-addr: 127.0.0.1:8848
config:
file-extension: yaml
我们在user-service中将pattern.dateformat这个属性注入到UserController中做测试:
XML
@RestController
@RefreshScope
public class UserController {
@Value("${pattern.dateformat}")
private String dateFormat;
@GetMapping("/now")
public String now() {
System.out.println("dateFormat:"+dateFormat);
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateFormat));
}
}
总结
将配置交给Nacos管理的步骤:
- 在Nacos中添加配置文件
- 在微服务中引入nacos的config依赖
- 在微服务中添加bootstrap.yaml,配置nacos地址、当前环境、服务名称、文件名后缀。这些决定了程序启动时去nacos读取哪个文件
2.1.2配置自动刷新
Nacos中的配置文件变更后,微服务无需重启就可以感知。不过需要通过下面两种配置实现:
方式一:在@Value注入的变量所在类上添加注解@RefreshScope

Nacos中配置文件变更后,微服务无需重启就可以感知。不过需要通过下面两种配置实现:
方式二:使用@ConfigurationProperties注解
java
@ConfigurationProperties(prefix = "pattern")
@Component
@Data
public class PatternProperties {
private String dateformat;
}
总结
Nacos配置更改后,微服务可以实现热更新,方式:
- 通过@Value注解注入,结合@RefreshScope来刷新
- 通过@ConfigurationProperties注入,自动刷新
注意事项:
- 不是所有的配置都适合放到配置中心,维护起来比较麻烦
- 建议将一些关键参数,需要运行时调整的参数放到nacos配置中心,一般都是自定义配置
2.1.3 多环境配置共享

多种配置的优先级

总结

三、Nacos集群搭建

总结
三、Feign

3.1基于Feign远程调用
3.1.1RestTemplate方式调用存在的问题

3.1.2 Feign的介绍

3.1.3 定义和使用Feign客户端
使用Feign的步骤如下:
1.引入依赖
java
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.在order-service的启动类添加注解 @EnableFeignClients 开启Feign的功能:

3.编写Feign客户端

主要是基于springMVC的注解来声明远程调用的额信息,比如:
- 服务名称:userService
- 请求方式:GET
- 请求路径:/hello
- 请求参数:Long id
- 返回值类型:String
总结

3.2 自定义Feign的配置




3.3 Feign的性能优化



3.4 Feign的最佳实践



3.4.1 抽取FeignClient


