spring cloud config客户端没有从配置服务器加载配置

时间:2015-05-03 17:14:28

标签: spring cloud config

我关注此链接: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage

我一次又一次地测试了这个并没有看到Spring云客户端正在从云服务器加载配置,请帮助看看错误在哪里:

POM:

    @Configuration
@EnableAutoConfiguration
@RestController
public class ConfigclientApplication {

    @Value("${spring.cloud.config.uri}")
    String url;

    @Value("${production.host}")
    String host;


    @RequestMapping("/")
    public String home() {
        return "Host is  => " + this.host ;
    }

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

应用:

<?php

if(isset($_POST)) {

$total_100  = $_POST['100']  * 100     ;
$total_50   = $_POST['50' ]  *  50     ;
$total_20   = $_POST['20' ]  *  20     ; 
$total_10   = $_POST['10' ]  *  10     ;
$total_5    = $_POST['5'  ]  *   5     ;
$total_2    = $_POST['2'  ]  *   2     ;
$total_1    = $_POST['1'  ]  *   1     ;
$total_05   = $_POST['05' ]  *   0.5   ; 
$total_02   = $_POST['02' ]  *   0.2   ; 
$total_01   = $_POST['01' ]  *   0.1   ;
$total_005  = $_POST['005']  *   0.05  ;

$total = $total_100 + $total_50 + $total_20 + $total_10 + $total_5;  

} else {

$total_100  = 0  ;
$total_50   = 0  ;
$total_20   = 0  ; 
$total_10   = 0  ;
$total_5    = 0  ;
$total_2    = 0  ;
$total_1    = 0  ;
$total_05   = 0  ; 
$total_02   = 0  ;
$total_005  = 0  ;

$total = $total_100 + $total_50 + $total_20 + $total_10 + $total_5 + $total_2 +  $total_1 +  $total_05 +  $total_02 +  $total_01 +  $total_005 ;  

}

?>


<body>

<h1>kas opmaak</h1>

<form action="#" method="POST">
<table>
            <tr>
                <td><input type="number" name="100" value="<?php echo $_POST['100']; ?>"></td>
                <td>X</td>
                <td>&#8364; 100</td>
                <td> = </td>
                <td align="right"><?php echo $total_100;?></td>
            </tr>
            <tr>
                <td><input type="number" name="50"  value="<?php echo $_POST['50'];?>"></td>
                <td>X</td>
                <td>&#8364; 50</td>
                <td> = </td>
                <td align="right"><?php echo $total_50;?></td>
            </tr>
            <tr>
                <td><input type="number" name="20"  value="<?php echo $_POST['20'];?>"></td>
                <td>X</td>
                <td>&#8364; 20</td>
                <td> = </td>
                <td align="right"><?php echo $total_20;?></td>
            </tr>
            <tr>
                <td><input type="number"  name="10" value="<?php echo $_POST['10'];?>"></td>
                <td>X</td>
                <td>&#8364; 10</td>
                <td> = </td>
                <td align="right"><?php echo $total_10;?></td>
            </tr>           
            <tr>
                <td><input type="number"  name="5"  value="<?php echo $_POST['5'];?>"></td>
                <td>X</td>
                <td>&#8364; 5</td>
                <td> = </td>
                <td align="right"><?php echo $total_5;?></td>
            </tr>
<tr>
                <td><input type="number"  name="2"  value="<?php echo $_POST['2'];?>"></td>
                <td>X</td>
                <td>&#8364; 2</td>
                <td> = </td>
                <td align="right"><?php echo $total_2;?></td>
            </tr>
<tr>
                <td><input type="number"  name="1"  value="<?php echo $_POST['1'];?>"></td>
                <td>X</td>
                <td>&#8364; 1</td>
                <td> = </td>
                <td align="right"><?php echo $total_1;?></td>
            </tr>
<tr>
                <td><input type="number"  name="05"  value="<?php echo $_POST['05'];?>"></td>
                <td>X</td>
                <td>&#8364; 0,50</td>
                <td> = </td>
                <td align="right"><?php echo $total_05;?></td>
            </tr>
<tr>
                <td><input type="number"  name="02"  value="<?php echo $_POST['02'];?>"></td>
                <td>X</td>
                <td>&#8364; 0,20</td>
                <td> = </td>
                <td align="right"><?php echo $total_02;?></td>
            </tr>
<tr>
                <td><input type="number"  name="01"  value="<?php echo $_POST['01'];?>"></td>
                <td>X</td>
                <td>&#8364; 0,10</td>
                <td> = </td>
                <td align="right"><?php echo $total_01;?></td>
            </tr>
            <tr>
                <td><input type="number"  name="005"  value="<?php echo $_POST['005'];?>"></td>
                <td>X</td>
                <td>&#8364; 0,05</td>
                <td> = </td>
                <td align="right"><?php echo $total_005;?></td>
            </tr>           
            <tr>
                <td colspan="4"><strong> Totaal contant </strong></td>

