美文网首页
springboot+sprcloud+consul父子工程模式

springboot+sprcloud+consul父子工程模式

作者: 楼兰King | 来源:发表于2020-09-29 15:29 被阅读0次
    前言:

    环境描述:
    docker部署的consul
    在搭建工程中,试过很多种玩法,行得通与不通,都去了解下,本文章提供的肯定是完善的一种模式。搭建中会遇到一些错误.
    比如:

    Connect to localhost:8500 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] f
    

    其实打开浏览器访问等都是可以的,那么为什么项目中启动会报此错误呢。


    image.png

    这个时候,先不要忙着觉得是不是docker什么端口,宿主机什么通不通,什么防火墙端口之类的,直接忽略,这不浏览器访问都正常,那就先从项目中下手。
    1.直接创建一个单独项目,启动正常,看来就是版本整合的问题了,切记,springboot+cloud整合时候,版本问题一定要注意,可以网上找资料看下版本对应。

    单独测试项目如下:
    image.png
    =======main启动========
    package com.king;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class clouddemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(clouddemoApplication.class, args);
        }
    }
    =====pom配置======
    <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>
      <groupId>com.king</groupId>
      <artifactId>clouddemo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>clouddemo</name>
      <properties>
            <java.version>11</java.version>
            <spring-cloud.version>Finchley.SR2</spring-cloud.version>
            <spring-boot.version>2.0.8.RELEASE</spring-boot.version>
        </properties>
    
        <!-- spring-cloud所有项目依赖包 -->
        <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>
    
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    <dependencies>
            <!-- SpringBoot整合Web组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--SpringCloud consul-server -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <encoding>utf-8</encoding>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    ======配置文件========
    spring.main.allow-bean-definition-overriding=true
    spring.application.name=clouddemo
    spring.resources.add-mappings=true
    spring.cloud.consul.host=localhost
    spring.cloud.consul.port=8500
    spring.cloud.consul.config.enabled=true
    spring.cloud.consul.discovery.prefer-ip-address=true
    spring.cloud.consul.discovery.heartbeat.enabled=true
    spring.cloud.consul.discovery.service-name=clouddemo
    server.port=0
    
    二:完整简单父子工程实例:
    2.1搭建父工程
    image.png image.png
    image.png
    2.2创建好后,删除src目录:
    image.png

    父工程pom文件如下:

    <?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.king</groupId>
        <artifactId>world_cloud_parent</artifactId>
        <version>1.0</version>
        <packaging>pom</packaging>
        <description>world_cloud_parent</description>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.8.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        <properties>
            <java.version>11</java.version>
            <spring-cloud.version>Finchley.SR2</spring-cloud.version>
        </properties>
        <!-- spring-cloud所有项目依赖包 -->
        <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>
               <!-- <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>-->
            </dependencies>
        </dependencyManagement>
    </project>
    
    2.2 子工程:
    image.png
    image.png
    image.png

    下一步选择初始化加载的一些引入,先什么都不选,后面自己加。接着在下一步,


    image.png
    至此:搭建完毕,子工程代码如下:
    ========main=======
    package com.king;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class WorldCloudAdminApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(WorldCloudAdminApplication.class, args);
        }
    
    }
    
    =========pom文件==========
    <?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>com.king</groupId>
            <artifactId>world_cloud_parent</artifactId>
            <version>1.0</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.king</groupId>
        <artifactId>world_cloud_admin</artifactId>
        <packaging>jar</packaging>
        <name>world_cloud_admin</name>
        <version>1.0</version>
        <description>Demo project for Spring Boot</description>
        <dependencies>
            <!-- SpringBoot整合Web组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--SpringCloud consul-server -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <encoding>utf-8</encoding>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <repositories>
            <repository>
                <id>aliyunmaven</id>
                <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            </repository>
        </repositories>
    </project>
    
    =========配置文件==========
    spring.main.allow-bean-definition-overriding=true
    spring.application.name=cloudadmin
    spring.resources.add-mappings=true
    spring.cloud.consul.host=localhost
    spring.cloud.consul.port=8500
    #spring.cloud.consul.config.data-key=data
    spring.cloud.consul.config.enabled=true
    spring.cloud.consul.discovery.prefer-ip-address=true
    spring.cloud.consul.discovery.instance-id=${spring.application.name}-${spring.cloud.client.ip-address}-${random.int[10000,99999]}
    spring.cloud.consul.discovery.heartbeat.enabled=true
    #spring.cloud.consul.discovery.register-health-check=true
    spring.cloud.consul.discovery.service-name=cloudadmin
    server.port=0
    
    三:完毕,运行查看效果。
    image.png

    相关文章

      网友评论

          本文标题:springboot+sprcloud+consul父子工程模式

          本文链接:https://www.haomeiwen.com/subject/xeseuktx.html