浅谈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> <!-- 微服务的包 --> <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
到此这篇关于浅谈spring cloud常用依赖和配置的文章就介绍到这了,更多相关spring cloud依赖和配置内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)