Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 环境变量和@Value可以';不要在弹簧靴上一起工作_Java_Spring_Docker_Spring Boot_Docker Compose - Fatal编程技术网

Java 环境变量和@Value可以';不要在弹簧靴上一起工作

Java 环境变量和@Value可以';不要在弹簧靴上一起工作,java,spring,docker,spring-boot,docker-compose,Java,Spring,Docker,Spring Boot,Docker Compose,我有一个SpringBoot应用程序,它连接到一个充当缓存的Redis实例。在开发环境中,我有以下几点: --- spring: profiles: default redis: host: localhost port: 6379 我的缓存配置类如下所示: @Configuration @EnableCaching public class CacheConfiguration { @Value("${redis.host}") String redisHos

我有一个SpringBoot应用程序,它连接到一个充当缓存的Redis实例。在开发环境中,我有以下几点:

---

spring:
  profiles: default
redis: 
  host: localhost
  port: 6379
我的缓存配置类如下所示:

@Configuration
@EnableCaching
public class CacheConfiguration {

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

   @Value("${redis.port}") 
   int redisPort;
在生产过程中,此应用程序已被docker化,我有以下
docker compose.yml
文件:

redis: 
  image: tutum/redis
  ports:
    - "6379:6379"
  volumes:
    - /data
app: 
  build: .
  ports:
    - "8080:8080"
  links:
    - redis
redis: 
  image: tutum/redis
  ports:
    - "6379:6379"
  volumes:
    - /data
应用程序.yml
是:

---

spring:
  profiles: docker
redis: 
  host: redis
  port: 6379
要在Docker上启动应用程序,我使用
-Dspring.profiles.active=Docker运行,但当应用程序启动时,会发生以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private int com.inkdrop.config.cache.CacheConfiguration.redisPort; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type [java.lang.String] to required type [int]; nested exception is java.lang.NumberFormatException: For input string: "tcp://172.17.0.3:6379"
出于某种原因,Spring Boot正在将redis.port读取为
tcp://172.17.0.3:6379
。因此,对于测试,我从
CacheConfiguration
类中删除了
@Value
注释,并手动将其设置为
redis
作为主机,
6379
作为端口,然后它工作了。似乎在使用环境变量和
@Value
时,Spring会丢失。有人有想法吗?

基于:

Compose使用Docker链接将服务容器公开给一个 另一个每个链接容器注入一组环境变量, 每一个都以容器的大写名称开头

Docker Compose将使用name\u-PORT格式(例如
REDIS\u-PORT)创建一个表示容器完整URL的环境变量=tcp://172.17.0.5:6379

并根据您的
docker compose.yml
文件:

redis: 
  image: tutum/redis
  ports:
    - "6379:6379"
  volumes:
    - /data
app: 
  build: .
  ports:
    - "8080:8080"
  links:
    - redis
redis: 
  image: tutum/redis
  ports:
    - "6379:6379"
  volumes:
    - /data
您将有一个名为
REDIS\u PORT
的环境变量,其值等于
tcp://172.17.0.3:6379
。由于操作系统环境变量相对于特定于配置文件的应用程序属性具有更高的优先级,Spring Boot将在
REDIS.PORT
上拾取
REDIS\u PORT
值,因此出现错误:

原因:org.springframework.beans.factory.BeanCreationException: 无法自动关联字段:private int com.inkdrop.config.cache.CacheConfiguration.redisPort;嵌套 异常为org.springframework.beans.typemischException:失败 将[java.lang.String]类型的值转换为所需的[int]类型; 嵌套异常为java.lang.NumberFormatException:对于输入字符串: "tcp://172.17.0.3:6379"

作为此问题的解决方法,您应该使用端口值覆盖
REDIS\u PORT
环境变量,或者将配置名称从
REDIS.name
重命名为争议较小的名称

有点离题,但只是引用了图腾·多克尔·雷迪斯的话:

此图像将很快被弃用。请使用docker官方软件 图片:


您使用的是哪个Spring启动版本?我还会尝试在docker compose文件本身中设置环境变量:
环境:redis.port=6379
,看看会发生什么。现在使用1.3.4。我会试试的我现在不在开发人员的电脑上,我稍后会试试。现在,我想这正是发生的事情。我现在接受这个答案:D