1. 程式人生 > >Spring Cloud Config 實現配置中心,看這一篇就夠了

Spring Cloud Config 實現配置中心,看這一篇就夠了

Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,雖然後來又釋出了 Consul 可以代替配置中心功能,但是 Config 依然適用於 Spring Cloud 專案,通過簡單的配置即可實現功能。

配置檔案是我們再熟悉不過的了,尤其是 Spring Boot 專案,除了引入相應的 maven 包之外,剩下的工作就是完善配置檔案了,例如 mysql、redis 、security 相關的配置。除了專案執行的基礎配置之外,還有一些配置是與我們業務有關係的,比如說七牛儲存、簡訊相關、郵件相關,或者一些業務上的開關。

對於一些簡單的專案來說,我們一般都是直接把相關配置放在單獨的配置檔案中,以 properties 或者 yml 的格式出現,更省事兒的方式是直接放到 application.properties 或 application.yml 中。但是這樣的方式有個明顯的問題,那就是,當修改了配置之後,必須重啟服務,否則配置無法生效。

目前有一些用的比較多的開源的配置中心,比如攜程的 Apollo、螞蟻金服的 disconf 等,對比 Spring Cloud Config,這些配置中心功能更加強大。有興趣的可以拿來試一試。

接下來,我們開始在 Spring Boot 專案中整合 Spring Cloud Config,並以 github 作為配置儲存。除了 git 外,還可以用資料庫、svn、本地檔案等作為儲存。主要從以下三塊來說一下 Config 的使用。

1.基礎版的配置中心(不整合 Eureka);

2.結合 Eureka 版的配置中心;

3.實現配置的自動重新整理;

實現最簡單的配置中心

最簡單的配置中心,就是啟動一個服務作為服務方,之後各個需要獲取配置的服務作為客戶端來這個服務方獲取配置。

先在 github 中建立配置檔案

我建立的倉庫地址為:配置中心倉庫

目錄結構如下:

配置檔案的內容大致如下,用於區分,略有不同。

data:
  env: config-eureka-dev
  user:
    username: eureka-client-user
    password: 1291029102

注意檔案的名稱不是亂起的,例如上面的 config-single-client-dev.yml 和 config-single-client-prod.yml 這兩個是同一個專案的不同版本,專案名稱為 config-single-client, 一個對應開發版,一個對應正式版。config-eureka-client-dev.yml 和 config-eureka-client-prod.yml 則是另外一個專案的,專案的名稱就是 config-eureka-client 。

建立配置中心服務端

1、新建 Spring Boot 專案,引入 config-server 和 starter-web

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

<!-- spring cloud config 服務端包 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

2、配置 config 相關的配置項

bootstrap.yml 檔案

spring:
  application:
    name: config-single-server  # 應用名稱
  cloud:
     config:
        server:
          git:
            uri: https://github.com/huzhicheng/config-only-a-demo #配置檔案所在倉庫
            username: github 登入賬號
            password: github 登入密碼
            default-label: master #配置檔案分支
            search-paths: config  #配置檔案所在根目錄

application.yml

server:
  port: 3301

3、在 Application 啟動類上增加相關注解 @EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

啟動服務,接下來測試一下。

Spring Cloud Config 有它的一套訪問規則,我們通過這套規則在瀏覽器上直接訪問就可以。

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

{application} 就是應用名稱,對應到配置檔案上來,就是配置檔案的名稱部分,例如我上面建立的配置檔案。

{profile} 就是配置檔案的版本,我們的專案有開發版本、測試環境版本、生產環境版本,對應到配置檔案上來就是以 application-{profile}.yml 加以區分,例如application-dev.yml、application-sit.yml、application-prod.yml。

{label} 表示 git 分支,預設是 master 分支,如果專案是以分支做區分也是可以的,那就可以通過不同的 label 來控制訪問不同的配置檔案了。

上面的 5 條規則中,我們只看前三條,因為我這裡的配置檔案都是 yml 格式的。根據這三條規則,我們可以通過以下地址檢視配置檔案內容:

http://localhost:3301/config-single-client/dev/master

http://localhost:3301/config-single-client/prod

http://localhost:3301/config-single-client-dev.yml

http://localhost:3301/config-single-client-prod.yml

http://localhost:3301/master/config-single-client-prod.yml

