VS Code开发Spring Boot + Cloud示例(六) 综合示例(Consul + Config + Hystrix + RabbitMQ + Feign + Turbine)

本博客通过VS Code创建并配置Spring Cloud项目,涵盖服务注册与发现(Consul)、配置服务器(Config)、断路器监控(Hystrix Dashboard)、消息中间件(RabbitMQ)、服务调用(Feign)和监控聚合(Turbine)。详细步骤包括子项目创建、依赖管理、服务配置、启动与验证等。
摘要由CSDN通过智能技术生成

接下来直接做一个综合一些的示例,用上服务注册和发现、配置服务器、断路器、消息。

1 创建子项目

之前创建的子项目不需要了,按上几篇说的方法重新创建下列子项目:
- config-dashboard(配置服务器)
- breaker-dashboard(断路器监控)
- microservice1 (微服务1,主要演示服务提供者)
- microservice2 (微服务2,主要演示服务消费者)
各个子项目的settings.gradle中name该成上面的名字,分别打开文件夹,自动生成项目相关文件。
这里写图片描述
这里写图片描述
之后打开主项目文件夹,在settings.gradle中包含子项目

include 'config-dashboard'
include 'breaker-dashboard'
include 'microservice1'
include 'microservice2'

这里写图片描述

2 增加依赖

2.1 主项目

把通用依赖加到主项目的build.gradle

subprojects { //只应用在子项目(不包括主项目)中
    buildscript {
        ext {
            springBootVersion = '2.0.4.RELEASE'
            springCloudVersion = 'Finchley.RELEASE'
        }
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }

    apply plugin: 'java'
    apply plugin: 'eclipse' 
//  apply plugin: 'org.springframework.boot'
//  apply plugin: 'io.spring.dependency-management'

    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = 10 //自动生成的是1.8,我装的是jdk10,所以会有警告

    repositories {
        mavenCentral()
    }

    dependencies {
        compile('org.springframework.boot:spring-boot-starter')
        runtime('org.springframework.boot:spring-boot-devtools')
        compile('org.springframework.boot:spring-boot-starter-web') //支持Web,包括了Spring MVC和嵌入式Tomcat
        compile('org.springframework.boot:spring-boot-starter-actuator') //使微服务支持监控和管理
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
}

2.2 配置服务器

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-consul-discovery') //consul发现服务
    compile('org.springframework.cloud:spring-cloud-config-server') //配置服务器依赖
} 

2.3 断路器监控

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix') //支持断路器
    compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix-dashboard') //支持断路器监控
    compile('org.springframework.cloud:spring-cloud-netflix-turbine') //支持断路器聚合监控(Hystrix Turbine),不加starter就是不包含Eureka
    compile('org.springframework.cloud:spring-cloud-starter-consul-discovery') //consul发现服务
} 

2.4 微服务1

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter')
    compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix') //支持断路器
    compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix-dashboard') //支持断路器监控
    compile('org.springframework.cloud:spring-cloud-starter-consul-discovery') //支持consul发现服务
    compile('org.springframework.cloud:spring-cloud-starter-config') //支持配置服务
    compile('org.springframework.cloud:spring-cloud-starter-bus-amqp') //支持Spring Cloud Bus
}

2.5 微服务2

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter')
    compile('org.springframework.cloud:spring-cloud-starter-consul-discovery') //支持consul发现服务
    compile('org.springframework.cloud:spring-cloud-starter-config') //支持配置服务
    compile('org.springframework.cloud:spring-cloud-starter-openfeign') //支持feign消费服务
} 

3 完善配置服务器

3.1 application.yml

server:
    port: 8888 #配置服务器端口
sprin
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值