来自/ config / application的数据库URL更改不会影响我的输出#Micronaut

我是这个微浮标和微服务的新手。这是我的问题:

我在Postgres中有2个名为“ consul”和“ consul_1”的数据库。他们两个都有一个名为“ trials”的表,具有相同的列名称(名称:varchar和age:int),但数据不同。

我有一个使用REST API的简单应用程序,使用micronaut来显示我的表数据。我听说过这种集中式配置分布式配置,然后我尝试在Yahoo格式的Consul的/ config / application KV内部构建配置。

我的配置实际上正在工作。我可以正确显示表的数据和属性(名为foo.bar)的值。但是,当我将数据源URL从../consul更改为../consul_1并将foo.bar值从 halo 更改为 hai 时,击中了/ refresh端点,然后尝试再次显示我的表数据和foo.bar的值,我的表数据保持不变,但是foo.bar更改了。

更新 我出于好奇,尝试从存储库,控制器类和POJO类中的micronaut中添加@Refreshable批注,但是这些都没有用。我还尝试从存储库实现中删除@CurrentSession@PersistenceObject(因为我认为这是原因),但也没有任何反应。

我尝试使用getEntityManagerFactory().getcache().evictAll(),尝试设置2个数据源,然后使用@Transactional("${foo.bar}"(数据源名称与foo.bar值相同)和@CurrentSession("${foo.bar})来刷新连接。但是这些没用

micronaut版本:1.2.1; JVM版本:1.8.0

这是我当前的代码:

REST控制器

@Controller("/control")
public class control {

    @Inject
    public inter Inter;

    @Get("/")
    public String test(){
        System.out.println(Inter.string());
        return String.valueOf(Inter.showAll());
    }
}

存储库界面

public interface inter {

    List<Trials> showAll();

    String string();
}

存储库实施

@Singleton
@Refreshable
public class imple implements inter {

    @Inject
    @CurrentSession
    private EntityManager entityManager;

    @Value("${foo.bar}")
    private String s;

    @Override
    @Transactional(readOnly = true)
    public List<Trials> showAll(){
        String qstring = "select u from Trials u";
        TypedQuery<Trials> query = entityManager.createQuery(qstring,Trials.class);
        List<Trials> hasil = query.getResultList();
        return hasil;
    }

    @Override
    public String string(){
        return s;
    }
}

试用班

@Entity(name = "Trials")
@Table
public class Trials {

    @Id
    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private Integer age;

    public Uji_Coba() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getage() {
        return age;
    }

    public void setage(Integer age) {
        this.age = age;
    }

    @Override
    public String toString(){
        ObjectMapper objectMapper = new ObjectMapper();
        try{
            return objectMapper.writeValueAsString(this);
        }catch (JsonProcessingException e){return "failed";}
    }
}

application.yml

micronaut:
  application:
    name: consul-config-exc
---
consul:
  client:
    registration:
      enabled: true
---
endpoints:
 all:
  enabled: true
  sensitive: false
 stop:
  enabled: false

bootstrap.yml

micronaut:
  application:
    name: consul-config-exc
  config-client:
    enabled: true

consul:
  client:
    config:
      format: YAML
    defaultZone: "${CONSUL_HOST:localhost}:${CONSUL_PORT:8500}"

最后,在领事键/值中输入 / config / application

foo:
 bar: halo
---
datasources:
 default:
  url: jdbc:postgresql://localhost:5432/consul
  username: postgres
  password: root
  driverClassname: org.postgresql.Driver
---
jpa:
 default:
  entity-scan:
   - "consul.config.exc.domain"

当我将“ consul”更改为“ consul_1”时,我想要的是,我只需要命中/ refresh端点(由micronaut提供),然后数据库就会更改。但是目前,我需要重新启动应用程序才能对其进行更改。

谁能告诉我我做错了什么,并告诉我如何使它起作用?非常感谢,英语不好。

luxi19830817 回答:来自/ config / application的数据库URL更改不会影响我的输出#Micronaut

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3167806.html

大家都在问