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