您的位置:首页 > 编程语言 > Java开发

SpringBoot客户端调用服务端的配置文件,启动后无法加载页面

2019-04-09 21:02 507 查看

如果你的客户端向服务端请求了git的配置文件,@value成功加载后,再通过页面进不去控制层,可能是因为客户端请求到了配置文件的同时,配置文件里配置了port端口号,把客户端的端口号覆盖掉了。代码如下

这是springboot的启动类

package com.new0409.new0409;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class New0409Application {

@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}

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

}

这是controller层

package com.a1824.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 当有请求/fresh节点的时候,会重新请求一次ConfigServer去拉取最新的配置文件
* 请求/fresh需要有几点要求:1.加actuator的依赖 2.SpringCloud1.5以上需要设置 management.security.enabled=false
* 这个Controller的作用是查看from这个key的值
*/
@RestController
@RefreshScope //开启更新功能
@RequestMapping("/aa")
public class TestController {

@Value("${rmq.namesrv_addr}")
private String fromValue;

/**
* 返回配置文件中的值
*/
@GetMapping("/aa")
public String returnFormValue(){
return fromValue;
}
}

这是bootstrap.xml

spring:
application:
name: hellxztest                     #指定了配置文件的应用名
cloud:
config:
uri: http://localhost:8888/        #Config server的uri
profile: dev                       #指定的环境
label: master                      #指定分支
server:
port: 7002

下面这行控制台提示信息提示了,端口号被修改,而修改后的端口号正是配置文件里的端口号。

2019-04-09 19:36:23.267  INFO 8868 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 10031

怎么恢复暂时不清楚,搞清楚了上传解决方案。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