基于gradle使用springboot2的springcloud-Greenwich.SR4多模块项目--openfeign和gateway使用

gradle项目里的build.gradle相当于maven的pom.xml,可以配置整个项目依赖和各个模块的依赖

idea新建项目,gradle,不选java,创建一个空项目,如果选了java,会在根目录创建src目录,因为是多模块,所以不需要在根目录写代码
在这里插入图片描述
下一步可以设置项目groupid
在这里插入图片描述
创建好空项目,idea会添加gradle程序,这些文件应该和代码一起放到git里,这样才是完整的gradle项目
在这里插入图片描述
需要的.gitignore忽略设置

.idea
.gradle
*/build

在项目根目录右键new–module,新建模块
在这里插入图片描述
根目录settings.gradle

rootProject.name = 'springcloud-gradle'
include 'eureka-service'
include 'app1'
include 'app2'
include 'gateway'

根目录build.gradle

group 'com.ddddd'
version '1.0'

buildscript {

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.10.RELEASE")
    }
}

allprojects {
    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'org.springframework.boot'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }

    dependencyManagement {
        imports {
            mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Greenwich.SR4'
        }
    }
}

subprojects {

    dependencies {
        // 通用依赖

        // 测试依赖
        testCompile(
                "junit:junit:4.12"
        )
    }

}

// 否则不能从项目顶层的tasks--build--bootJar执行,报找不到mainclass
bootJar {
    enabled = false
}

jar {
    enabled = true
}

下面是各个模块的内容,springcloud微服务框架,1个eureka,1个gateway,2个app
项目总览
在这里插入图片描述

  • eureka
//build.gradle
plugins {
    id 'java'
}

group 'com.ddddd'
version '1.0'

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

bootJar {
    archiveFileName = "myEureka.jar"  // 自定义jar包名称
}

//application.yml
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://127.0.0.1:${server.port}/eureka/
  enableSelfPreservation: false
server:
  port: 8000

//EurekaService.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaService {

    public static void main(String[] args) {
        SpringApplication.run(EurekaService.class);
    }
}
  • gateway
//build.gradle
plugins {
    id 'java'
}

group 'com.ddddd'
version '1.0'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-gateway')
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

//application.yml
eureka:
  instance:
    hostname: gateway
    preferIpAddress: true
    instance-id: 127.0.0.1:${server.port}
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8000/eureka/

server:
  port: 9000
spring:
  application:
    name: sgateway
  cloud:
    gateway:
      routes:
      - id: sapp1
        uri: lb://sapp1
        predicates:
        - Path= /papp1/**
        filters:
        - StripPrefix= 1 # 删除url的第一间隔,就是去掉/papp1,把后面的url转发为微服务
      - id: sapp2
        uri: lb://sapp2
        predicates:
        - Path= /papp2/**
        filters:
        - StripPrefix= 1

//Gateway.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Gateway {

    public static void main(String[] args) {
        SpringApplication.run(Gateway.class);
    }
}
  • app1
//build.gradle
plugins {
    id 'java'
}

group 'com.ddddd'
version '1.0'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    compile('org.springframework.cloud:spring-cloud-starter-openfeign')
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

//application.yml
eureka:
  instance:
    hostname: app1
    preferIpAddress: true
    instance-id: 127.0.0.1:${server.port}
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8000/eureka/

server:
  port: 8091
spring:
  application:
    name: sapp1

//App1.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class App1 {

    public static void main(String[] args) {
        SpringApplication.run(App1.class);
    }
}

//FeignApp2.java
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("sapp2")
public interface FeignApp2 {

    @RequestMapping("/app1Feign")
    public String app1Feign();
}

//TestController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController

public class TestController {

    @RequestMapping({"","/"})
    public String hi(){
        return "app1-hi";
    }

    @Autowired
    private FeignApp2 feignApp2;

    @RequestMapping("/feignTest")
    public String feignTest(){
        return feignApp2.app1Feign();
    }

}
  • app2
//build.gradle
plugins {
    id 'java'
}

group 'com.ddddd'
version '1.0'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

//application.yml
eureka:
  instance:
    hostname: app2
    preferIpAddress: true
    instance-id: 127.0.0.1:${server.port}
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8000/eureka/

server:
  port: 8092
spring:
  application:
    name: sapp2

//App2.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App2 {

    public static void main(String[] args) {
        SpringApplication.run(App2.class);
    }
}

//TestController.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping({"","/"})
    public String hi(){
        return "app2-hi";
    }


    @RequestMapping("/app1Feign")
    public String app1Feign(){
        return "app1Feign from app2";
    }
}

双击bootJar打jar包,需要根目录build.gradle加bootJar设置,否则不能在项目根的tasks执行bootJar来生成所以模块的jar包,参考eureka的build.gradle可以设置jar包名
在这里插入图片描述

发布了259 篇原创文章 · 获赞 118 · 访问量 187万+

猜你喜欢

转载自blog.csdn.net/c5113620/article/details/103428338