大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 手把手教你SpringCloud項目搭建

手把手教你SpringCloud項目搭建

更新時間:2021-12-28 11:55:05 來源:動力節點 瀏覽2662次

手把手教你搭建spring cloud項目

IDE開發工具:IntelliJ IDEA 14.0.2

版本管理:Maven

技術棧:spring cloud

環境:JDK 1.8

1.創建Maven項目

(1)文件->新建項目->maven,如圖:

文件->新建項目

(2)填寫模塊名稱和項目路徑

按照以下步驟創建 Maven 項目。

這時候的項目不是spring boot項目!!

2.將maven項目轉為spring boot項目

(1)pom.xml 引入所需的jar包

注:根據各項目實際情況,建筑業主為maven私人倉庫

介紹SpringBoot所需的jar包

引入spring cloud需要的jar包

引入ereka服務注冊發現客戶端需要的jar包

介紹mybatis spring cloud 依賴的jar包

介紹kafka需要的jar包

引入redis需要的jar包

配置中心Spring config client的引入依賴jar包等,根據各個項目的需要。

<?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.**inks.e**s</groupId>
    <artifactId>msg</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging> 
    <name>msg</name>
    <description>Demo project for Spring Boot</description> 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent> 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <tomcat.version>8.5.30</tomcat.version>
    </properties> 
    <dependencies>
        <!--spring-cloud-config introduce -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--spring-cloud Monitoring function is introduced, which can be used to refresh configuration files dynamically -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- introduce feign rely on -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- kafka rely on -->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <!-- redis rely on -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--mybatis to configure-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.epaylinks.efps</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.epaylinks.efps</groupId>
            <artifactId>logtracer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency> 
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpasyncclient</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.2.2</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR2</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>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>src/main/resources/generatorConfig-mch.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc6</artifactId>
                        <version>11.1.0.7.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

(2)創建spring cloud項目的主入口類** Application

package com.**s.**s;
import com.**ks.e**.common.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate; 
@RefreshScope
@EnableEurekaClient
@EnableFeignClients
@EnableKafka
@EnableScheduling
@EnableAspectJAutoProxy(proxyTargetClass=true , exposeProxy=true)
@SpringBootApplication
public class MsgApplication {
    // Note that since we inject RestTemplate into the controller, we need to instantiate an instance of this class at startup
    @Autowired
    private RestTemplateBuilder builder; 
        // Use RestTemplateBuilder to instantiate the RestTemplate object. spring has injected the RestTemplateBuilder instance by default
    @Bean
    public RestTemplate restTemplate() {
        return builder.build();
    } 
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MsgApplication.class);
        SpringContextUtils.setApplicationContext(springApplication.run(args));
    }
}

@SpringBootApplication的注解說明這個項目是一個SpringBoot項目,這個類是項目的主入口類。

使用以下主要方法啟動應用程序

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MsgApplication.class);
        SpringContextUtils.setApplicationContext(springApplication.run(args));
    }

至此,spring boot項目已經完成了大半。接下來是配置數據庫連接和registry config、ereka等配置文件

3.Registry和服務發現配置

(1)讀取Spring Config Center配置項目的配置文件

樓主使用Spring Config Center作為配置中心,配置讀取項目所需的各種連接和配置信息(Spring Config Center這里不再詳細介紹)

(2)GitLab遠程托管配置信息

Spring Config Center配置項目的連接和讀取權限后,在gitlab上配置項目的各種需要的信息

在此處查看另一篇文章

spring cloud項目使用gitLab作為配置中心

(3)加載項目中各個環境對應的配置文件信息(dev、test、prod、uat)

以dev開發環境為例

按圖標順序加載

所有spring cloud項目都是從bootstrap.yml文件開始加載項目所需的各種連接和配置信息。這是spring cloud core自帶的決定。可以研究源碼,這里不詳述。

4.bootstrap-dev.yml的配置如下:

spring:
  application:
    name: msg
  cloud:
    config:
      uri: http://172.20 *. 4 *. 80:9000 / ? configure the url of the spring cloud config server
      profile: dev                      # Specify profile
      label: master                     # Specify the branch of the gitlab repository

主要是連接spring cloud config server獲取遠程gitlab上的配置信息。

5.application-dev.yml的配置如下:

eureka:
  client:
    registerWithEureka: true
    service-url:
      defaultZone: http://172.20.4.80:8000/eureka/
swagger:
  enable: true

主要用于連接eureka服務注冊和發現

至此,春關工程已基本完成。下一步就是為各種數據庫建表,配置Mybatis,更新代碼。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 日本在线 | 中文 | 曰韩毛片 | 好好的曰com久久 | 啪啪免费网站入口链接 | 狠狠综合久久久久综合小说网 | 日韩亚射吧| 高清国产美女一级毛片 | 亚洲精品久久久久久久777 | 91一区二区在线观看精品 | 不卡无毒免费毛片视频观看 | 国产精品亚洲欧美日韩一区在线 | 亚洲日本va中文字幕婷婷 | 久久伊人热 | 亚洲精品不卡午夜精品 | 一本到视频在线观看 | 精品国产91久久久久 | 日韩a免费| 国产成人a大片大片在线播放 | 九九综合视频 | 纯欧美一级毛片免费 | 国产深夜福利视频在线观看 | 狠狠骑| 羞羞视频网站在线观看 | 老司机午夜在线视频免费 | 真人特级毛片免费视频 | 九九黄色大片 | 亚洲高清视频在线 | 成人国产一区二区三区 | 久久黄色网 | 日本老熟妇激情毛片 | 在线观看中文字幕第一页 | 久草视频播放 | 亚洲国产精品综合久久网络 | 性xxx免费视频 | 亚洲高清视频在线播放 | 亚洲欧美另类在线观看 | 亚洲综合色在线观看 | 亚洲精品国产福利在线观看 | 99精品国产免费久久国语 | 热久久免费视频 | 东京干手机福利视频 |