SpringCloud入門

SpringCloud講解

標(biāo)簽: springcloud api


springcloud 講解

  • 基本內(nèi)容

配置管理,服務(wù)注冊(cè),服務(wù)發(fā)現(xiàn),斷路器,智能路由,負(fù)載均衡,微代理,服務(wù)間調(diào)用,一次性令牌,控制總線,思維導(dǎo)圖,全局鎖,領(lǐng)導(dǎo)選舉,集群狀態(tài),分布式會(huì)話,分布式消息。

  • springboot與springcloud區(qū)別

spingboot 2.0.3.RELEASE->springcloud Finchley.RC2,版本命名規(guī)則:倫敦地鐵站命名(目前基于springboot 2.0.3 )maven基于3.3.9+

springcloud 配置入門

springcloud官網(wǎng)

微服務(wù)啟動(dòng).png
eureka.png
redis.png
天氣界面.png

服務(wù)注冊(cè)與發(fā)現(xiàn)

  • eureka server

注冊(cè)服務(wù)器搭建,本注冊(cè)服務(wù)搭建采用maven3.3.9版本,添加注冊(cè)服務(wù)器依賴。并配置相應(yīng)參數(shù)(yml文件)。并綁定注冊(cè)啟動(dòng)類。

<?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.xiaojinzi</groupId>
    <artifactId>misco_weather_eureka_demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>misco_weather_eureka_demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.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>
        <spring-cloud.version>Finchley.RC2</spring-cloud.version>
    </properties>

    <dependencies>

        <!--eureka server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <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>
    </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>

server:
  port: 8761 # 服務(wù)端口

eureka:
  instance:
    hostname: localhost # 設(shè)置主機(jī)名
  client:
    registerWithEureka: false # 是否向自己注冊(cè)服務(wù)。該應(yīng)用為服務(wù)注冊(cè)中心,不需要自注冊(cè),設(shè)置為 false
    fetchRegistry: false # 是否檢索服務(wù)。該應(yīng)用為服務(wù)注冊(cè)中心,職責(zé)為注冊(cè)和發(fā)現(xiàn)服務(wù),無需檢索服務(wù),設(shè)置為 false
    serviceUrl:
      deafultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 設(shè)置同步為空時(shí)的等待時(shí)間。默認(rèn) 5 * MINUTES
@SpringBootApplication
@EnableEurekaServer
public class MiscoWeatherEurekaDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MiscoWeatherEurekaDemoApplication.class, args);
    }
}
  • eureka client

注冊(cè)服務(wù)器客戶端。

<!--eureka-client-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
spring:
  application:
    name: misco_weather_client_demo
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ # 服務(wù)注冊(cè)地址
@SpringBootApplication
@EnableDiscoveryClient
public class MiscoWeatherClientDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MiscoWeatherClientDemoApplication.class, args);
    }
}
  • 實(shí)現(xiàn)服務(wù)的注冊(cè)與發(fā)現(xiàn)

天氣系統(tǒng)中的四個(gè)服務(wù)項(xiàng)目修改,通過將每個(gè)服務(wù)中添加客戶端的配置。從而實(shí)現(xiàn)注冊(cè)。這里只以修改數(shù)據(jù)收集為案例。

<?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.xiaojinzi</groupId>
    <artifactId>misco_weather_data</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>misco_weather_data</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.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>
        <spring-cloud.version>Finchley.RC2</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--請(qǐng)求發(fā)送-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <!--定時(shí)器-->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
        </dependency>
    </dependencies>

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

    <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>

    <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>

server:
  port: 8090
spring:
  application:
    name: misco_weather_data_client
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
@EnableDiscoveryClient

服務(wù)消費(fèi)

  • httpclient

參考數(shù)據(jù)獲取api調(diào)用方式。

  • ribbon

客戶端模式的負(fù)載均衡。

  • feign

客戶端模式的負(fù)載均衡。案例通過feign的方式獲取城市列表信息。啟動(dòng)注冊(cè)服務(wù)器和城市信息獲取的服務(wù)注冊(cè),添加一個(gè)通過feign調(diào)用城市列表的服務(wù)。

依賴

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

調(diào)用方式

