目录结构:
build.gradle文件:
// buildscript 代码块中脚本优先执行
buildscript {
// ext 用于定义动态属性
ext {
springBootVersion = '2.0.0.M3'
}
// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
// mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
// 依赖关系
dependencies {
// classpath 声明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
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'
// 指定了生成的编译文件的版本,默认是打成了 jar 包
group = 'com.waylau.spring.cloud'
version = '1.0.0'
// 指定编译 .java 文件的 JDK 版本
sourceCompatibility = 1.8
// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
//mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
ext {
springCloudVersion = 'Finchley.M2'
}
// 依赖关系
dependencies {
// Eureka Client
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
// 该依赖用于测试阶段
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
application.properties文件:
spring.application.name: micro-weather-eureka-client
//这里就是上篇构建的eureka服务的地址:
eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
server.port=7071
启动类Application :
package com.waylau.spring.cloud.weather;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动之后,再访问上篇的项目链接:
http://localhost:8761/
这里就能检测到该客户端。