通過訪問以上地址,如果可以正常返回資料,則說明配置中心服務端一切正常。

建立配置中心客戶端,使用配置

配置中心服務端好了,配置資料準備好了,接下來,就要在我們的專案中使用它了。

1、引用相關的 maven 包。

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

<!-- spring cloud config 客戶端包 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

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

2、初始化配置檔案

bootstrap.yml

spring:
  profiles:
    active: dev

---
spring:
  profiles: prod
  application:
    name: config-single-client
  cloud:
     config:
       uri: http://localhost:3301
       label: master
       profile: prod


---
spring:
  profiles: dev
  application:
    name: config-single-client
  cloud:
     config:
       uri: http://localhost:3301
       label: master
       profile: dev

配置了兩個版本的配置,並通過 spring.profiles.active 設定當前使用的版本,例如本例中使用的 dev 版本。

application.yml

server:
  port: 3302
management:
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"

data:
  env: NaN
  user:
    username: NaN
    password: NaN

其中 management 是關於 actuator 相關的,接下來自動重新整理配置的時候需要使用。

data 部分是當無法讀取配置中心的配置時,使用此配置,以免專案無法啟動。

3、要讀取配置中心的內容,需要增加相關的配置類,Spring Cloud Config 讀取配置中心內容的方式和讀取本地配置檔案中的配置是一模一樣的。可以通過 @Value 或 @ConfigurationProperties 來獲取。

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

4、要讀取配置中心的內容,需要增加相關的配置類,Spring Cloud Config 讀取配置中心內容的方式和讀取本地配置檔案中的配置是一模一樣的。可以通過 @Value 或 @ConfigurationProperties 來獲取。

使用 @Value 的方式:

@Data
@Component
public class GitConfig {

    @Value("${data.env}")
    private String env;

    @Value("${data.user.username}")
    private String username;

    @Value("${data.user.password}")
    private String password;

}

使用 @ConfigurationProperties 的方式:

@Component
@Data
@ConfigurationProperties(prefix = "data")
public class GitAutoRefreshConfig {

    public static class UserInfo {
        private String username;

        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "UserInfo{" +
                    "username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }

    private String env;

    private UserInfo user;
}

4、增加一個 RESTController 來測試使用配置

@RestController
public class GitController {

    @Autowired
    private GitConfig gitConfig;

    @Autowired
    private GitAutoRefreshConfig gitAutoRefreshConfig;

    @GetMapping(value = "show")
    public Object show(){
        return gitConfig;
    }

    @GetMapping(value = "autoShow")
    public Object autoShow(){
        return gitAutoRefreshConfig;
    }
}

5、專案啟動類

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

啟動專案,訪問 RESTful 介面

http://localhost:3302/show,結果如下:

{
  "env": "localhost-dev-edit",
  "username": "fengzheng-dev",
  "password": "password-dev"
}

http://localhost:3302/autoShow,結果如下:

{
  "env": "localhost-dev-edit",
  "user": {
      "username": "fengzheng-dev",
      "password": "password-dev"
    }
}

實現自動重新整理

Spring Cloud Config 在專案啟動時載入配置內容這一機制,導致了它存在一個缺陷,修改配置檔案內容後,不會自動重新整理。例如我們上面的專案,當服務已經啟動的時候,去修改 github 上的配置檔案內容,這時候,再次重新整理頁面,對不起,還是舊的配置內容,新內容不會主動重新整理過來。
但是,總不能每次修改了配置後重啟服務吧。如果是那樣的話,還是不要用它了為好,直接用本地配置檔案豈不是更快。

它提供了一個重新整理機制,但是需要我們主動觸發。那就是 @RefreshScope 註解並結合 actuator ,注意要引入 spring-boot-starter-actuator 包。

1、在 config client 端配置中增加 actuator 配置,上面大家可能就注意到了。

management:
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"

其實這裡主要用到的是 refresh 這個介面

2、在需要讀取配置的類上增加 @RefreshScope 註解,我們是 controller 中使用配置,所以加在 controller 中。

@RestController
@RefreshScope
public class GitController {

    @Autowired
    private GitConfig gitConfig;

    @Autowired
    private GitAutoRefreshConfig gitAutoRefreshConfig;

    @GetMapping(value = "show")
    public Object show(){
        return gitConfig;
    }

