Spring Cloud(一):使用Gradle搭建Eureka

一.创建gradle项目(步骤省略)

二.添加Eureka的mvn依赖

buildscript {
    ext{
        springBootVersion = '2.1.7.RELEASE'    
    }
    repositories {
         maven{ url 'http://nexus.d17w.cn:9092/nexus/content/groups/public'}
         maven{ url 'http://repo.spring.io/libs-milestone'}
    }
    dependencies {
        classpath ( "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath ( "io.spring.gradle:dependency-management-plugin:1.0.8.RELEASE")
        classpath ( "com.bmuschko:gradle-cargo-plugin:2.6.1")
    }
}

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


group 'com.claimplat'
version = '1.0.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8

tasks.withType(JavaCompile){
    options.encoding = "UTF-8"
}

def env = System.getProperty("package.environment") ?: "dev"

jar{
    baseName = 'claimplat-eureka'
}

war {
    enabled = true
    baseName = 'claimplat-eureka'
    archiveName 'eureka.war'
}

repositories {
    maven{ url 'http://nexus.d17w.cn:9092/nexus/content/groups/public'}
    maven{ url 'http://repo.spring.io/libs-milestone'}
}

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

dependencies {   
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-config'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-server'
    compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-logging'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-aop'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    compile group: 'org.springframework.boot', name: 'spring-boot-devtools'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
    testCompile group: 'junit', name: 'junit'
}

注:添加依赖后需要刷新gradle工程(下载依赖)


三.创建主启动类与配置文件

主启动类如下:

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication implements ApplicationListener{

    private static final Logger LOGGER = LoggerFactory.getLogger(EurekaApplication.class);

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

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if(event instanceof ApplicationEnvironmentPreparedEvent) {
            LOGGER.info("初始化环境变量");
        }else if(event instanceof ApplicationPreparedEvent) {
            LOGGER.info("初始化完成");
        }else if(event instanceof ContextRefreshedEvent) {
            LOGGER.info("应用刷新");
        }else if(event instanceof ApplicationReadyEvent) {
            LOGGER.info("应用已启动完成");
        }else if(event instanceof ContextStartedEvent) {
            LOGGER.info("应用启动");
        }else if(event instanceof ContextStoppedEvent) {
            LOGGER.info("应用停止");
        }else if(event instanceof ContextClosedEvent) {
            LOGGER.info("应用关闭");
        }else {
            //LOGGER.info("其他事件"+event.toString());
        }

    }
}


application.properties配置文件如下:

package.environment=dev
debug=false
 
server.port=8761
server.servlet.context-path=/claimplat-eureka

spring.application.name=claimplat-eureka
eureka.instance.hostname=claimplat-eureka-01
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.enable-self-preservation=false

最后Run As运行。

 

PS:互相分享,点赞支持,感谢~若有问题还请评论指出,以便及时修正。

你可能感兴趣的:(Spring Cloud(一):使用Gradle搭建Eureka)