                <td><?php echo $total;?></td>
            </tr>               








            <tr>
                <td colspan="5" align="right"><br/>  <input type="submit" value="Calculate cash!"></td>

            </tr>

</table>        
</form>

bootstrap.properties: spring.cloud.config.uri = http://localhost:8888

  1. 配置服务器很好: http://localhost:8888/spirent/default

    { “名称”: “思博伦”, “简档”:[ “默认”], “标签”: “主”, “propertySources”:[{ “名称”: “类路径:/spirent.yml”,”源 “:{” production.host “:” 服务器1" , “production.port”:9999, “production.value1”:12345 “test.host”: “server2.com”, “test.port”:4444, “test.value”: “hello123”}}]}

  2. 现在http://localhost:8080/根本无法启动。

    创建名为'configclientApplication'的bean时出错 似乎自动注入@Value无法找到production.host环境值。

  3. 从配置服务器加载后,如何在客户端中读取配置?

    感谢您的帮助。

4 个答案:

答案 0 :(得分:30)

如果您使用的是 2020.0.0 版本的 spring cloud,那么您需要在 Maven 依赖项中使用此依赖项来启用引导程序,该程序在 2020.0.0 中默认禁用。

Breaking changes in 2020.0.0

它对我有用。

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

答案 1 :(得分:7)

如果您使用的是 2020.0.0 版本的 spring cloud,那么您需要在 Maven 依赖项中使用此依赖项来启用引导程序,该程序在 2020.0.0 中默认禁用。

2020.0.0 中的重大变化

它对我有用。

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

是的,这是正确的,这对我有用......谢谢

答案 2 :(得分:6)

对于那些在使用了与Spring Boot 2.4.0或2.4.1相关的默认Maven Spring Cloud依赖版本(例如,对于Spring Boot 2.4.0的2020.0.0-M5)之后使用RubinsMN的良好建议的人,请注意,此类Spring Cloud依赖项默认情况下未启用引导程序。根据{{​​3}}:

spring-cloud-commons提供的引导程序在默认情况下不再启用。如果您的项目需要它,则可以通过属性或新的启动器重新启用它。

  • 要通过属性重新启用,请设置spring.cloud.bootstrap.enabled = true或spring.config.use-legacy-processing = true。这些必须设置为环境变量,java系统属性或命令行参数。
  • 另一种选择是在您的POM文件中包括新的spring-cloud-starter-bootstrap。

我使用了第二个选项,并且工作正常。

答案 3 :(得分:5)

正如Deinum所暗示的那样,我确保您将客户端配置为 parent 作为spring-cloud-starter-parent并为其提供版本。当你在依赖项中包含并且记住云是一个与启动不同的项目时,Spring云提供的Maven插件不会工作。将其更改为:

<parent>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-parent</artifactId>
    <version>1.0.0.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

其次,作为一门新学科(可能不是你的问题),我会在你的应用程序上使用新的注释而不是@Configuration和@EnableAutoConfiguration

@SpringBootApplication

第三,仔细检查配置服务器上是否有@EnableConfigServer

第四,确保客户端上的bootstrap.properties具有指定的spring应用程序名称:

spring.application.name=spirent

最后,如果您使用了spring-cloud-config示例项目,则必须在URI中设置默认用户和安全密码:

http://user:ddf4757e-0077-42e4-b2ad-2ae04340b08c@localhost:8888

否则,请尝试从位于此处的spring-cloud-config项目开始,以确保您的配置服务器设置正确:

https://github.com/spring-cloud/spring-cloud-config