    @GetMapping(value = "autoShow")
    public Object autoShow(){
        return gitAutoRefreshConfig;
    }
}

注意,以上都是在 client 端做的修改。

之後,重啟 client 端,重啟後,我們修改 github 上的配置檔案內容,並提交更改,再次重新整理頁面,沒有反應。沒有問題。

接下來,我們傳送 POST 請求到 http://localhost:3302/actuator/refresh 這個介面,用 postman 之類的工具即可,此介面就是用來觸發載入新配置的,返回內容如下:

[
    "config.client.version",
    "data.env"
]

之後,再次訪問 RESTful 介面,http://localhost:3302/autoShow 這個介面獲取的資料已經是最新的了,說明 refresh 機制起作用了。

而 http://localhost:3302/show 獲取的還是舊資料,這與 @Value 註解的實現有關,所以,我們在專案中就不要使用這種方式載入配置了。

在 github 中配置 Webhook

這就結束了嗎,並沒有,總不能每次改了配置後,就用 postman 訪問一下 refresh 介面吧,還是不夠方便呀。 github 提供了一種 webhook 的方式,當有程式碼變更的時候,會呼叫我們設定的地址,來實現我們想達到的目的。

1、進入 github 倉庫配置頁面,選擇 Webhooks ,並點選 add webhook;

2、之後填上回調的地址,也就是上面提到的 actuator/refresh 這個地址,但是必須保證這個地址是可以被 github 訪問到的。如果是內網就沒辦法了。這也僅僅是個演示,一般公司內的專案都會有自己的程式碼管理工具,例如自建的 gitlab,gitlab 也有 webhook 的功能,這樣就可以呼叫到內網的地址了。

使用 Spring Cloud Bus 來自動重新整理多個端

Spring Cloud Bus 將分散式系統的節點與輕量級訊息代理連結。這可以用於廣播狀態更改(例如配置更改)或其他管理指令。一個關鍵的想法是,Bus 就像一個擴充套件的 Spring Boot 應用程式的分散式執行器,但也可以用作應用程式之間的通訊渠道。

—— Spring Cloud Bus 官方解釋

如果只有一個 client 端的話,那我們用 webhook ,設定手動重新整理都不算太費事,但是如果端比較多的話呢,一個一個去手動重新整理未免有點複雜。這樣的話,我們可以藉助 Spring Cloud Bus 的廣播功能,讓 client 端都訂閱配置更新事件,當配置更新時,觸發其中一個端的更新事件,Spring Cloud Bus 就把此事件廣播到其他訂閱端,以此來達到批量更新。

1、Spring Cloud Bus 核心原理其實就是利用訊息佇列做廣播,所以要先有個訊息佇列,目前官方支援 RabbitMQ 和 kafka。

這裡用的是 RabbitMQ, 所以先要搭一套 RabbitMQ 環境。請自行準備環境,這裡不再贅述。我是用 docker 直接建立的,然後安裝了 rabbitmq-management 外掛,這樣就可以在瀏覽器訪問 15672 檢視 UI 管理介面了。

2、在 client 端增加相關的包,注意,只在 client 端引入就可以。

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

3、在配置檔案中增加 RabbitMQ 相關配置,預設的埠應該是 5672 ,因為我是用 docker 建立的,所以有所不同。

spring:
  rabbitmq:
    host: localhost
    port: 32775
    username: guest
    password: guest

4、啟動兩個或多個 client 端,準備來做個測試

在啟動的時候分別加上 vm option:-Dserver.port=3302 和 -Dserver.port=3303 ,然後分別啟動就可以了。

5、分別開啟 http://localhost:3302/autoShow 和 http://localhost:3303/autoShow,檢視內容,然後修改 github 上配置檔案的內容並提交。再次訪問這兩個地址,資料沒有變化。

6、訪問其中一個的 actuator/bus-refresh 地址,注意還是要用 POST 方式訪問。之後檢視控制檯輸出,會看到這兩個端都有一條這樣的日誌輸出

o.s.cloud.bus.event.RefreshListener: Received remote refresh request. Keys refreshed

7、再次訪問第 5 步的兩個地址,會看到內容都已經更新為修改後的資料了。

綜上所述,當我們修改配置後,使用 webhook ,或者手動觸發的方式 POST 請求一個 client 端的 actuator/bus-refresh 介面,就可以更新給所有端了。

結合 Eureka 使用 Spring Cloud Config

以上講了 Spring Cloud Config 最基礎的用法,但是如果我們的系統中使用了 Eureka 作為服務註冊發現中心,那麼 Spring Cloud Config 也應該註冊到 Eureka 之上,方便其他服務消費者使用,並且可以註冊多個配置中心服務端,以實現高可用。

好的,接下來就來整合 Spring Cloud Config 到 Eureka 上。

在 github 倉庫中增加配置檔案

啟動 Eureka Server

首先啟動一個 Eureka Server,之前的文章有講過 Eureka ,可以回過頭去看看。Spring Cloud Eureka 實現服務註冊發現,為了清楚,這裡還是把配置列出來

1、pom 中引入相關包

<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-security</artifactId>
</dependency>

2、設定配置檔案內容

bootstrap.yml

spring:
  application:
    name: kite-eureka-center
  security:
      user:
        name: test  # 使用者名稱
        password: 123456   # 密碼
  cloud:
    inetutils: ## 網絡卡設定
      ignoredInterfaces:  ## 忽略的網絡卡
        - docker0
        - veth.*
        - VM.*
      preferredNetworks:  ## 優先的網段
        - 192.168

application.yml

server:
  port: 3000
eureka:
  instance:
    hostname: eureka-center
    appname: 註冊中心
  client:
    registerWithEureka: false # 單點的時候設定為 false 禁止註冊自身
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://test:123456@localhost:3000/eureka
  server:
    enableSelfPreservation: false
    evictionIntervalTimerInMs: 4000

3、Application 啟動類

@EnableEurekaServer
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

4、啟動服務,在瀏覽器訪問 3000 埠,並輸出使用者名稱 test,密碼 123456 即可進入 Eureka UI

配置 Spring Cloud Config 服務端

服務端和前面的相比也就是多了註冊到 Eureka 的配置,其他地方都是一樣的。

1、在 pom 中引入相關的包

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

<!-- spring cloud config 服務端包 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

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

2、配置檔案做配置

application.yml

server:
  port: 3012
eureka:
  client:
    serviceUrl:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://test:123456@localhost:3000/eureka/
  instance:
    preferIpAddress: true
spring:
  application:
    name: config-eureka-server
  cloud:
     config:
        server:
          git:
            uri: https://github.com/huzhicheng/config-only-a-demo
            username: github 使用者名稱
            password: github 密碼
            default-label: master
            search-paths: config

相比於不加 Eureka 的版本,這裡僅僅是增加了 Eureka 的配置,將配置中心註冊到 Eureka ,作為服務提供者對外提供服務。

3、啟動類,在 @EnableConfigServer 的基礎上增加了 @EnableEurekaClient,另外也可以用 @EnableDiscoveryClient 代替 @EnableEurekaClient

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

4、啟動服務,之後訪問 Eureka ,可以看到服務已註冊成功

配置 Spring Cloud Config 客戶端

客戶端的配置相對來說變動大一點,加入了 Eureka 之後,就不用再直接和配置中心服務端打交道了,要通過 Eureka 來訪問。另外,還是要注意客戶端的 application 名稱要和 github 中配置檔案的名稱一致。

1、pom 中引入相關的包

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

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

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

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

2、配置檔案

bootstrap.yml

eureka:
  client:
    serviceUrl:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://test:123456@localhost:3000/eureka/
  instance:
    preferIpAddress: true


spring:
  profiles:
    active: dev

---
spring:
  profiles: prod
  application:
    name: config-eureka-client
  cloud:
     config:
       label: master
       profile: prod
       discovery:
         enabled: true
         service-id: config-eureka-server


---
spring:
  profiles: dev
  application:
    name: config-eureka-client
  cloud:
     config:
       label: master  #指定倉庫分支
       profile: dev   #指定版本 本例中建立了dev 和 prod兩個版本
       discovery:
          enabled: true  # 開啟
          service-id: config-eureka-server # 指定配置中心服務端的server-id 

除了註冊到 Eureka 的配置外,就是配置和配置中心服務端建立關係。

其中 service-id 也就是服務端的application name。

application.yml

server:
  port: 3011
management:
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"

data:
  env: NaN
  user:
    username: NaN
    password: NaN

3、啟動類,增加了 @EnableEurekaClient 註解,可以用 @EnableDiscoveryClient 代替

@SpringBootApplication
@EnableEurekaClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

4、另外的配置實體類和 RESTController 和上面的一樣,沒有任何更改,直接參考即可。

5、啟動 client 端,訪問 http://localhost:3011/autoShow,即可看到配置檔案內容。

這個例子只是介紹了和 Eureka 結合的最基礎的情況,還可以註冊到高可用的 Eureka 註冊中心,另外,配置中心服務端還可以註冊多個例項,同時保證註冊中心的高可用。

注意事項

1. 在 git 上的配置檔案的名字要和 config 的 client 端的 application name 對應;

2. 在結合 eureka 的場景中,關於 eureka 和 git config 相關的配置要放在 bootstrap.yml 中,否則會請求預設的 config server 配置,這是因為當你加了配置中心,服務就要先去配置中心獲取配置,而這個時候,application.yml 配置檔案還沒有開始載入,而 bootstrap.yml 是最先載入的。

如果你覺得寫的還可以的話,請點個「推薦」吧

歡迎關注,不定期更新本系列和其他文章
古時的風箏 ,進入公眾號可以加入交流群

相關推薦

Spring Cloud Config 實現配置中心

Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,雖然後來又釋出了 Consul 可以代替配置中心功能,但是 Config 依然適用於 Spring Cloud 專案,通過簡單的配置即可實現功能。 配置檔案是我們再熟悉不過的了,尤其是 Spring Boot 專案

Hadoop2.9+Hive3.0+Mysql8.0安裝配置

由於以上均是最新版本,網上以往的資料用起來幾乎都有多少的錯誤,不少的錯誤真的會把人搞瘋,近10篇博文結合我自己在外網上找到的一些方法,記錄下來。 hadoop的安裝推薦看這一篇:http://dblab.xmu.edu.cn/blog/install-hado

如何低成本實現Flutter富文字

作者:閒魚技術-玄川 背景 閒魚是國內最早使用Flutter 的團隊,作為一個電商App商品詳情頁是非常重要場景,其中最主要

利用COM元件實現對WORD書籤各種操作大全

