✨ 远程配置读取
This commit is contained in:
parent
13c5304139
commit
4c25f24561
|
@ -405,3 +405,112 @@ private Product getProductFromRemoteWithLoadBalancerAnnotation(Long productId) {
|
|||
return restTemplate.getForObject(url, Product.class);
|
||||
}
|
||||
```
|
||||
|
||||
### 远程配置读取
|
||||
|
||||
#### 1、引入依赖
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
#### 2、在nacos中配置
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
```yml
|
||||
order:
|
||||
timeout: 30min
|
||||
auto-confirm: true
|
||||
```
|
||||
|
||||
#### 3、创建接口访问
|
||||
|
||||
```java
|
||||
@RestController
|
||||
@RequestMapping("/api/order")
|
||||
@RequiredArgsConstructor
|
||||
public class OrderController {
|
||||
|
||||
private final OrderService orderService;
|
||||
|
||||
@Value("${order.timeout}")
|
||||
private String timeout;
|
||||
|
||||
@Value("${order.auto-confirm}")
|
||||
private String autoConfirm;
|
||||
|
||||
@Operation(summary = "读取配置")
|
||||
@GetMapping("config")
|
||||
public String config() {
|
||||
return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 4、SpringBoot中配置
|
||||
|
||||
> [!CAUTION]
|
||||
>
|
||||
> 需要注意的是,不要在`server-addr`地址中写`${nacos.server-addr}`,否则无法访问正确的地址。
|
||||
|
||||
> [!NOTE]
|
||||
>
|
||||
> 如果某个项目模块暂时不需要动态配置,但是引入了,可以在配置中加上,导入检查。
|
||||
>
|
||||
> ```yaml
|
||||
> cloud:
|
||||
> nacos:
|
||||
> server-addr: 192.168.95.135:8848
|
||||
> config:
|
||||
> import-check:
|
||||
> enabled: false
|
||||
> ```
|
||||
>
|
||||
> 如果某个配置是可选也可以在前面加上`ptional:`。
|
||||
>
|
||||
> ```yaml
|
||||
> spring:
|
||||
> config:
|
||||
> import:
|
||||
> - optional:nacos:service-order.yml
|
||||
> ```
|
||||
|
||||
```yaml
|
||||
server:
|
||||
port: 8000
|
||||
spring:
|
||||
application:
|
||||
name: service-order
|
||||
profiles:
|
||||
active: dev
|
||||
config:
|
||||
import:
|
||||
- nacos:service-order.yml
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: 192.168.95.135:8848
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
||||
```
|
||||
|
||||
#### 5、动态刷新读取
|
||||
|
||||
如果需要动态刷新需要在控制器或者是启动类上加上`@RefreshScope`
|
||||
|
||||
```java
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@RefreshScope
|
||||
public class ProductServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ProductServiceApplication.class, args);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 39 KiB |
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
|
@ -64,6 +64,10 @@
|
|||
<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>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
|
|
|
@ -3,9 +3,11 @@ package cn.bunny.service;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@RefreshScope
|
||||
public class OrderServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OrderServiceApplication.class, args);
|
||||
|
|
|
@ -4,6 +4,7 @@ import cn.bunny.model.order.bean.Order;
|
|||
import cn.bunny.service.service.OrderService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
@ -15,9 +16,21 @@ public class OrderController {
|
|||
|
||||
private final OrderService orderService;
|
||||
|
||||
@Value("${order.timeout}")
|
||||
private String timeout;
|
||||
|
||||
@Value("${order.auto-confirm}")
|
||||
private String autoConfirm;
|
||||
|
||||
@Operation(summary = "创建订单")
|
||||
@GetMapping("create")
|
||||
public Order createOrder(Long userId, Long productId) {
|
||||
return orderService.createOrder(productId, userId);
|
||||
}
|
||||
|
||||
@Operation(summary = "读取配置")
|
||||
@GetMapping("config")
|
||||
public String config() {
|
||||
return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
server:
|
||||
port: 8000
|
||||
|
||||
|
||||
nacos:
|
||||
server-addr: 192.168.95.135:8848
|
||||
|
|
|
@ -5,6 +5,12 @@ spring:
|
|||
name: service-order
|
||||
profiles:
|
||||
active: dev
|
||||
config:
|
||||
import:
|
||||
- nacos:service-order.yml
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: ${nacos.server-addr}
|
||||
server-addr: 192.168.95.135:8848
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
|
@ -3,9 +3,11 @@ package cn.bunny.service;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@RefreshScope
|
||||
public class ProductServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ProductServiceApplication.class, args);
|
||||
|
|
|
@ -5,6 +5,10 @@ spring:
|
|||
name: service-product
|
||||
profiles:
|
||||
active: dev
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: ${nacos.server-addr}
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
||||
|
|
Loading…
Reference in New Issue