微服务的服务注册与发现—EurekaServer与EurekaClient

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

微服务的服务注册与发现—EurekaServer与EurekaClient

此时刷新一下地址:http://localhost:8791 ,发现已经有服务注册到服务注册中心了。

微服务的服务注册与发现—EurekaServer与EurekaClient


分享到:


相關文章: