SpringBoot
配置優(yōu)先級(jí)
- 在命令行中傳入的參數(shù) 如:java -jar storeMs.jar --server.port=8888
- spring_application_json的屬性
- java:comp/env中的jndi屬性
- Java的系統(tǒng)屬性,可以通過(guò)System.getProperties()獲得的內(nèi)容。
- 操作系統(tǒng)的環(huán)境變量
- 通過(guò)random.*配置的隨機(jī)屬性
- 位于當(dāng)前Jar包之外的 application-{profile}.properties文件
- 位于當(dāng)前Jar包之內(nèi)的 application-{profile}.properties文件
- 位于當(dāng)前Jar包之外的 application.properties文件
- 位于當(dāng)前Jar包之內(nèi)的 application.properties文件
- 在@Configuration注解修改的類中,通過(guò)@PropertySource注解定義的屬性。
- 應(yīng)用默認(rèn)屬性 使用SpringApplication.setDefaultProperties定義的內(nèi)容。
了解springboot的配置加載順序,有助于理解springCloud的遠(yuǎn)程配置中心實(shí)現(xiàn)的原理。
監(jiān)控功能
- 引入actuator依賴
- 使用相關(guān)的端點(diǎn)接口,查看監(jiān)控的信息
- 監(jiān)控的三種類:?jiǎn)?dòng)時(shí)系統(tǒng)配置相關(guān)、運(yùn)行時(shí)個(gè)項(xiàng)功能的性能監(jiān)控、系統(tǒng)的操作控制。
SpringCloud
Eureka注冊(cè)服務(wù)中心 三個(gè)重要的角色:服務(wù)注冊(cè)中心、服務(wù)提供者、服務(wù)消費(fèi)者。
- 單點(diǎn)模式 注冊(cè)服務(wù)中心只有一個(gè)。
#指定端口號(hào)
server.port=1111
#是否優(yōu)先使用IP地址作為主機(jī)名的標(biāo)識(shí) 默認(rèn)為false
eureka.instance.preferIpAddress=true
#是否注冊(cè)到eureka
eureka.client.register-with-eureka=false
#是否從eureka獲取注冊(cè)信息
eureka.client.fetch-registry=false
#eureka服務(wù)器的地址(注意:地址最后面的 /eureka/ 這個(gè)是固定值)
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
- 高可用模式 服務(wù)中心可用將自己作為服務(wù)提供者,注冊(cè)到相關(guān)的服務(wù)中心去。服務(wù)中心可以有多個(gè),集群的方式。
#application-peer1.properties
spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/
#application-peer2.properties
spring.application.name=eureka-server
server.port=1112
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/
- 在有多個(gè)eureka注冊(cè)服務(wù)的情況下,服務(wù)提供者需要配置所有的eureka中心
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/
- 服務(wù)提供者 可以有多個(gè)服務(wù)提供者 eureka會(huì)將所有的相同名稱的服務(wù),做成一個(gè)列表的形式,ribbon可以實(shí)現(xiàn)負(fù)載均衡 獲取服務(wù)。
spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
server.port=8082[8081]
- 服務(wù)消費(fèi)者 通過(guò)@LoadBalanced來(lái)負(fù)載均衡
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return RestTemplateMgr.getInstance().init().getTemplate();
}
- 服務(wù)續(xù)約
#發(fā)送心跳個(gè)server的頻率 默認(rèn)30秒
eureka.instance.lease-renewal-interval-in-seconds=30
#兩個(gè)心跳之間的時(shí)間間隔 超過(guò)則將服務(wù)摘除。 默認(rèn)90秒
eureka.instance.lease-expiration-duration-in-seconds=90
- 其他的一些配置 如:服務(wù)失效剔除、eureka的自我保護(hù)、服務(wù)下線等。
# 單機(jī)調(diào)試的時(shí)候 可以關(guān)閉保護(hù)機(jī)制
eureka.server.enable-self-preservation=false
- eureka服務(wù)的具體配置信息 可以查看:com.netflix.eureka.EurekaServerConfig 都是以eureka.server開(kāi)頭。
- eureka客戶端的具體配置信息 可以查看:org.springframework.cloud.netflix.eureka.EurekaClientConfigBean 都是以eureka.client開(kāi)頭。
- 服務(wù)實(shí)例類配置信息 可以查看:org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean 都是以eureka.instance開(kāi)頭。
springCloud Ribbon 客戶端負(fù)載均衡
- RibbonEurekaAutoConfiguration 自動(dòng)配置類
- 開(kāi)啟負(fù)載均衡的步驟:
- 多個(gè)服務(wù)提供者,注冊(cè)到服務(wù)中心
- 服務(wù)消費(fèi)者通過(guò)調(diào)用被@LoadBalanced注解修飾過(guò)的restTemplate
- RestTemplate 與 Ribbon整合使用 詳解
- RestTemplate 基本使用 GET POST PUT DELETE
- RestTemplate 與 Ribbon 整合
- 重點(diǎn)源碼:LoadBalancerClient LoadBalancerAutoConfiguration
springCloud Hystrix 服務(wù)容錯(cuò)保護(hù)
- HystrixCommand:用在依賴的服務(wù)返回單個(gè)操作結(jié)果的時(shí)候
- HystrixObservableCommand:用在依賴的服務(wù)返回多個(gè)操作結(jié)果的時(shí)候
- 通過(guò)幾個(gè)注解的方式可以簡(jiǎn)單使用斷路器的功能
#程序啟動(dòng)的地方
@EnableCircuitBreaker
#具體需要斷路器的服務(wù)方法上
@HystrixCommand(fallbackMethod = "helloFallback", commandKey = "helloKey")
#斷路器被觸發(fā)熔斷的回調(diào)方法
public String helloFallback() {}
springCloud Feign 聲明是服務(wù)調(diào)用
- 尚未仔細(xì)看
springCloud Zuul API網(wǎng)關(guān)服務(wù)
- Zuul的使用
#在pom.xml引入spring-cloud-starter-zuul
#在application.properties配置
spring.application.name=api-gateway
server.port=5555
#在啟動(dòng)類使用@EnableZuulProxy
- Zuul的主要功能有:
- 請(qǐng)求轉(zhuǎn)發(fā),即路由的功能;與服務(wù)治理框架結(jié)合,實(shí)現(xiàn)自動(dòng)化的服務(wù)實(shí)例維護(hù)以及負(fù)載均衡的路由轉(zhuǎn)發(fā)。
- 請(qǐng)求過(guò)濾,即可以當(dāng)做是權(quán)限驗(yàn)證。權(quán)限校驗(yàn)與微服務(wù)業(yè)務(wù)邏輯解耦。
- 它作為系統(tǒng)的統(tǒng)一入口,屏蔽了系統(tǒng)內(nèi)部各個(gè)服務(wù)的細(xì)節(jié)。
- 傳統(tǒng)路由方式
zuul.routes.api-a-url.path=/api-a-url/**
zuul.routes.api-a-url.url=http://localhost:8001/
- 面向服務(wù)的路由 使用eureka服務(wù)
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=hello-service
zuul.routes.api-b.path=/api-b/**
zuul.routes.api-b.serviceId=hello-service
zuul.routes.api-c.path=/ddd/**
zuul.routes.api-c.serviceId=hello-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
- 使用服務(wù)名稱的方式 不用eureka服務(wù)治理
zuul.routes.api-d.path=/ddd/**
zuul.routes.api-d.serviceId=hello
ribbon.eureka.enabled=false
hello.ribbon.listOfServers=http://localhost:8001/,http://localhost:8002/
- Cookie與頭信息 為保證請(qǐng)求經(jīng)過(guò)zuul轉(zhuǎn)發(fā)后,還保留有Cookie Heads等信息,需要做一些配置:
#通過(guò)設(shè)置全局參數(shù)為空來(lái)覆蓋默認(rèn)值
zuul.senstitiveHeaders=
#通過(guò)制定路由的參數(shù)來(lái)配置 有如下兩種配置
zuul.routes.<router>.customSensitiveHeaders=true
zuul.routes.<router>.senstiveHeaders=
- 重定向問(wèn)題 spring security 和 shiro
zuul.addHostHeader=true
SpringCloud 的 Brixton會(huì)有重定向問(wèn)題 Camden Dalston 則沒(méi)有
- Hystrix和Ribbon支持 盡量使用 path與serviceId對(duì)應(yīng) 即使用面向服務(wù)的路由。
- 動(dòng)態(tài)加載/動(dòng)態(tài)路由:原理 將配置放置在git遠(yuǎn)程倉(cāng)庫(kù),更新倉(cāng)庫(kù)的配置文件,調(diào)用refresh接口,加載新的配置信息。
#
- 動(dòng)態(tài)過(guò)濾器:使用groovy實(shí)現(xiàn)。
springCloud Sleuth 分布式服務(wù)跟蹤
- 整合使用
- 添加pom.xml的依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
- 啟動(dòng)eureka服務(wù)
- 訪問(wèn) 查看日志 分析日志即可看出鏈路調(diào)用的規(guī)則。 [trace-1,7cbdce82c9447510,7667724d864b3ec,false]
trace-1:應(yīng)用名稱 即application.properties中的spring.application.name的值
7cbdce82c9447510:springCloud Sleuth生成的一個(gè)ID 稱為TraceID 用來(lái)標(biāo)識(shí)一條請(qǐng)求鏈路
7667724d864b3ec:springCloud Sleuth生成的另外一個(gè)ID 稱為SpanID 表示一個(gè)基本的工作單元 比如發(fā)送一個(gè)HTTP請(qǐng)求。
false:代表該信息是否要被后續(xù)的跟蹤信息收集器獲取和存儲(chǔ)。
一條請(qǐng)求鏈路中 只能包含一個(gè)TraceID 可以有多SpanID
- 服務(wù)跟蹤的實(shí)現(xiàn)原理
1.服務(wù)框架為每個(gè)請(qǐng)求創(chuàng)建唯一的跟蹤標(biāo)識(shí)。 一般是在httpHeader里標(biāo)識(shí)
2.統(tǒng)計(jì)各個(gè)處理單元的時(shí)間耗時(shí)。 下一個(gè)單元開(kāi)始 上一個(gè)單元結(jié)束。
3.源碼跟蹤:org.springframework.cloud.sleuth.Span
public static final String SAMPLED_NAME = "X-B3-Sampled";
public static final String PROCESS_ID_NAME = "X-Process-Id";
public static final String PARENT_ID_NAME = "X-B3-ParentSpanId";
public static final String TRACE_ID_NAME = "X-B3-TraceId";
public static final String SPAN_NAME_NAME = "X-Span-Name";
public static final String SPAN_ID_NAME = "X-B3-SpanId";
public static final String SPAN_EXPORT_NAME = "X-Span-Export";
- Sleuth抽樣收集策略
1. 通過(guò)Sampler接口實(shí)現(xiàn) 默認(rèn)使用PercentageBasedSampler
@Bean
public AlwaysSampler defaultSampler() {
return new AlwaysSampler();
}
2. 通過(guò)配置文件配置
spring.sleuth.sampler.percentage=0.1 #代表獲取10%的樣例 1代表100%
- 與Logstash整合 收集日志分析
- ELK平臺(tái) ElasticSearch/Logstash/Kibana這三個(gè)工具。
- 配置logstash對(duì)JSON格式日志的支持
- 與Zipkin整合 生產(chǎn)開(kāi)發(fā)中,應(yīng)該使用這個(gè) 附帶有圖形界面查看鏈路調(diào)用的服務(wù)。
- 四大核心組件:
1. Collector:收集器組件,主要處理從外部系統(tǒng)發(fā)送過(guò)來(lái)的信息,將這些信息轉(zhuǎn)換成Zipkin內(nèi)部處理的Span格式,以支持后續(xù)的存儲(chǔ)、分析、展示等功能。
2. Storage:存儲(chǔ)組件,主要處理收集器收到的信息,默認(rèn)會(huì)將這些信息存儲(chǔ)在內(nèi)存中。可以修改存儲(chǔ)的策略,通過(guò)使用其他存儲(chǔ)組件,將跟蹤信息存儲(chǔ)到數(shù)據(jù)庫(kù)中。
3. Restful API:API組件,童工外部訪問(wèn)接口。
4. WEB UI:UI組件,基于API組件實(shí)現(xiàn)的上層應(yīng)用。方便用戶查詢、分析跟蹤信息。
- Sleuth與Zipkin整合 HTTP方式。
1. 搭建ZipKinServer 服務(wù)
<dependencies>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
</dependencies>
2. 為應(yīng)用引入和配置ZipKin服務(wù)
spring.zipkin.base-url=http://localhost:9411
- Sleuth與Zipkin整合 消息中間件收集
1. 為具體應(yīng)用添加pom依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
2. 配置rabbitmq服務(wù):
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=springcloud
spring.rabbitmq.password=123456
3. 修改zipkin-server的pom依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
- API接口
springboot + elasticsearch全文檢索
- Elasticsearch與關(guān)系型數(shù)據(jù)庫(kù)的比對(duì)
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
Elasticsearch集群可以包含多個(gè)索引(indices)(數(shù)據(jù)庫(kù)) ,每一個(gè)索引可以包含多個(gè)類型(types)(表) ,每一個(gè)類型包含多個(gè)文檔(documents)(行) ,然后每個(gè)文檔包含多個(gè)字段(Fields)(列) 。
- DSL查詢(Query DSL) DSL(Domain Specific Language特定領(lǐng)域語(yǔ)言)以JSON請(qǐng)求體的形式出現(xiàn)
- 高亮(highlight)匹配
- 短語(yǔ)搜索
- 全文搜索
- 分析 聚合 在數(shù)據(jù)上生成復(fù)雜的分析統(tǒng)計(jì)
自動(dòng)配置類
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchProperties
框架整合
- POM.xml的配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<!-- 添加 spring-data-elasticsearch的依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
<version>3.0.9</version>
</dependency>
- application.properties
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.properties.path.logs=./elasticsearch/logs
spring.data.elasticsearch.properties.path.data=./elasticsearch/data
#獨(dú)立服務(wù)或者是es集群的時(shí)候打開(kāi)配置 cluster-name 必須與es配置的name一致
#spring.data.elasticsearch.cluster-name: #默認(rèn)為elasticsearch
#spring.data.elasticsearch.cluster-nodes: IP:port #配置es節(jié)點(diǎn)信息,多個(gè)用逗號(hào)分隔
- 注解說(shuō)明
- @Document
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Document {
String indexName();//索引庫(kù)的名稱,一般用項(xiàng)目的名稱命名
String type() default "";////類型,一般用實(shí)體的名稱命名
boolean useServerConfiguration() default false;
short shards() default 5;//默認(rèn)分區(qū)數(shù)
short replicas() default 1;//每個(gè)分區(qū)默認(rèn)的備份數(shù)
String refreshInterval() default "1s";//刷新間隔
String indexStoreType() default "fs";//索引文件存儲(chǔ)類型
boolean createIndex() default true;
}
- @Field
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
@Inherited
public @interface Field {
FieldType type() default FieldType.Auto;//自動(dòng)檢測(cè)屬性的類型
FieldIndex index() default FieldIndex.analyzed;//默認(rèn)情況下分詞
DateFormat format() default DateFormat.none;
String pattern() default "";
boolean store() default false;//默認(rèn)情況下不存儲(chǔ)原文
String searchAnalyzer() default "";//指定字段搜索時(shí)使用的分詞器
String analyzer() default "";//指定字段建立索引時(shí)指定的分詞器
String[] ignoreFields() default {};//指定需要忽略的字段
boolean includeInParent() default false;
}
springCloud 與 springBoot問(wèn)題匯總
- zuul 與 eureka 使用的springCloud版本需要對(duì)應(yīng)上,否則容易引起Ribbon的hystrix熔斷機(jī)制。