溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring Cloud常見模塊有哪些

發(fā)布時間:2021-11-17 13:55:22 來源:億速云 閱讀:184 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要講解了“Spring Cloud常見模塊有哪些”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Spring Cloud常見模塊有哪些”吧!

什么是Spring Cloud :  

  • Spring Cloud 是一系列框架的集合

  • 利用Spring Boot的簡化了開發(fā)

Spring Cloud 常見模塊 :

  • Eureka:注冊中心,用于注冊所有服務(wù)(項目/應(yīng)用)

  • Ribbon:負載均衡,用于搭建集群的。(同一個功能多個tomcat,ribbon幫著選擇一個tomcat)

  • Hystrix:熔斷器,與正主斷了聯(lián)系,使用備選方案(備胎)。

  • Feign:服務(wù)與服務(wù)之間調(diào)用。類似HttpClient

  • zuul 網(wǎng)關(guān):確定統(tǒng)一入口,方便進行管理。

Spring Cloud常見模塊有哪些

spring cloud版本 : spring cloud 采用 Greenwich版本,對應(yīng)spring boot 2.1.*版本

Eureka 入門

Eureka職責(zé):

  • 服務(wù)注冊:服務(wù)提供方將服務(wù)注冊到注冊中心

  • 服務(wù)發(fā)現(xiàn):服務(wù)調(diào)用方法,從注冊中心中,獲得需要的服務(wù)

  • 服務(wù)檢測:注冊中心與服務(wù)之間采用心跳檢測服務(wù)狀態(tài)

Eureka 入門案例

搭建父項目

  • 步驟一:創(chuàng)建父項目 cloud_parent

  • 步驟二:修改pom.xml文件,確定spring cloud版本

<!--1 確定spring boot的版本-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <!--2  確定sprig cloud版本-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <spring-cloud-release.version>Greenwich.RELEASE</spring-cloud-release.version>
    </properties>

    <!-- 3 鎖定sprig cloud版本-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-release.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 4 確定spring cloud私有倉庫-->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

搭建注冊中心

  • 步驟一:創(chuàng)建子項目 eureka_demo

  • 步驟二:修改pom.xml文件,添加web和 eureka service 依賴

  <dependencies>
        <!--web起步依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Eureka服務(wù)端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
  • 步驟三:創(chuàng)建yml文件,配置端口號、服務(wù)名、eureka注冊地址

#服務(wù)端口號
server:
  port: 10086
#服務(wù)名稱
spring:
  application:
    name: eurekaDemo
#注冊中心地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:${server.port}/eureka   #eureka服務(wù)注冊地址
    register-with-eureka: false     #關(guān)閉將自己注冊到注冊中心中
    fetch-registry: false           #關(guān)閉從注冊中心獲得列表(不拉去其他服務(wù)信息)
  • 步驟四:創(chuàng)建啟動類,添加開啟 eureka service 注解 @EnableEurekaService

package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer     //開啟eureka服務(wù)端
public class EurekaDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaDemoApplication.class ,args);
    }
}

搭建服務(wù)提供方

  • 步驟一:創(chuàng)建提供方項目,eureka_service

  • 步驟二:修改pom.xml文件,添加 web、eureka client、spring boot 監(jiān)控依賴  (監(jiān)控依賴可加可不加)

<dependencies>
        <!--web起步依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Eureka客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--spring boot監(jiān)控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
  • 步驟三:創(chuàng)建application.yml文件,配置端口號、服務(wù)名、eureka注冊中心位置

#端口號
server:
  port: 8080
#服務(wù)名稱
spring:
  application:
    name: service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  • 步驟四:編寫啟動類,添加啟動客戶端注解 @EnableEurekaClient

package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


@SpringBootApplication
@EnableEurekaClient   //開啟eureka客戶端
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class,args);
    }
}

步驟五:編寫controller , 測試程序

  • 測試路徑:http://localhost:8080/test  

  • 顯示結(jié)果:  "測試數(shù)據(jù)"

package com.czxy.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping
    public ResponseEntity<String> test(){
        return ResponseEntity.ok("測試數(shù)據(jù)");
    }
}

搭建 服務(wù)調(diào)用方

  • 步驟一:創(chuàng)建調(diào)用方項目,eureka_client

  • 步驟二:修改pom.xml文件,添加 web、eureka client、spring boot 監(jiān)控依賴(與eureka_service項目一樣)

<dependencies>
        <!--web起步依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Eureka客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--spring boot監(jiān)控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
  • 步驟三:創(chuàng)建yml文件,(與eureka_service項目相似,有不同端口和服務(wù)名)

#端口號
server:
  port: 9090
#服務(wù)名稱
spring:
  application:
    name: client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  • 步驟四:編寫啟動類,添加eureka客戶端注解,(與eureka_service項目相似,有不同類名)

package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


@SpringBootApplication
@EnableEurekaClient     //開啟eureka客戶端
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class,args);
    }
}


調(diào)用方測試數(shù)據(jù)

Spring Cloud常見模塊有哪些

  • 步驟一:編寫config配置類,用于配置RestTemplate(遠程調(diào)用)實例

package com.czxy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


@Configuration
public class HttpConfig {
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
  • 步驟二:編寫DataDao,用于進行遠程調(diào)用

package com.czxy.dao;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


@Component
public class DataDao {
    @Resource
    private RestTemplate restTemplate;

    public ResponseEntity<String> data(){ 
                                          //服務(wù)提供方地址
        return restTemplate.getForEntity("http://localhost:8080/test",String.class);
    }
}
  • 步驟三:編寫DataController,提供接口進行訪問

package com.czxy.controller;

import com.czxy.dao.DataDao;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("/data")
public class DataController {
    @Resource
    private DataDao dataDao;\

    @GetMapping
    public ResponseEntity data(){
        return dataDao.data();
    }
}
  •  測試路徑 :  http://localhost:9090/data

  • 顯示結(jié)果 "測試數(shù)據(jù)" 說明就Eureka入門案例就完成了

追加 ----> 配置eureka instance

Spring Cloud常見模塊有哪些

yml文件配置

  • instance-id : 用于配置可視化頁面中,顯示的服務(wù)名稱

    • ${spring.application.name} 獲得服務(wù)名

    • ${spring.cloud.client.ip-address} 獲得ip地址

    • ${server.port} 獲得端口號

    • 默認服務(wù)名稱:計算機名稱:服務(wù)名:端口號

    • 自定義服務(wù)名稱:

  • prefer-ip-address:用于配置可視化頁面中,訪問時是否顯示ip地址

    • 默認顯示的是:計算機名稱:端口號

eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  instance:
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true   #注冊中心可視化中顯示IP地址
  • properties文件配置(不建議),參考學(xué)習(xí)

eureka.client.service-url.defaultZone=http://localhost:10086/eureka
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
eureka.instance.prefer-ip-address=true

感謝各位的閱讀,以上就是“Spring Cloud常見模塊有哪些”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Spring Cloud常見模塊有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI