2025-05-26 17:29:17 +08:00
|
|
|
|
# Nacos注册中心
|
2025-05-20 21:29:12 +08:00
|
|
|
|
|
2025-05-20 21:38:52 +08:00
|
|
|
|
## 注册中心
|
|
|
|
|
|
|
|
|
|
### 服务发现
|
|
|
|
|
|
|
|
|
|
发现服务信息。
|
|
|
|
|
|
|
|
|
|
```java
|
2025-05-26 13:32:07 +08:00
|
|
|
|
@SpringBootTest()
|
|
|
|
|
public class DiscoveryTest {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private DiscoveryClient discoveryClient;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private NacosDiscoveryClient nacosDiscoveryClient;
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void discoveryClientTest() {
|
|
|
|
|
for (String service : discoveryClient.getServices()) {
|
2025-05-26 16:19:06 +08:00
|
|
|
|
System.out.println(service);
|
2025-05-20 21:38:52 +08:00
|
|
|
|
|
2025-05-26 13:32:07 +08:00
|
|
|
|
for (ServiceInstance instance : discoveryClient.getInstances(service)) {
|
2025-05-26 16:19:06 +08:00
|
|
|
|
System.out.println("IP地址:" + instance.getHost());
|
|
|
|
|
System.out.println("端口号" + instance.getPort());
|
2025-05-26 13:32:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("----------------------------------------------");
|
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
// 两个方式一样,DiscoveryClient 是 Spring自带的 NacosDiscoveryClient是 Nacos
|
2025-05-26 13:32:07 +08:00
|
|
|
|
for (String service : nacosDiscoveryClient.getServices()) {
|
2025-05-26 16:19:06 +08:00
|
|
|
|
System.out.println(service);
|
2025-05-26 13:32:07 +08:00
|
|
|
|
|
|
|
|
|
for (ServiceInstance instance : nacosDiscoveryClient.getInstances(service)) {
|
2025-05-26 16:19:06 +08:00
|
|
|
|
System.out.println("IP地址:" + instance.getHost());
|
|
|
|
|
System.out.println("端口号" + instance.getPort());
|
2025-05-26 13:32:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-20 21:38:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-26 13:32:07 +08:00
|
|
|
|
```
|
2025-05-20 21:38:52 +08:00
|
|
|
|
|
2025-05-26 13:32:07 +08:00
|
|
|
|
### 远程调用
|
2025-05-20 21:38:52 +08:00
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
订单模块调用远程商品模块,使用了nacos,可以使用`RestTemplate`,其中`RestTemplate`是线程安全的,只要注册一次全局都是可以使用。
|
2025-05-20 21:38:52 +08:00
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
**RestTemplate源码**
|
2025-05-26 13:32:07 +08:00
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
继承了`InterceptingHttpAccessor`,在`InterceptingHttpAccessor`中,使用了单例模式。
|
2025-05-26 13:32:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public ClientHttpRequestFactory getRequestFactory() {
|
|
|
|
|
List<ClientHttpRequestInterceptor> interceptors = this.getInterceptors();
|
|
|
|
|
if (!CollectionUtils.isEmpty(interceptors)) {
|
|
|
|
|
ClientHttpRequestFactory factory = this.interceptingRequestFactory;
|
|
|
|
|
if (factory == null) {
|
|
|
|
|
factory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors);
|
|
|
|
|
this.interceptingRequestFactory = factory;
|
|
|
|
|
}
|
2025-05-26 16:19:06 +08:00
|
|
|
|
|
2025-05-26 13:32:07 +08:00
|
|
|
|
return factory;
|
|
|
|
|
} else {
|
|
|
|
|
return super.getRequestFactory();
|
2025-05-20 21:38:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2025-05-26 13:32:07 +08:00
|
|
|
|
|
2025-05-26 13:56:57 +08:00
|
|
|
|
#### 实现远程调用
|
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
##### 普通方式调用
|
2025-05-26 13:56:57 +08:00
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
注册`RestTemplate`
|
2025-05-26 13:56:57 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
@Bean
|
|
|
|
|
public RestTemplate restTemplate() {
|
|
|
|
|
return new RestTemplate();
|
|
|
|
|
}
|
|
|
|
|
```
|
2025-05-26 13:32:07 +08:00
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
如果我们的服务启动了多个,在下面代码中即使一个服务宕机也可以做到远程调用。
|
2025-05-26 13:32:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
private Product getProductFromRemote(Long productId) {
|
2025-05-26 16:19:06 +08:00
|
|
|
|
// 获取商品服务所有及其的 IP+port
|
2025-05-26 13:32:07 +08:00
|
|
|
|
List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
|
|
|
|
|
ServiceInstance instance = instances.get(0);
|
2025-05-26 16:19:06 +08:00
|
|
|
|
|
|
|
|
|
// 远程URL
|
2025-05-26 13:32:07 +08:00
|
|
|
|
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/api/product/" + productId;
|
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
// 2. 远程发送请求
|
|
|
|
|
log.info("远程调用:{}", url);
|
2025-05-26 13:32:07 +08:00
|
|
|
|
return restTemplate.getForObject(url, Product.class);
|
|
|
|
|
}
|
|
|
|
|
```
|
2025-05-26 13:56:57 +08:00
|
|
|
|
|
|
|
|
|
##### 负载均衡调用
|
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
注册`RestTemplate`
|
2025-05-26 13:56:57 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
@Bean
|
|
|
|
|
public RestTemplate restTemplate() {
|
|
|
|
|
return new RestTemplate();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
使用负载均衡`LoadBalancerClient`,通过负载均衡算法动态调用远程服务。
|
2025-05-26 13:56:57 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
/**
|
2025-05-26 16:19:06 +08:00
|
|
|
|
* 远程调用商品模块 --- 负载均衡
|
2025-05-26 13:56:57 +08:00
|
|
|
|
*
|
2025-05-26 16:19:06 +08:00
|
|
|
|
* @param productId 商品id
|
|
|
|
|
* @return 商品对象
|
2025-05-26 13:56:57 +08:00
|
|
|
|
*/
|
|
|
|
|
private Product getProductFromRemoteWithLoadBalancer(Long productId) {
|
2025-05-26 16:19:06 +08:00
|
|
|
|
// 1. 获取商品服务所有及其的 IP+port
|
2025-05-26 13:56:57 +08:00
|
|
|
|
ServiceInstance instance = loadBalancerClient.choose("service-product");
|
2025-05-26 16:19:06 +08:00
|
|
|
|
|
|
|
|
|
// 远程URL
|
2025-05-26 13:56:57 +08:00
|
|
|
|
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/api/product/" + productId;
|
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
// 2. 远程发送请求
|
|
|
|
|
log.info("负载均衡远程调用:{}", url);
|
2025-05-26 13:56:57 +08:00
|
|
|
|
return restTemplate.getForObject(url, Product.class);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
##### 负载均衡注解调用
|
2025-05-26 13:56:57 +08:00
|
|
|
|
|
|
|
|
|
> [!TIP]
|
2025-05-26 16:19:06 +08:00
|
|
|
|
>
|
|
|
|
|
> 如果远程注册中心宕机是否可以调用?
|
|
|
|
|
>
|
|
|
|
|
> 调用过:远程调用不在依赖注册中心,可以通过。
|
|
|
|
|
>
|
|
|
|
|
> 没调用过:第一次发起远程调用;不能通过。
|
2025-05-26 13:56:57 +08:00
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
在`RestTemplate`上加上`@LoadBalanced`注解使用负载均衡。
|
2025-05-26 13:56:57 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
@Bean
|
|
|
|
|
@LoadBalanced
|
|
|
|
|
public RestTemplate restTemplate() {
|
|
|
|
|
return new RestTemplate();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
在实际的调用中并不需要再显式调用,将URL替换成服务名称即可。
|
2025-05-26 13:56:57 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
/**
|
2025-05-26 16:19:06 +08:00
|
|
|
|
* 远程调用商品模块 --- 负载均衡注解调用
|
2025-05-26 13:56:57 +08:00
|
|
|
|
*
|
2025-05-26 16:19:06 +08:00
|
|
|
|
* @param productId 商品id
|
|
|
|
|
* @return 商品对象
|
2025-05-26 13:56:57 +08:00
|
|
|
|
*/
|
|
|
|
|
private Product getProductFromRemoteWithLoadBalancerAnnotation(Long productId) {
|
2025-05-26 16:19:06 +08:00
|
|
|
|
// 远程URL,实现动态替换
|
2025-05-26 13:56:57 +08:00
|
|
|
|
String url = "http://service-product/api/product/" + productId;
|
2025-05-26 15:37:24 +08:00
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
// 远程发送请求
|
|
|
|
|
log.info("负载均衡注解调用:{}", url);
|
|
|
|
|
return restTemplate.getForObject(url, Product.class);
|
2025-05-26 15:37:24 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
## 按需加载
|
|
|
|
|
|
|
|
|
|
### 数据隔离架构
|
2025-05-26 15:37:24 +08:00
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|

|
2025-05-26 16:01:16 +08:00
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
### 命名空间管理
|
2025-05-26 15:37:24 +08:00
|
|
|
|
|
2025-05-26 16:19:06 +08:00
|
|
|
|
#### 创建命名空间
|
2025-05-26 15:42:35 +08:00
|
|
|
|
|
|
|
|
|
> [!TIP]
|
2025-05-26 17:29:17 +08:00
|
|
|
|
> 预配置的命名空间示例可在项目 `samples/namespace-config` 目录下找到。
|
|
|
|
|
|
|
|
|
|
建议创建以下标准命名空间:
|
|
|
|
|
|
|
|
|
|
- `dev` - 开发环境
|
|
|
|
|
- `test` - 测试环境
|
|
|
|
|
- `prod` - 生产环境
|
|
|
|
|
|
|
|
|
|
**操作步骤**:
|
2025-05-26 16:01:16 +08:00
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
1. 进入Nacos控制台命名空间管理
|
|
|
|
|
2. 点击"新建命名空间"
|
|
|
|
|
3. 填写命名空间信息(ID和名称)
|
2025-05-26 15:42:35 +08:00
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|

|
|
|
|
|

|
2025-05-26 15:42:35 +08:00
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
#### 配置管理
|
2025-05-26 15:42:35 +08:00
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
**开发环境配置示例**:
|
2025-05-26 16:01:16 +08:00
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
1. 基础配置:
|
2025-05-26 16:01:16 +08:00
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
```yaml
|
2025-05-26 16:19:06 +08:00
|
|
|
|
order:
|
|
|
|
|
timeout: 1min
|
|
|
|
|
auto-confirm: 1h
|
2025-05-26 16:01:16 +08:00
|
|
|
|
```
|
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
2. 数据库配置:
|
2025-05-26 16:01:16 +08:00
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
```yaml
|
2025-05-26 16:19:06 +08:00
|
|
|
|
order:
|
2025-05-26 17:29:17 +08:00
|
|
|
|
db-url: jdbc:mysql://dev-db:3306/order_dev
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
#### 命名空间克隆
|
|
|
|
|
|
|
|
|
|
通过克隆功能快速创建相似环境的命名空间:
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
### 动态环境配置
|
|
|
|
|
|
|
|
|
|
#### Spring Boot 配置方案
|
|
|
|
|
|
|
|
|
|
**基础配置**:
|
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
server:
|
|
|
|
|
port: 8000
|
|
|
|
|
spring:
|
|
|
|
|
application:
|
|
|
|
|
name: service-order
|
|
|
|
|
profiles:
|
|
|
|
|
active: dev
|
|
|
|
|
cloud:
|
|
|
|
|
nacos:
|
|
|
|
|
server-addr: ${NACOS_HOST:192.168.95.135}:8848
|
|
|
|
|
config:
|
|
|
|
|
namespace: ${spring.profiles.active:dev} # 动态匹配当前profile
|
|
|
|
|
group: DEFAULT_GROUP
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**多环境配置加载**:
|
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
spring:
|
|
|
|
|
config:
|
|
|
|
|
import:
|
|
|
|
|
- optional:nacos:service-order.yml
|
|
|
|
|
- optional:nacos:common.yml?group=order
|
|
|
|
|
- optional:nacos:database.yml?group=order
|
2025-05-26 16:01:16 +08:00
|
|
|
|
```
|
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
> [!NOTE]
|
|
|
|
|
> 使用`optional:`前缀可避免配置不存在时启动失败
|
2025-05-26 16:01:16 +08:00
|
|
|
|
|
2025-05-26 17:29:17 +08:00
|
|
|
|
### 配置读取实现
|
|
|
|
|
|
|
|
|
|
**配置类**:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
@Configuration
|
|
|
|
|
@ConfigurationProperties(prefix = "order")
|
|
|
|
|
@Getter
|
|
|
|
|
@Setter
|
|
|
|
|
public class OrderProperties {
|
|
|
|
|
private String timeout;
|
|
|
|
|
private String autoConfirm;
|
|
|
|
|
private String dbUrl; // 自动映射db-url
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**REST接口**:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/order")
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class OrderController {
|
|
|
|
|
private final OrderProperties orderProperties;
|
|
|
|
|
|
|
|
|
|
@GetMapping("/config")
|
|
|
|
|
public Map<String, String> getConfig() {
|
|
|
|
|
return Map.of(
|
|
|
|
|
"timeout", orderProperties.getTimeout(),
|
|
|
|
|
"autoConfirm", orderProperties.getAutoConfirm(),
|
|
|
|
|
"dbUrl", orderProperties.getDbUrl()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 按需加载策略
|
|
|
|
|
|
|
|
|
|
#### 多环境差异化配置
|
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
spring:
|
|
|
|
|
profiles:
|
|
|
|
|
active: prod # 可通过启动参数覆盖
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
# 生产环境配置
|
|
|
|
|
spring:
|
|
|
|
|
config:
|
|
|
|
|
import:
|
|
|
|
|
- nacos:service-order-prod.yml
|
|
|
|
|
- nacos:common-prod.yml?group=order
|
|
|
|
|
activate:
|
|
|
|
|
on-profile: prod
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
# 开发环境配置
|
|
|
|
|
spring:
|
|
|
|
|
config:
|
|
|
|
|
import:
|
|
|
|
|
- nacos:service-order-dev.yml
|
|
|
|
|
- nacos:database-dev.yml?group=order
|
|
|
|
|
activate:
|
|
|
|
|
on-profile: dev
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
# 测试环境配置
|
|
|
|
|
spring:
|
|
|
|
|
config:
|
|
|
|
|
import:
|
|
|
|
|
- nacos:service-order-test.yml
|
|
|
|
|
- nacos:database-test.yml?group=order
|
|
|
|
|
activate:
|
|
|
|
|
on-profile: test
|
|
|
|
|
```
|