Skip to content
欢迎扫码关注公众号

动态指定 Spring Boot 服务的名字

同一个服务需要使用多个不同的服务名发布时,可以通过环境变量的方式指定服务的名字(环境变量可以通过启动命令指定,也可以通过配置 docker 镜像的环境变量指定)。
另外,服务配置是在 Nacos 中配置的,同样的需要通过环境变量指定配置文件的名字。

示例文件 bootstrap.yml 如下:

yaml
spring:
  application:
    name: ${application.name:App-A}
  cloud:
    nacos:
      config:
        namespace: ${nacos.namespace:}
        server-addr: ${nacos.url}:8001
        group: DEFAULT_GROUP
        prefix: ${nacos.config.prefix:app-config-a}
        file-extension: yaml
        refreshable-dataids:
      discovery:
        namespace: ${nacos.namespace:}
        server-addr: ${nacos.url}:8001

可以看到上面的配置文件中通过 ${application.name:App-A} 的方式来设置 spring.application.name 属性的值。

: 前面的 application.name 是环境变量名,: 后面的 App-A 是默认值。

IDEA 中可以通过在配置界面的 Environment variables 中指定环境变量,格式如下:

shell
application.name=App-B;nacos.config.prefix=app-config-b

k8s 中,可以在 docker 容器配置文件的 env 项中设置环境变量,格式如下:

yaml
containers:
  - env:
    - name: application.name
      value: App-B
    - name: nacos.config.prefix
      value: app-config-b

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.