  有個需求是,程式匯出一份word報告,報告中有各種各樣的表格,匯出時還需要插入圖片。   腦海中迅速閃過好幾種元件,openxml元件,com元件,npoi。為了減少程式畫複雜表格,我們選用了com元件+word模板的方式,程式只需要對word中的書籤進行賦值即可。   不知道這幾種元件的(或者還有其他寫

Azure IOT 設備固件更新技巧

trigger 物聯網平臺 搭建 href ice 有效 面板 調用 創建 嫌長不看版 今天為大家準備的硬菜是:在 Azure IoT 中心創建 Node.js 控制臺應用,進行端到端模擬固件更新,為基於 Intel Edison 的設備安裝新版固件的流程。通過創建模擬設備

想做好PPT折線圖

12月 image 菊花 -c 強調 spa any border 線圖 配圖主題無關今天鄭少跟大家聊聊折線圖的使用方法,或者你有疑問,折線圖很簡單,插入修改數據不就好了嗎?如果你要是這樣想的,恭喜你,有可能你會做出下面這樣的效果。如果你要是稍微懂一點折線圖的使用方法,你就

Linux 問題故障定位

1. 背景 有時候會遇到一些疑難雜症,並且監控外掛並不能一眼立馬發現問題的根源。這時候就需要登入伺服器進一步深入分析問題的根源。那麼分析問題需要有一定的技術經驗積累,並且有些問題涉及到的領域非常廣,才能定位到問題。所以,分析問題和踩坑是非常鍛鍊一個人的成長和提升自我能力。如果我們有一套好的分析工具,那將是事

C語言從入門到精通

影響 內容 當前 位置 replace 雙精度 下標 寄存器變量 一個 No.1 計算機與程序設計語言的關系 計算機系統由硬件系統和軟件系統構成,硬件相當於人類的肉體,而軟件相當於人類的靈魂,如果脫離了靈魂,人類就是一具行屍走肉 No.2 C語言的特點 代碼簡潔,靈活性高

【MYSQL學習筆記02】MySQL的高階應用之Explain(完美詳細版

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/wx1528159409 最近學習MySQL的高階應用Explain,寫一篇學習心得與總結,目錄腦圖如下: 一、Explain基本概念 1. Explain定義 · 我們知道M

抖音內容運營全解剖 !

抖音的火爆已經不用多說,作為短視訊的頭部APP,抖音已經從微信手中奪走不少使用者時間,成為新的“時間黑洞”。 比如:“中毒了,我每天晚上要刷2個小時”,“下一站,逃離微信,上抖音”… 一個企業運營抖音的目的是什麼? 答案顯而易見,無非就是做品牌營銷、擴大品牌影響力。 在短視訊領域積累

百萬併發下的Nginx優化

本文作者主要分享在 Nginx 效能方面的實踐經驗,希望能給大家帶來一些系統化思考,幫助大家更有效地去做 Nginx。 優化方法論 我重點分享如下兩個問題: 保持併發連線數,怎麼樣做到記憶體有效使用。 在高併發的同時保持高吞吐量的重要要點。 實現層面主要是三方面優化,主要聚焦

理解Sharding jdbc原理

相比於Spring基於AbstractRoutingDataSource實現的分庫分表功能,Sharding jdbc在單庫單表擴充套件到多庫多表時,相容性方面表現的更好一點。例如,spring實現的分庫分表sql寫法如下: select id, name, price,

產品設計教程:如何理解 px,dp,dpi, pt

先聊聊熟悉的幾個單位 圍繞著各種螢幕做設計和開發的人會碰到下面幾個單位:in, pt, px, dpi,dip/dp, sp 下面先簡單回顧下前四個單位: "in" inches的縮寫,英寸。就是螢幕的物理長度單位。一英寸等於2.54cm。比如Android手機

中後臺產品的表格設計(原型規範下載)

中後臺產品的表格設計,看這一篇就夠了(原型規範下載) 2018年4月16日luodonggan 中後臺產品的表格設計,看這一篇就夠了(原型規範下載) 經過了將近一年的後臺產品經歷,踩了很多坑,試了很多錯,也學習到了很多東西,目前也形成了自己的一套規範。本文將其中的部分收穫彙總成文,

Linux 常用指令 —— 摘自《Linux Probe》

touch:用於建立空白檔案或設定檔案的時間,ps:黑客可以用touch指令來修改檔案的最後修改時間,以隱藏自己的修改行為。 mkdir:用於建立空白的目錄,如mkdir path,可以結合引數-p來遞迴建立檔案目錄,如mkdir -p a/b/c/d/e cp:用於複製檔案或目錄,如cp 1.txt p

樹狀陣列(Binary Indexed Tree)

定義 根據維基百科的定義: A Fenwick tree or binary indexed tree is a data structure that can efficiently update elements and calculate pr

Cookie介紹及在Android中的使用總結超詳細

Cookie介紹 cookie的起源 早期Web開發面臨的最大問題之一是如何管理狀態。簡言之,伺服器端沒有辦法知道兩個請求是否來自於同一個瀏覽器。那時的辦法是在請求的頁面中插入一個token,並且在下一次請求中將這個token返回(至伺服器)。這就需要在form中插入一個包含toke

關於Kaggle入門

這次醞釀了很久想給大家講一些關於Kaggle那點兒事,幫助對資料科學(Data Science)有興趣的同學們更好的瞭解這個專案,最好能親身參與進來,體會一下學校所學的東西和想要解決一個實際的問題所需要的能力的差距。雖然不是Data Science出身,但本著嚴謹的科研態

並查集(Union-Find Algorithm)

動態連線(Dynamic connectivity)的問題 所謂的動態連線問題是指在一組可能相互連線也可能相互沒有連線的物件中,判斷給定的兩個物件是否聯通的一類問題。這類問題可以有如下抽象: 有一組構成不相交集合的物件 union: 聯通兩個物件

Android 必須知道2018年流行的框架庫及開發語言

導語2017 已經悄悄的走了,2018 也已經匆匆的來了,我們在總結過去的同時,也要展望一下未來,來規劃一下今年要學哪些新技術。這幾年優秀Android的開源庫不斷推出,新技術層出不窮,需要我們不斷去了解和掌握,在提高自身開發水平的同時,我們需要付出更多學習精力和時間。俗話說