基于gradle构建spring cloud项目
构建环境
idea:2021.1.2
gradle:4.10.3
项目介绍
gradle-spring-cloud 根项目,用于统一一些公共配置
gradle-eurakeserver 模块使用eurake提供服务注册功能
gradle-getway 提供网关服务
gradle-serviceA和gradle-serviceB用来提供接口服务,服务名相同,用于测试getway 负载均衡
构建开始
一、创建根项目(gradle-spring-cloud)
1.idea创建新的gradle项目
2.配置项目gradle
3.修改buid.gradle文件
buildscript {ext {springBootVersion = '2.0.7.RELEASE'springCloudVersion = 'Finchley.SR2'}repositories {maven { url "http://nexus.xxxxx.com/repository/maven-public" }}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}
allprojects {group 'org.example'version '1.0-SNAPSHOT'apply plugin: 'java'// 指定JDK版本sourceCompatibility = 1.8targetCompatibility = 1.8//指定编码格式tasks.withType(JavaCompile) {options.encoding = "UTF-8"}repositories {maven { url "http://nexus.xxxxx.com/repository/maven-public" }}
}subprojects {//dependency-management 插件apply plugin: 'io.spring.dependency-management'dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"}}jar {manifest.attributes provider: 'gradle'}}
说明: maven { url "http://nexus.xxxxx.com/repository/maven-public" } 配置的是 maven私服地址,大家可以配置成阿里云的镜像地址。
4.build下项目,无报错,根项目构建成功。
二、构建gradle-eurakeserver服务注册中心
1.在根项目下,new module,取名为gradle-eurakeserver
2.修改build.gradle配置文件,加入相关依赖
apply plugin: 'org.springframework.boot'dependencies { //dependencies闭包implementation 'org.springframework.boot:spring-boot-starter-web'implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
3.build项目
4.创建spring boot启动类
package org.example.eurake.server;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);}
}
5.配置application.properties
server.port=7101
spring.application.name=gradle-eureka
#不向注册中心注册自己
eureka.client.register-with-eureka=false
#健康检测
eureka.client.fetch-registry=false
#开发环境关闭自我保护机制,保证不可用的服务及时剔除
eureka.server.enable-self-preservation=false
#间隔2秒钟剔除一次
eureka.server.eviction-interval-timer-in-ms=2000
#服务注册地址
eureka.client.service-url.defaultZone=http://127.0.0.1:7101/eureka
6.启动项目,访问 http://127.0.0.1:7101/ 注册中心搭建完毕
三、搭建gradle-getway网关服务
1.在根项目下,new module,取名为gradle-getway
2.修改build.gradle配置文件,加入相关依赖
dependencies {implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'implementation 'org.springframework.cloud:spring-cloud-starter-gateway'implementation 'org.springframework.cloud:spring-boot-starter-data-redis-reactive'implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'implementation 'com.alibaba:fastjson:1.2.47'
}
3.build项目
4.创建spring boot启动类
@SpringBootApplication
@EnableDiscoveryClient
public class GetWayApplication {public static void main(String[] args) {SpringApplication.run(GetWayApplication.class,args);}
}
5.配置application.yml
# 端口
server:port: 8000spring:application:# 应用名称name: gradle-getwaycloud:gateway:discovery:locator:# 是否和服务注册与发现组件结合,设置为 true 后可以直接使用应用名称调用服务enabled: true# 路由(routes:路由,它由唯一标识(ID)、目标服务地址(uri)、一组断言(predicates)和一组过滤器组成(filters)。filters 不是必需参数。)routes:# 路由标识(id:标识,具有唯一性) 转发指定服务并传入参数- id: route_addRequestParameter# 目标服务地址(uri:地址,请求转发后的地址)uri: lb://gradle-service# 路由条件(predicates:断言,匹配 HTTP 请求内容)predicates:## 匹配 GET 请求- Method=GET# 过滤器(filters:过滤器,过滤规则)filters:## 添加指定参数- AddRequestParameter=age, threeeureka:client:serviceUrl:# 注册中心地址defaultZone: http://127.0.0.1:7101/eurekalogging:level:# log 级别org.springframework.cloud.gateway: debug
6.启动getway,成功注册到eureka
四、创建gradle-service 服务,AB服务只是端口不同,所以只以A为例
1.在根项目下,new module,取名为gradle-serviceA
2.修改build.gradle配置文件,加入相关依赖
dependencies {implementation 'org.springframework.boot:spring-boot-starter-web'implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'implementation 'org.springframework.boot:spring-boot-starter-data-redis'implementation 'org.projectlombok:lombok:1.18.18'implementation 'redis.clients:jedis:2.9.0'implementation 'org.apache.commons:commons-pool2'}
3.build项目
4.创建spring boot启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceApplicationA {public static void main(String[] args) {SpringApplication.run(ServiceApplicationA.class,args);}
}
5.配置application.yml
# 端口
server:port: 9000# 应用名称
spring:application:name: gradle-serviceeureka:client:serviceUrl:# 注册中心地址defaultZone: http://127.0.0.1:7101/eurekafetch-registry: trueregister-with-eureka: true
6.创建controller接口类
@RestController
public class ASayHelloController {/** @ClassName ASayHelloController* @Desc TODO 读取配置文件中的端口* @Date 2019/5/20 23:24* @Version 1.0*/@Value("${server.port}")private String port;/** @ClassName ASayHelloController* @Desc TODO Say Hello* @Date 2019/5/20 23:24* @Version 1.0*/@RequestMapping("/hello")public String hello(){return "Hello!I'm a. port:" + port;}}
7.相同的创建B服务,端口改为9001.
8.启动 AB服务,注册到注册中心,搭建完成
五、测试getway网关功能
调用网关地址如下 http://localhost:8000/hello
第一次返回 Hello!I'm a. port:9000
第二次返回 Hello!I'm a. port:9001
基于gradle 构建简单的cloud 项目,完毕。
基于gradle构建spring cloud项目相关推荐
- IDEA2020.1基于Maven开发spring cloud项目报错 程序包 com.xxx.xxx.xxx不存在
原因分析 在dependency中有相应的包,但是Maven找不到,那么最根本的原因是没引入依赖 通常,一个微服务开发中会创建两个子模块,一个用于写接口和实体类与其他微服务相互调用,另一个写业务逻辑 ...
- 01 | 使用Gradle构建多模块项目
系列文章目录 01 | 使用Gradle构建多模块项目 02 | 架构师必备 - DDD领域驱动设计之落地实践 03 | 异常处理实践 - 抛异常+错误码 从今天开始,我将从无到有的搭建一套基于 Sp ...
- Spring Cloud项目是如何读取bootstrap.properties文件的?
提前说明:关于Spring Cloud和Spring Boot源码分析基于的版本如下所示 <!-- Spring Dependencies --> <dependency> & ...
- SpringCloud核心教程 | 第二篇: 使用Intellij中的maven来快速构建Spring Cloud工程
spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...
- Linux中部署Spring Cloud项目
Linux中部署Spring Cloud项目 文章为本人在学习的过程中,记录部署过程,仅供参考学习.因本人经验不足,教程或有不妥之处,还望指正. 保姆级教程,敬请食用!!! 简介 在学习过程中,部署时 ...
- 基于 Kubernetes 和 Spring Cloud 的微服务化实践
写在前面 网易云容器平台期望能给实施了微服务架构的团队提供完整的解决方案和闭环的用户体验,为此从 2016 年开始,我们容器服务团队内部率先开始进行 dogfooding 实践,看看容器云平台能不能支 ...
- 基于gradle构建Java应用程序
基于gradle构建java应用程序示例 目录 你需要什么 用户手册 初始化项目 项目结构 编译程序 运行程序 总结 源码 你需要什么 大约8分钟 一个文本编辑器 命令行终端 jdk1.7或更高版本 ...
- 告诉老默我想学Spring Cloud了(新手篇):从0到1搭建Spring Cloud项目(实际项目开发的浓缩精华版)
告诉老默我想学Spring Cloud了(新手篇):从0到1搭建Spring Cloud项目 一.前言 二.如何选择版本 2.1 SpringCloud 和 Spring Boot 版本选型 2.1. ...
- 自己动手,使用Spring Initializr从零开始搭建Spring Cloud项目
新建Project 这里使用的开发工具是IDEA,JDK版本1.8. 打开IDEA开发工具,File -> New -> Project 然后一步步往下设置,然后到这一步,选择Spring ...
最新文章
- edge浏览器 开启java,启动Edge浏览器,然后等待其关闭[重复]
- linux 查看 占用内存最多 占用cpu最多 程序
- 没想到你是这样的直播研发骚年
- 【bzoj2693】jzptab 莫比乌斯反演+线性筛
- webpack安装报错(Unexpected end of JSON input while parsing near '...e6139ad7957,tarball')
- jedis mysql 数据结构_Redis的数据结构和内部编码
- SqlServer中如何按姓氏笔画排序
- 利用Python批量重命名一系列文件名杂乱的文件
- 用php写圣诞祝福页面,圣诞祝福文案 抖音圣诞节一句话祝福
- 开源真实场景图像检测数据集汇总
- 物联网边缘-物联网准入或接入安全防护产品及解决方案
- 屏幕绘图最佳利器Pointfix,绿色中文版_我是亲民_新浪博客
- Fixing DSDT
- 视频学习前端的经验之谈
- WGCNA那么多图,都啥意思? 官网
- SQL查询语句(从单表到多表、从简单到复杂)
- 各种网络下的社会面现有安防监控摄像头如何统一接入管理方案介绍
- flink生产环境参数配置
- 把一个c类的网络地址192-168-1-0划分为6个子网,请计算出...
- Velocity使用示例
热门文章
- 织梦教程:自定义模型会员无法投稿的解决方法
- OSG三维渲染引擎编程学习之五:“第一章:OSG介绍” 之 “1.5 OSG模块”
- 李航《统计学习方法》感知机代码
- 打印机显示服务器磁盘已满,打印机无法打印显示内存已满是怎么回事,怎么解决?...
- 服务器装win7自动关机,Win7系统常见的3种自动关机原因及解决方法
- python实现植物大战僵尸_Python开发植物大战僵尸游戏
- ADN: Artifact Disentanglement Network for Unsupervised Metal Artifact Reduction
- 解决VirtualBox不能为虚拟电脑打开一个新任务
- 服务器主板显示不了独立显卡,独立显卡故障——如何才知道显卡和主板不兼容...
- 最大化参数 火车头_火车头采集:网址参数设置教程[参数N]