spring cloud 框架搭建

說明 : EnableDiscoveryClient跟 EnableEurekaClient 區別

用@EnableDiscoveryClient註解標明後,該微服務會被Eureka-Server掃描找到,這裏也可以用@EnableEurekaClient,

他們不同在於@EnableEurekaClient只適用於Eureka-Server發現服務,而@EnableDiscoveryClient也適用其他發現組件。
————————————————
版權聲明:本文爲CSDN博主「nanfeiliulanghan」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/nanfeiliulanghan/article/details/90423824

注意: 創建完pom文件後 父pom需要添加pom格式

 <packaging>pom</packaging>

子pom需要添加爲jar 格式

 <packaging>jar</packaging>

在這裏插入代碼片


第一步

創建父項目 pom文件引入spring clou的依賴 父pom進行版本管理


 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    #    <relativePath/> <!-- lookup parent from repository -->    必須刪除 這裏做提示
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-spring-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-spring-cloud</name>
    <description>Demo project for Spring Boot</description>

 <!--統一版本號-->
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>


  <dependencyManagement>
        <dependencies>
			<!-- spring cloud依賴-->
            <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>

<!-- 國內maven鏡像庫  公司內一般配置在maven setting文件內-->
<repositories>
        <repository>
            <id>central</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <layout>default</layout>
            <!-- 是否開啓發布版構件下載 -->
            <releases>
                <enabled>true</enabled>
            </releases>
            <!-- 是否開啓快照版構件下載 -->
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

第二步 新建項目 創建 註冊中心

pom 依賴

<parent>
        <groupId>com.example</groupId>
        <artifactId>demo-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>discorvey</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>discorvey</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <!--註冊中心依賴 eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <!--maven 啓動插件-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

啓動類配置註解
@EnableEurekaServer

yml配置

server:
  port: 8761  # 默認8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    fetch-registry: false          #
    register-with-eureka: false    # 默認不註冊到註冊中心  集羣打開
  server:
    enableSelfPreservation: false
    evictionIntervalTimerInMs: 4000 # 清理間隔(單位毫秒,默認是60*1000)
spring:
  application:
    name: center                     # 應用名

第二步 創建 gateway

pom文件

<parent>
        <groupId>com.example</groupId>
        <artifactId>demo-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>


    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
          <!--網關依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</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>


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

啓動類註解
@EnableZuulProxy

yml配置文件

server:
  port: 8180
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka   # 註冊中心地址
spring:
  application:
    name: gateway
zuul:
  routes:
     # 複雜寫法
#    customer:
#      path: /customer/**
#      serviceId: customer
    customer: /customer/**    # 簡單寫法

消費者

pom依賴

 <parent>
        <groupId>com.example</groupId>
        <artifactId>demo-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>


    <artifactId>customer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>customer</name>
    <packaging>jar</packaging>
    <description>Demo project for Spring Boot</description>

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

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</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>

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

啓動類註解

@SpringBootApplication
@EnableFeignClients("com.example.common.service")
public class CustomerApplication {

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

}

yml配置

server:
  port: 8181
  servlet:
    context-path: /customer
spring:
  application:
    name: customer

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka   # 註冊中心地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章