@FeignClient("misco-city-data-client")
@Service
public interface CityListService {
    @GetMapping("/city/list")
    String cityList();
}
  • 基于feign實(shí)現(xiàn)天氣服務(wù)之間的相互通信。其中包括天氣數(shù)據(jù)采集城市數(shù)據(jù)來源與城市數(shù)據(jù)微服務(wù),其中城市數(shù)據(jù)展示api獲取來源于城市采集數(shù)據(jù)微服務(wù)。

GitHub項(xiàng)目地址

  • 負(fù)載均衡

服務(wù)端的負(fù)載均衡實(shí)現(xiàn)。eureka會(huì)采用系統(tǒng)的負(fù)載均衡算法實(shí)現(xiàn)負(fù)載均衡,我們可以將同一個(gè)服務(wù)已不同端口的方式運(yùn)行,當(dāng)某一個(gè)宕機(jī)的時(shí)候,系統(tǒng)會(huì)實(shí)現(xiàn)負(fù)載均衡調(diào)用。

API網(wǎng)關(guān)

統(tǒng)一API入口,數(shù)據(jù)安全。

  • 優(yōu)點(diǎn)

[1] 避免內(nèi)部信息泄露

[2] 為微服務(wù)提供添加額外的安全層

[3] 支持混合通信

[4] 降低構(gòu)建微服務(wù)的復(fù)雜性

[5] 微服務(wù)虛擬與虛擬化

  • 缺點(diǎn)

[1] 架構(gòu)編排與管理

[2] 路由配置統(tǒng)一管理

[3] 引發(fā)單點(diǎn)故障

  • 實(shí)現(xiàn)方式

[1] nginx 方向代理

[2] spring cloud zuul

zuul集成

  • 功能

認(rèn)證、壓力測試、金絲雀測試、動(dòng)態(tài)路由、負(fù)載削減、安全、靜態(tài)響應(yīng)處理、主動(dòng)交換管理。

  • 基本參數(shù)
zuul:
    routes: 
        hi(不固定):
            path: /**/**
            serviceId: 應(yīng)用名稱
  • 結(jié)合天氣項(xiàng)目實(shí)現(xiàn)

配置API網(wǎng)關(guān)。提供zuul來進(jìn)行網(wǎng)關(guān)。

GitHub地址

集中化配置

springcloud config

  • 服務(wù)端配置
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
spring.cloud.config.server.git.uri = ****(配置文件服務(wù)器地址)
spring.cloud.config.server.git.searchPaths = ***(配置文件目錄)
  • 客戶端配置
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>
spring.cloud.config.profile=文件
spring.cloud.config.uri=配置中心地址
  • 服務(wù)器配置文件命名規(guī)范

官方說明

服務(wù)熔斷

表示對(duì)服務(wù)進(jìn)行熔斷機(jī)制。通過設(shè)置可能存在熔斷的服務(wù),并當(dāng)出現(xiàn)斷路時(shí),提供熔斷回調(diào)內(nèi)容。

  • 依賴
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
  • 啟動(dòng)類注解
@EnableCirculBreaker
  • 出現(xiàn)熔斷的回調(diào)
@HystixCommand(fallbackMethod="defaultCities")
  • 聯(lián)合feign使用

通過設(shè)置回調(diào)類,并實(shí)現(xiàn)接口,通過設(shè)置回調(diào)的內(nèi)容。啟動(dòng)feign熔斷機(jī)制。

@FeignClient(name="**",fallback="回調(diào)類")
feign:
    hustrix:
        enabled: true

微服務(wù)自動(dòng)擴(kuò)展

優(yōu)點(diǎn):提高高可用和容錯(cuò)機(jī)制;增加可伸縮性;最佳使用率,節(jié)約成本;優(yōu)先考慮某些服務(wù)或服務(wù)組。

問題:管理容器?監(jiān)控?應(yīng)用規(guī)則與約束?容器獲取資源效率?確保最小實(shí)例運(yùn)行?確保服務(wù)正常運(yùn)行?滾動(dòng)升級(jí)與遷移?錯(cuò)誤回滾?


  • 本篇博客原視頻博主[從天氣項(xiàng)目觀看springcloud微服務(wù)智力]
  • 本篇博客撰寫人: XiaoJinZi 轉(zhuǎn)載請(qǐng)注明出處
  • 學(xué)生能力有限 附上郵箱: 986209501@qq.com 不足以及誤處請(qǐng)大佬指責(zé)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容