idea 创建gradle
1. 创建
2. 版本号可以变更:gradle-7.4.1-all.zip
3.创建个版本管理文件 version.gradle
4.定义版本版本号:例如:
使用 ext{ } 把要统一的版本号进行归类统一
版本依赖后续补充:
ext {
orgSpringframeworkBootVersion = "2.6.6"
springBootStarterWebVersion = "2.6.6"
springBootStarterWebFluxVersion = "2.6.6"
springBootStarterActuatorVersion = "2.6.6"
springBootStarterTestVesion = "2.6.6"
springBootStarterValidation="2.6.6"
springCloudStarterLoadbalancer="3.1.1"
springCloudStarterNetflixRibbon="2.6.6"
lombokVersion = "1.18.22"
}
在build.gradle文件下加上:apply from: “version.gradle”
修改build.gradle:
apply from: "version.gradle"
//自带属性 group-group version-项目版本 name-项目名
//name 'wap_greatest_work'
group 'com.xms'
version '1.0.1'
// 设置所有project 信息 root + child
allprojects {
// 指定去maven的远程仓库查找
repositories {
maven {
url 'https://maven.aliyun.com/repository/public/'
}
maven {
url = 'https://maven.aliyun.com/repository/spring/'
}
mavenCentral()
// 指定去maven的本地仓库查找
// maven {
// url 'file:///C:/'
// }
// maven {
// url "$rootDir/lib/xxx"
// }
// mavenLocal()
}
}
println "---wap_greatest_work---: subprojects"
//对所有Child Project的进行统一配置
subprojects {
afterEvaluate { project ->
if (project.hasProperty("dependencies")) {
dependencies {
compileOnly "org.projectlombok:lombok:${rootProject.ext.lombokVersion}"
annotationProcessor "org.projectlombok:lombok:${rootProject.ext.lombokVersion}"
testImplementation "org.springframework.boot:spring-boot-starter-test:${rootProject.ext.springBootStarterTestVesion}"
testImplementation "io.projectreactor:reactor-test:${rootProject.ext.reactorTestVersion}"
}
}
}
}
创建子模块–eureka
1.创建
2.检查 setting.gradle文件下面有没有子模块信息
3.修改build.gradle
build.gradle:
//引入依赖相关的gradle插件
plugins {
id 'org.springframework.boot' version '2.6.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.xms'
version = '1.0.1'
sourceCompatibility = '1.8'
//配置此项目依赖项配置
configurations {
compileOnly {
extendsFrom annotationProcessor
}
providedRuntime
// remove default logger 删除默认的 logger
all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2021.0.1")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
implementation "org.springframework.boot:spring-boot-starter-log4j2"
implementation "org.springframework.boot:spring-boot-starter-actuator"
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
//依赖项管理
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
依赖说明:
1. eureka 所用的依赖:
1.spring-cloud-starter-netflix-eureka-server
2.spring-boot-starter-web
2. log 依赖:
1.spring-boot-starter-log4j2
2.springboot默认用logback日志输出 ,当我们使用log4j,为避免因包冲突导致配置文件失效,去掉springboot默认配置
3. spring-boot-starter-actuator:
1.检测系统的健康情况、当前的Beans、系统的缓存
2.配置访问的uri、权限、端口:后续说明
***// properties文件:配置:***
management.server.port=8081 访问端口
management.endpoints.web.base-path=/actuator/z 根路径
management.endpoints.web.exposure.include=* web端允许路径
***// yml文件配置:***
management:
endpoints:
web:
exposure:
include: '*'
4.依赖管理项
4.配置文件:
1. log4j2
参考:
springboot 整合log4j2
2.配置application.yml
这里采用了yml的配置文件,删除了初始的properties
代码如下:
server:
port: 10001
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10001/eureka
register-with-eureka: true # 是否将自己作为客户端注册到注册中心:false-不注册。默认为true。集群搭建中,设置true
fetch-registry: false # 是否需要从注册中心检索获取服务的注册信息。默认值为true 单机版的可设置成false,集群版的由于需要同步其他节点的服务注册数据,故设成true
healthcheck:
enabled: true
server:
#enable-self-preservation: true
#是否开启自我保护模式,默认值true
#eureka server默认在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期,
#但是在保护期内如果服务刚好这个服务提供者非正常下线了,此时服务消费者就会拿到一个无效的服务实例,此时会调用失败
enable-self-preservation: false # 关闭自我保护 不向注册中心注册自己
#扫描失效服务的间隔时间,单位毫秒,默认值 60 * 1000
eviction-interval-timer-in-ms: 5000 # 每隔5秒钟,进行一次服务列表的清理
instance:
lease-renewal-interval-in-seconds: 5 # 5秒钟发送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期
hostname: ${server.port}
### log config
logging:
config: classpath:log/log4j2.xml
3.测试
启动项目,输入地址,打开页面如下:
eureka 配置完成。
创建子模块–gateway(网关)
Spring Cloud Gateway是Spring官方基于Spring5.0、SpringBoot2.0和Project Reactor等技术开发的网关,旨在为微服务框架提供一种简单而有效的统一的API路由管理方式,统一访问接口。
Spring Cloud Gateway作为Spring Cloud生态体系中的网关,目标是替代Netflix的Zuul,其不仅提供统 一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全、监控/埋点和限流等等。 它是基于Netty的响应式开发模式。
1.创建gateway项目(网关)
2.检查配置中有没有子模块信息,没有手动添加
3.修改build.gradle
build.gradle:
//引入依赖相关的gradle插件
plugins {
id 'org.springframework.boot' version '2.6.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.xms'
version = '1.0.1'
sourceCompatibility = '1.8'
//配置此项目依赖项配置
configurations {
compileOnly {
extendsFrom annotationProcessor
}
providedRuntime
// remove default logger 删除默认的 logger
all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2021.0.1")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
implementation 'org.apache.commons:commons-lang3'
implementation "com.google.code.gson:gson"
implementation "org.springframework.cloud:spring-cloud-starter-openfeign"
implementation "com.alibaba:fastjson:1.2.74"
compileOnly "org.projectlombok:lombok:"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
}
//依赖项管理
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
依赖说明:
1. eureka 所用的依赖:
1.spring-boot-starter-actuator:
检测系统的健康情况、当前的Beans、系统的缓存
2.spring-cloud-starter-netflix-eureka-client :
注册到eureka 所用的客户端
3.spring-cloud-starter-gateway
4.org.springframework.cloud:spring-cloud-starter-openfeign
Feign是一款Java语言编写的HttpClient绑定器,在Spring Cloud微服务中用于实现微服务之间的声明式调用。
5.等等。。。。。
4.配置文件:
1.log4j2:
同上:参考:
springboot 整合log4j2
2.配置application.yml
server:
port: 10000
spring:
application:
name: gateway-sever
cloud:
gateway:
routes:
- id: xxxxxxx
uri: lb://xxxxx
predicates:
- Path=/cs/**
filters:
- StripPrefix=1
# 全局跨域
globalcors:
# 跨域配置(可以在代码里面处理允许跨域,也可在这里全局处理)
corsConfigurations:
'[/**]':
#allowedOrigins: "*"
allowedOriginPatterns: "*"
allowedHeaders: "*"
allowCredentials: true
allowedMethods:
- GET
- POST
- OPTIONS
- DELETE
- PUT
- HEAD
- PATCH
default-filters:
- DedupeResponseHeader=Vary Access-Control-Allow-Credentials Access-Control-Allow-Origin, RETAIN_UNIQUE
- DedupeResponseHeader=Access-Control-Allow-Origin, RETAIN_FIRST
eureka:
# instance:
# prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
registry-fetch-interval-seconds: 5
service-url:
defaultZone: http://127.0.0.1:10001/eureka
instance:
lease-renewal-interval-in-seconds: 5 # 5秒钟发送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期
### actuator
management:
endpoints:
web:
exposure:
include: '*'
### log config
logging:
config: classpath:log/log4j2.xml
level:
org.springframework.cloud.gateway: DEBUG
reactor.netty.http.client: DEBUG
说明:后续更新。
5.测试
修改启动类:
修改配置文件:eureka 的地址
发现服务已经注册:
创建子模块–api
修改build.gradle,导入生产所用的依赖
修改配置文件(数据库,注册中心等 )
等(同上)