您的位置:首頁技術文章
文章詳情頁

淺談springcloud常用依賴和配置

瀏覽:5日期:2023-07-13 08:37:15
spring cloud常用依賴和配置整理

淺談springcloud常用依賴和配置

常用依賴

// 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> <!-- 微服務的包 --> <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> <!-- 啟動類長運行配置 @SpringBootApplication --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka 服務端 @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 注冊 http://localhost:8500/ui/dc1/services --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!-- nacos 注冊 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 聲明式服務調用 替代 RestTemplate @EnableFeignClients --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- hystrix 熔斷器,服務降級 @EnableCircuitBreaker --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!-- hystrix 圖形化監控,只能監控一個服務 @EnableHystrixDashboard http://localhost:8769/hystrix --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <!-- turbine 聚合監控 @EnableTurbine http://localhost:8769/turbine.stream --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> <!-- spring-boot 提供的監控 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 網關 --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- git 配置類服務端 @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 自動刷新 --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <!-- stream-rabbitmq 發送消息 --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> <!-- sleuth + zipkin 服務鏈路追蹤。需要 zipkin 的 jar包,圖形化查看地址 http://localhost:9411--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId> </dependency></dependencies> </dependencyManagement></project>

配置

// application.yml# 設置端口server: port: 8000# 服務名spring: application: name: eureka# eureka 配置eureka: instance: hostname: localhost client: service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # 是否需要將自己的路徑注冊到 eureka 服務端 register-with-eureka: true # 是否需要從 eureka 服務端抓取路徑 fetch-registry: true# consulspring: cloud: consul: host: localhost port: 8500 discovery:# 注冊到 consul 的服務名service-name: ${spring.application.name}# 監控界面顯示 ipprefer-ip-address: true# nacosspring: cloud: nacos: discovery:# 服務端地址server-addr: 127.0.0.1:8848# ribben 負載均衡策略provider: ribbon: NFloadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule# feign 超時配置, 集成了 ribbonribbon: # 連接超時時間 默認 1000ms ConnectTimeout: 1000 # 邏輯處理超時時間 默認 1000ms ReadTimeout: 3000#feign 集成了 hystrix,開啟 hystrixfeign: hystrix: enabled: true# feign 設置日志級別,只支持 debug, 請求響應的相關數據logging: level: com.roit.controller: debug# turbine 聚合監控turbine: combine-host-port: true # 配置監控的服務名 app-config: provider,consumer cluster-name-expression: '’default’' aggregator: cluster-config: default #instanceUrlSuffix: /actuator/hystrix.stream# gateway 網關spring: cloud: gateway: routes: - id: provider# provider 的靜態訪問路徑# uri: http://localhost:8001/# 動態uri: lb://provider# 匹配規則predicates:- Path=/goods/**# 局部過濾器filters: - AddRequestParameter=username,zs discovery:locator: # 請求路徑加上微服務名稱,http://localhost/provider/goods/ 或 http://localhost/goods/ 都行 enabled: true # 默認名稱大寫,改為允許小寫 lower-case-service-id: true# config 服務端spring: cloud: config: server:# 文件的倉庫地址git: uri: https://gitee.com/config.git # username: zs # password: 123 # 文件所在分支 label: master# config 客戶端,bootstrap.ymlspring: cloud: config: # http://localhost:8888/master/config-dev.yml # config 服務端地址 # uri: http://localhost:8888 name: config profile: dev,redis label: master # 動態配置 config 服務端地址,先將config 服務端注冊到 eureka discovery:enabled: true# config 服務端的名字,大寫service-id: config-server# config 客戶端 單服務自動刷新# 1. 加依賴 actuator# 2. 獲取數據的 controller 上加@RefreshScope# 3. curl -X POST http://localhost:8001/actuator/refreshmanagement: endpoints: web: exposure:# * 暴露所有;refresh 暴露自動刷新,/actuator/refresh。include: ’*’# bus 自動刷新,先給 config-server 發消息,再由 server 去通知所有的 config-client# bus-amqp 內部使用 rabbitmq 發消息# config-server 需暴露 bus-refresh 和 配置 rabbitmq# curl -X POST http://localhost:8888/actuator/bus-refreshinclude: ’bus-refresh’# config-client 需配置 rabbitmq 和 在獲取數據的 controller 上加 @RefreshScopespring: rabbitmq: host: localhost port: 5672 username: guest password: guest virtual-host: /# stream-rabbitspring: cloud: stream: binders:# 定義綁定器名稱mybinder: type: rabbit # 指定 mq 的環境 environment: spring: rabbitmq:host: localhostport: 5672username: guestpassword: guestvirtual-host: / bindings:# 生產者 @EnableBinding(Source.class)output:# 消費者 @EnableBinding(Sink.class), @StreamListener(Sink.INPUT)# input: binder: mybinder # 綁定的交換機名稱 destination: myexchange# sleuth + zipkinspring: zipkin: # zipkin 服務端路徑 base-url: http://lacalhost:9411/ sleuth: sampler: # 數據采集率 默認0.1 probability: 0.1

到此這篇關于淺談spring cloud常用依賴和配置的文章就介紹到這了,更多相關spring cloud依賴和配置內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
国产综合久久一区二区三区