浅谈springcloud常用依赖和配置

作者:放气 时间:2023-11-24 07:50:02 

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

# consul
spring:
 cloud:
   consul:
     host: localhost
     port: 8500
     discovery:
       # 注册到 consul 的服务名
       service-name: ${spring.application.name}
       # 监控界面显示 ip
       prefer-ip-address: true

# nacos
spring:
 cloud:
   nacos:
     discovery:
       # 服务端地址
       server-addr: 127.0.0.1:8848

# ribben 负载均衡策略
provider:
 ribbon:
   NFloadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

# feign 超时配置, 集成了 ribbon
ribbon:
 # 连接超时时间  默认 1000ms
 ConnectTimeout: 1000
 # 逻辑处理超时时间 默认 1000ms
 ReadTimeout: 3000

#feign 集成了 hystrix,开启 hystrix
feign:
 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.yml
spring:
 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/refresh
management:
 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-refresh
       include: 'bus-refresh'

# config-client 需配置 rabbitmq 和 在获取数据的 controller 上加 @RefreshScope
spring:
 rabbitmq:
   host: localhost
   port: 5672
   username: guest
   password: guest
   virtual-host: /

# stream-rabbit
spring:
 cloud:
   stream:
     binders:
       # 定义绑定器名称
       mybinder:
         type: rabbit
         # 指定 mq 的环境
         environment:
           spring:
             rabbitmq:
               host: localhost
               port: 5672
               username: guest
               password: guest
               virtual-host: /
     bindings:
       # 生产者 @EnableBinding(Source.class)
       output:
       # 消费者 @EnableBinding(Sink.class), @StreamListener(Sink.INPUT)
       # input:
         binder: mybinder
         # 绑定的交换机名称
         destination: myexchange

# sleuth + zipkin
spring:
 zipkin:
   # zipkin 服务端路径
   base-url: http://lacalhost:9411/
 sleuth:
   sampler:
     # 数据采集率 默认0.1
     probability: 0.1

来源:https://blog.csdn.net/liu1shi/article/details/117308135

标签:springcloud,依赖,配置
0
投稿

猜你喜欢

  • C#生成唯一值的方法汇总

    2023-11-09 07:25:21
  • 详解JS与APP原生控件交互

    2022-11-21 21:28:15
  • C#书写规范

    2023-07-09 09:15:57
  • Spring Boot实现异步请求(Servlet 3.0)

    2023-11-27 06:26:47
  • Java两种方法计算出阶乘尾部连续0的个数

    2021-09-03 06:04:40
  • Java数据类型的全面剖析

    2022-07-09 06:51:38
  • C#使用GDI+实现生成验证码

    2023-04-10 10:42:44
  • C#将制定目录文件名转换成大写的方法

    2022-10-03 19:46:36
  • C#分布式事务的超时处理实例分析

    2022-06-16 03:11:28
  • Java中List.contains(Object object)方法使用

    2022-04-25 20:47:11
  • java中ThreadLocal的基本原理

    2022-03-28 01:13:44
  • Android开发之WebView组件的使用解析

    2022-07-22 21:52:15
  • 浅析依赖注入框架Autofac的使用

    2023-04-19 22:23:31
  • Java设计模式之工厂模式实现方法详解

    2023-11-26 07:55:51
  • C# Winform选项卡集成窗体详解

    2021-08-12 17:13:55
  • C#调用QQ_Mail发送邮件实例代码两例

    2023-11-10 17:27:05
  • 浅谈两个jar包中包含完全相同的包名和类名的加载问题

    2023-04-13 04:47:02
  • SpringBoot整合Activiti工作流框架的使用

    2022-03-02 12:32:44
  • C++ 继承,虚继承(内存结构)详解

    2023-09-05 21:28:01
  • winform实现可拖动的自定义Label控件

    2022-12-14 09:11:36
  • asp之家 软件编程 m.aspxhome.com