ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
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'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
springCloudVersion = 'Finchley.SR1'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
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}"
}
}
啟動類EurekaClientSayHiApplication.java文件
package com.example.eurekaclientsayhi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientSayHiApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientSayHiApplication.class, args);
}
@RequestMapping("/hi")
public String sayHi(){
return "hi";
}
}
application.yml文件
server:
port: 8792
spring:
application:
name: eureka-client-say-hi
eureka:
client:
service-url:
defaultZone: http://localhost:8791/eureka/ #eureka-server的地址
啟動該eureka-client項目:
訪問http://localhost:8792/hi
此時刷新一下地址:http://localhost:8791 ,發現已經有服務註冊到服務註冊中心了。
閱讀更多 JAVA技術開發 的文章