Spring Boot项目中的全局变量和应用程序变量定义 [英] Global variables And Application Variables Defining in Spring boot project

查看:2319
本文介绍了Spring Boot项目中的全局变量和应用程序变量定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用Spring和Spring Boot开发微服务.在我的项目中,我正在将整体式架构转换为面向服务的架构.项目包含20个Micro服务.我需要设置应用程序变量和全局变量.我对此有一些困惑,我在这里添加了这些困惑,

I am trying to develop micro service by using spring and spring boot. In my project , I am converting monolithic to service oriented architecture. Project contain 20 Micro services.I these I need to set application variables and global variables. I have confusions related to this , And I am adding those confusions here,

  1. 是否可以在application.properties文件中声明我的全局变量?如果无法在哪里定义我的全局变量?
  2. 如果我使用spring config服务器进行全局配置,如何将这些属性有条件地导入客户端项目?
  3. 是否可以在配置服务器中为不同的配置文件设置不同的属性文件,并有条件地将不同的配置文件导入客户端项目?这里的每个个人资料代表我的情况下的不同区域.

推荐答案

在我的探索之后,我找到了解决此问题的解决方案,以加载全局变量和应用程序变量,包括数据库配置.最好的使用方法是-Spring Cloud Config服务器外部化配置.

After My Exploration I find out Solution for this problem for loading global variables and application variables including database configuration. The best way we can use that is - spring cloud config server externalized configuration.

我们可以为Spring Cloud Config Server创建一个微服务.在配置服务器中,我们可以通过两种方式创建变量和配置.

We can create a microservice for spring cloud config server. In config server we can create our variables and configuration in two ways.

  1. GIT链接参考中的配置
  2. 使用本地文件系统/环境变量.

链接参考

  1. https://cloud.spring.io /spring-cloud-config/single/spring-cloud-config.html
  2. https ://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.3.RELEASE/multi/multi__spring_cloud_config_server.html
  3. https://github.com/spring-cloud/spring-cloud-config
  1. https://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html
  2. https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.3.RELEASE/multi/multi__spring_cloud_config_server.html
  3. https://github.com/spring-cloud/spring-cloud-config

我在这里使用本地文件系统.

需要在src/main/resources下创建Config文件夹.并按照命名约定创建不同的配置文件,

Need to create Config folder under src/main/resources. And create different profiles by following naming convention,

db,properties,db-test.properties,db-prod.properties,db-dev.properties. 我为不同的开发环境创建了示例.就像我们可以为变量和配置创建任何配置文件一样.

db,properties , db-test.properties , db-prod.properties , db-dev.properties. I created for example for different development environment. Like we can create any profiles for variables and configuration.

并在application.properties中为配置服务器添加以下内容

And add following in application.properties for config server

server.port=8888
spring.profiles.active=native

在配置服务器的pom.xml文件中添加配置服务器依赖项,

Add config server dependency in pom.xml file of config server,

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

将以下内容添加到主应用程序运行类中,

Add the following into main application run class,

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
    }
}

并通过添加pom.xml依赖关系来创建客户端微服务项目,

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

在application.properties文件中添加以下行,以设置客户端以从服务器接收配置,

Add following line in application.properties file for setting client to receive configuration from server,

server.port=8080
spring.application.name=db
spring.cloud.config.uri=localhost:8888

最后通过指定配置文件来运行您的客户端项目,

Finally run your client project by specifying profile ,

java -jar -Dsping.profiles.active=<profile> <jar_name>.jar

预先感谢

这篇关于Spring Boot项目中的全局变量和应用程序变量定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