首页 文章

Spring Cloud Config JDBC后端

提问于
浏览
1

我有必要配置一个Spring Cloud Config Server,以便从JDBC后端(PostgreSQL)而不是Git存储库中读取variuos实例的属性 . 我按照官方文档,但不起作用 . 我添加到Spring Cloud Config Server的application.properties

spring.profiles.active=jdbc
spring.datasource.url= jdbc:postgresql://localhost:5432/example
spring.datasource.username=user
spring.datasource.password=pass

我在数据库中创建表

CREATE TABLE public."PROPERTIES"
(
    "APPLICATION" character varying(500) COLLATE pg_catalog."default",
    "PROFILE" character varying(500) COLLATE pg_catalog."default",
    "LABEL" character varying(500) COLLATE pg_catalog."default",
    "KEY" character varying(500) COLLATE pg_catalog."default",
    "VALUE" character varying(500) COLLATE pg_catalog."default"
)

并且,对于eureka服务器,我插入

INSERT INTO public."PROPERTIES"
("APPLICATION", "PROFILE", "LABEL", "KEY", "VALUE")
VALUES('discovery-service', '', '', 'spring.application.name', 'discovery-service');
INSERT INTO public."PROPERTIES"
("APPLICATION", "PROFILE", "LABEL", "KEY", "VALUE")
VALUES('discovery-service', '', '', 'server.port', '8761');

如果我在Git存储库中使用此参数,Eureka Server工作正常,但使用JDBC后端不起作用 . 有人可以帮帮我吗?

1 回答

  • 1

    说“属性”不存在的PSQLException是因为PostgreSQL区分大小写 . 使用PGAdmin创建表/列时,如果名称全部为大写,则工具会自动添加引号,从而使其区分大小写 .

    您可以尝试以下方法:

    CREATE TABLE public.properties
    (
        application character varying(50) COLLATE pg_catalog."default",
        profile character varying(50) COLLATE pg_catalog."default",
        label character varying(50) COLLATE pg_catalog."default",
        key character varying(50) COLLATE pg_catalog."default",
        value character varying(500) COLLATE pg_catalog."default"
    )
    
    INSERT INTO properties
    ("application", "profile", "label", "key", "value")
    VALUES('myconfig', 'default', 'master', 'my.message', 'hello');
    

    application.properties包含:

    server.port=8888
    spring.profiles.active=jdbc
    spring.datasource.driver-class-name=org.postgresql.Driver
    spring.datasource.url= jdbc:postgresql://localhost:5432/configdb
    spring.datasource.username=xxxxxx
    spring.datasource.password=xxxxxx
    

    您现在可以使用http://localhost:8888/myconfig/default进行访问

相关问题