简体   繁体   English

Spring 云微服务服务器端口转换异常

[英]Spring cloud microservice server port cast exception

Being new to spring boot and microservices architectures, trying to build a simple app using spring boot cloud starter.作为 spring 引导和微服务架构的新手,尝试使用 spring 引导云启动器构建一个简单的应用程序。

I've used spring-cloud-config-server in order to externalize config files (.properties)我使用spring-cloud-config-server来外部化配置文件(.properties)

I've got here the spring cloud config which brings.properties files from a git repository and another microservice (product-microservice) trying to reach its config from the spring-cloud-config-server microservice.我这里有 spring 云配置,它带来了来自 git 存储库的.properties 文件和另一个试图从spring-cloud-config-server微服务访问其配置的微服务(product-microservice)

Everything works fine, except for the server.port property which throws a ClassCastException (inside the 2nd microservice)一切正常,除了抛出ClassCastExceptionserver.port属性(在第二个微服务内)

Here's the config file: https://github.com/mssm199996/microservice-ecommerce-2/blob/master/product-microservice.properties这是配置文件: https://github.com/mssm199996/microservice-ecommerce-2/blob/master/product-microservice.properties

Code about the 1st microservice:关于第一个微服务的代码:

@EnableConfigServer
@SpringBootApplication
public class CloudServerConfigApplication {

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mssmfactory</groupId>
    <artifactId>cloud-server-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-server-config</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.BUILD-SNAPSHOT</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
</project>

server.port=8888
spring.application.name=config-server

spring.cloud.config.server.git.uri=https://github.com/mssm199996/microservice-ecommerce-2.git

Code about the 2nd microservice:关于第二个微服务的代码:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mproduits</groupId>
    <artifactId>mproduits</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mproduits</name>
    <description>Microservice de gestion des produits</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.BUILD-SNAPSHOT</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- ********************************************************************** -->

        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>-->

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

spring.application.name=product-microservice

*************************** APPLICATION FAILED TO START ********************* ****************************** 应用程序无法启动 ******************* **


Description:描述:

Failed to bind properties under 'server.port' to java.lang.Integer:无法将“server.port”下的属性绑定到 java.lang.Integer:

 Property: server.port Value: {value=9091, origin=1:15} Origin: "server.port" from property source "bootstrapProperties" Reason: No converter found capable of converting from type [java.util.LinkedHashMap<?, ?>] to type [java.lang.Integer]

Action:行动:

Update your application's configuration更新应用程序的配置

Finally found the problem.终于发现问题了。

It was an issue in the pom.xml file, because i've downloaded the above microservice from a github repository and it was using an old version of spring that i modified without modifying the spring cloud version which conflicts with the newer version of spring. It was an issue in the pom.xml file, because i've downloaded the above microservice from a github repository and it was using an old version of spring that i modified without modifying the spring cloud version which conflicts with the newer version of spring.

What i did was, creating a new module in intellij using spring intializr and adding spring cloud config client dependancy to it so that it generates automaticaly the pom.xml file, and used it in my microservice.我所做的是,使用spring intializr在 intellij 中创建一个新模块,并添加spring 云配置客户端依赖关系,以便它自动生成pom.xml文件,并在 myservice 文件中使用它。

here's the new pom.xml这是新的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mproduits</groupId>
    <artifactId>mproduits</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mproduits</name>
    <description>Microservice de gestion des produits</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.M3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- ********************************************************************** -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
</project>

Hope it helps !希望能帮助到你 !

You need to override your properties which are sent from config server , then add in product-microservice.properties the following configurations:您需要覆盖从config server发送的属性,然后在product-microservice.properties中添加以下配置:

spring.cloud.config.allowOverride=true
spring.cloud.config.overrideNone=false
spring.cloud.config.overrideSystemProperties=false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用微服务 AppRole 到 Spring 云配置服务器和 Vault 集成 - Using Microservice AppRole to Spring Cloud Config Server and Vault integration 微服务不从 spring-cloud-config-server 微服务中获取属性 - microservice doesn't fetch properties from spring-cloud-config-server microservice Web 服务器启动失败。 端口 8080 已被使用。 Spring 启动微服务 - Web server failed to start. Port 8080 was already in use. Spring Boot microservice 仅在弹簧云微服务实例中使用https - Using https only with spring cloud microservice instances 使用spring cloud更改eureka服务器的默认端口 - Changing default port of eureka server using spring cloud Spring 启动微服务作为 Kafka 消费者异常 - Spring boot microservice as Kafka consumer exception Spring集成DSL | Jch异常| 与&lt;断开连接 <Server> &gt;端口22 - Spring Integration DSL | Jch Exception | Disconnecting from <<Server>> port 22 Spring Boot微服务中的Spring Cloud Sleuth日志跟踪 - Spring Cloud Sleuth log tracing in Spring Boot Microservice 来自 Spring Configuration Server 的 MicroService 的不同配置 - Different configurations for MicroService from Spring Configuration Server Spring Cloud Port Conflict 8888 - Spring Cloud Port Conflict 8888
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM