好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Quarkus集成apollo配置中心

前言

Quarkus默认的配置文件和spring boot 一样,默认读取application.properties文件。

apollo是一个配置集中管理的开源项目,已被广泛应用。

下面我们就分析下Quarkus的配置加载结构,将apollo集成进来。

Eclipse MicroProfile Config: https://github测试数据/eclipse/microprofile-config/

smallrye-config: https://github测试数据/smallrye/smallrye-config

Quarkus的config构成

Quarkus的配置功能是基于Eclipse MicroProfile Config配置规范而来的,MicroProfile Config本身不提供配置功能的实现,只提供了基础的配置api抽象,smallrye-config是这个api的其中一个实现,Quarkus里用的就是smallrye-config。

microProfile config设计

1、可以通过ConfigProvider#getConfig()访问应用程序的当前配置。

2、一个配置包括从org.eclipse.microprofile.config.spi.ConfigSource接口收集的列表。

这些ConfigSource根据其序号进行排序。这样,可以从外部以较低的重要性覆盖配置。

默认情况下,有3个默认的ConfigSources:

System.getProperties() (ordinlal =400) System.getenv()(ordinal =300) ClassPath上的所有META-INF / microprofile-config.properties文件。(默认ordinal = 100,可通过每个文件内的config_ordinal属性分别配置)

因此,可以在与应用程序打包在一起的上述文件中指定默认值,以后可以为每个部署覆盖默认值。较高的序数优先于较低的序数。

从microProfile config设计来看,配置文件应该是META-INF / microprofile-config.properties文件才对,但是除了上面默认的三个配置源外,Quarkus也提供了一个,代码见:

io.quarkus.runtime.configuration.ApplicationPropertiesConfigSource,

如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

public abstract class ApplicationPropertiesConfigSource extends PropertiesConfigSource {

     private static final long serialVersionUID = -4694780118527396798L;

     static final String APPLICATION_PROPERTIES = "application.properties" ;

     static final String MP_PROPERTIES = "META-INF/microprofile-config.properties" ;

     ApplicationPropertiesConfigSource(InputStream is, int ordinal) {

         super (readProperties(is), APPLICATION_PROPERTIES, ordinal);

     }

     ApplicationPropertiesConfigSource(InputStream is, String nm, int ordinal) {

         super (readProperties(is), nm, ordinal);

     }

     private static Map readProperties( final InputStream is) {

         if (is == null ) {

             return Collections.emptyMap();

         }

         try (Closeable ignored = is) {

             try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8)) {

                 try (BufferedReader br = new BufferedReader(isr)) {

                     final Properties properties = new Properties();

                     properties.load(br);

                     return (Map) (Map) properties;

                 }

             }

         } catch (IOException e) {

             throw new IOError(e);

         }

     }

}

集成apollo 

综上,集成apollo就变得异常简单了,首先实现ConfigSource接口,我的实现如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

/**

  * @author kl : http://kailing.pub

  * @version 1.0

  * @date 2020/7/8 11:15

  */

public class ApolloConfigSource implements ConfigSource {

     private final static String CONFIG_SOURCE_NAME = "Apollo" ;

     private final Config config = ConfigService.getAppConfig();

     @Override

     public SetgetPropertyNames() {

         return config.getPropertyNames();

     }

     @Override

     public int getOrdinal() {

         return 600 ;

     }

     @Override

     public Map getProperties() {

         Map properties = new HashMap<>( 6 );

         for (String key : getPropertyNames()) {

             properties.put(key, config.getProperty(key, null ));

         }

         return properties;

     }

     @Override

     public String getValue(String propertyName) {

         return config.getProperty(propertyName, null );

     }

     @Override

     public String getName() {

         return CONFIG_SOURCE_NAME;

     }

}

第二步,在META-INF/services下,创建文件

org.eclipse.microprofile.config.spi.ConfigSource

将你的实现全路径名称写入这个文件,声明配置源即可,如下图所示: 

 现在,你可以将你的application.properties中的所有配置全部复制到apollo中了,然后删除这个文件,重新启动项目,你会发现一起运行正常

以上就是Quarkus集成apollo配置中心的详细内容,更多关于Quarkus集成apollo配置的资料请关注其它相关文章!

原文链接:http://HdhCmsTestkailing.pub/article/index/arcid/287.html

查看更多关于Quarkus集成apollo配置中心的详细内容...

  阅读:10次