淺談springcloud常用依賴和配置
spring cloud常用依賴和配置整理

常用依賴
// pom.xml
<?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.roit</groupId>
<artifactId>config</artifactId>
<version>1.0.0</version>
<!-- 微服務(wù)的包 -->
<packaging>pom</packaging>
<!-- spring-boot 父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- spring-cloud 依賴 https://spring.io/projects/spring-cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 啟動(dòng)類長(zhǎng)運(yùn)行配置 @SpringBootApplication -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka 服務(wù)端 @EnableConfigServer http://localhost:8761 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- eureka 客戶端 @EnableEurekaClient -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
<!-- consul 注冊(cè) http://localhost:8500/ui/dc1/services -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- nacos 注冊(cè) http://localhost:8848/nacos -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<!-- feign 聲明式服務(wù)調(diào)用 替代 RestTemplate @EnableFeignClients -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- hystrix 熔斷器,服務(wù)降級(jí) @EnableCircuitBreaker -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- hystrix 圖形化監(jiān)控,只能監(jiān)控一個(gè)服務(wù) @EnableHystrixDashboard http://localhost:8769/hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- turbine 聚合監(jiān)控 @EnableTurbine http://localhost:8769/turbine.stream -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<!-- spring-boot 提供的監(jiān)控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 網(wǎng)關(guān) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- git 配置類服務(wù)端 @EnableConfigServer http://localhost/8888/master/config-dev.yml -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- git 配置類客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- bus-rabbitmq 消息總線,做 config 自動(dòng)刷新 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- stream-rabbitmq 發(fā)送消息 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<!-- sleuth + zipkin 服務(wù)鏈路追蹤。需要 zipkin 的 jar包,圖形化查看地址 http://localhost:9411 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
</dependencyManagement>
</project>
配置
// application.yml
# 設(shè)置端口
server:
port: 8000
# 服務(wù)名
spring:
application:
name: eureka
# eureka 配置
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
# 是否需要將自己的路徑注冊(cè)到 eureka 服務(wù)端
register-with-eureka: true
# 是否需要從 eureka 服務(wù)端抓取路徑
fetch-registry: true
# consul
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
# 注冊(cè)到 consul 的服務(wù)名
service-name: ${spring.application.name}
# 監(jiān)控界面顯示 ip
prefer-ip-address: true
# nacos
spring:
cloud:
nacos:
discovery:
# 服務(wù)端地址
server-addr: 127.0.0.1:8848
# ribben 負(fù)載均衡策略
provider:
ribbon:
NFloadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
# feign 超時(shí)配置, 集成了 ribbon
ribbon:
# 連接超時(shí)時(shí)間 默認(rèn) 1000ms
ConnectTimeout: 1000
# 邏輯處理超時(shí)時(shí)間 默認(rèn) 1000ms
ReadTimeout: 3000
#feign 集成了 hystrix,開啟 hystrix
feign:
hystrix:
enabled: true
# feign 設(shè)置日志級(jí)別,只支持 debug, 請(qǐng)求響應(yīng)的相關(guān)數(shù)據(jù)
logging:
level:
com.roit.controller: debug
# turbine 聚合監(jiān)控
turbine:
combine-host-port: true
# 配置監(jiān)控的服務(wù)名
app-config: provider,consumer
cluster-name-expression: "'default'"
aggregator:
cluster-config: default
#instanceUrlSuffix: /actuator/hystrix.stream
# gateway 網(wǎng)關(guān)
spring:
cloud:
gateway:
routes:
- id: provider
# provider 的靜態(tài)訪問路徑
# uri: http://localhost:8001/
# 動(dòng)態(tài)
uri: lb://provider
# 匹配規(guī)則
predicates:
- Path=/goods/**
# 局部過濾器
filters:
- AddRequestParameter=username,zs
discovery:
locator:
# 請(qǐng)求路徑加上微服務(wù)名稱,http://localhost/provider/goods/ 或 http://localhost/goods/ 都行
enabled: true
# 默認(rèn)名稱大寫,改為允許小寫
lower-case-service-id: true
# config 服務(wù)端
spring:
cloud:
config:
server:
# 文件的倉庫地址
git:
uri: https://gitee.com/config.git
# username: zs
# password: 123
# 文件所在分支
label: master
# config 客戶端,bootstrap.yml
spring:
cloud:
config:
# http://localhost:8888/master/config-dev.yml
# config 服務(wù)端地址
# uri: http://localhost:8888
name: config
profile: dev,redis
label: master
# 動(dòng)態(tài)配置 config 服務(wù)端地址,先將config 服務(wù)端注冊(cè)到 eureka
discovery:
enabled: true
# config 服務(wù)端的名字,大寫
service-id: config-server
# config 客戶端 單服務(wù)自動(dòng)刷新
# 1. 加依賴 actuator
# 2. 獲取數(shù)據(jù)的 controller 上加@RefreshScope
# 3. curl -X POST http://localhost:8001/actuator/refresh
management:
endpoints:
web:
exposure:
# * 暴露所有;refresh 暴露自動(dòng)刷新,/actuator/refresh。
include: '*'
# bus 自動(dòng)刷新,先給 config-server 發(fā)消息,再由 server 去通知所有的 config-client
# bus-amqp 內(nèi)部使用 rabbitmq 發(fā)消息
# config-server 需暴露 bus-refresh 和 配置 rabbitmq
# curl -X POST http://localhost:8888/actuator/bus-refresh
include: 'bus-refresh'
# config-client 需配置 rabbitmq 和 在獲取數(shù)據(jù)的 controller 上加 @RefreshScope
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
# stream-rabbit
spring:
cloud:
stream:
binders:
# 定義綁定器名稱
mybinder:
type: rabbit
# 指定 mq 的環(huán)境
environment:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
bindings:
# 生產(chǎn)者 @EnableBinding(Source.class)
output:
# 消費(fèi)者 @EnableBinding(Sink.class), @StreamListener(Sink.INPUT)
# input:
binder: mybinder
# 綁定的交換機(jī)名稱
destination: myexchange
# sleuth + zipkin
spring:
zipkin:
# zipkin 服務(wù)端路徑
base-url: http://lacalhost:9411/
sleuth:
sampler:
# 數(shù)據(jù)采集率 默認(rèn)0.1
probability: 0.1
到此這篇關(guān)于淺談spring cloud常用依賴和配置的文章就介紹到這了,更多相關(guān)spring cloud依賴和配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程中的CountDownLatch詳細(xì)解讀
這篇文章主要介紹了Java多線程中的CountDownLatch詳細(xì)解讀,一個(gè)同步輔助類,在完成一組正在其他線程中執(zhí)行的操作之前,它允許一個(gè)或多個(gè)線程一直等待,用給定的計(jì)數(shù) 初始化 CountDownLatch,需要的朋友可以參考下2023-11-11
Java基礎(chǔ)詳解之集合框架工具Collections
這篇文章主要介紹了Java基礎(chǔ)詳解之集合框架工具Collections,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-04-04
Java 多態(tài)中繼承的轉(zhuǎn)型詳解與用法分析
繼承是java面向?qū)ο缶幊碳夹g(shù)的一塊基石,因?yàn)樗试S創(chuàng)建分等級(jí)層次的類。繼承就是子類繼承父類的特征和行為,使得子類對(duì)象(實(shí)例)具有父類的實(shí)例域和方法,或子類從父類繼承方法,使得子類具有父類相同的行為2021-10-10
java并發(fā)編程中實(shí)現(xiàn)可見性的四種可行方案解析
這篇文章主要介紹了java并發(fā)編程中實(shí)現(xiàn)可見性的四種可行方案解析,使用關(guān)鍵字volatile和使用鎖(如synchronized關(guān)鍵字或者java.util.concurrent包中的鎖)來確保對(duì)共享變量的修改在多線程環(huán)境中能夠正確地被其他線程所觀察到,需要的朋友可以參考下2023-08-08
Spring Native項(xiàng)目實(shí)戰(zhàn)(體驗(yàn)79毫秒啟動(dòng)springboot應(yīng)用)
Spring Native是Spring提供的、制作native image的技術(shù)方案,本篇主要內(nèi)容是開發(fā)springboot應(yīng)用再構(gòu)建為native image的方法,通過Spring Native項(xiàng)目實(shí)戰(zhàn)讓大家體驗(yàn)79毫秒啟動(dòng)springboot應(yīng)用,感興趣的朋友跟隨小編一起看看吧2021-05-05
Java 利用DeferredResult實(shí)現(xiàn)http輪詢實(shí)時(shí)返回?cái)?shù)據(jù)接口
這篇文章主要介紹了Java 利用 DeferredResult 實(shí)現(xiàn) http 輪詢實(shí)時(shí)返回?cái)?shù)據(jù)接口,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-03-03
SpringBoot如何用java生成靜態(tài)html
這篇文章主要介紹了SpringBoot如何用java生成靜態(tài)html,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,需要的朋友可以參考一下2022-06-06

