學習微服務的服務註冊與發現——EurekaServer與EurekaClient

微服務項目通常會存在多個服務,但服務與服務之間的調用,聯調就用到了服務註冊與發現,spring cloud全家桶就包含了Eureka來實現這個功能。java

1.首先建立服務註冊中心 利用spring Initializr來建立項目,new Module->選中Spring Initializr->Nextweb

而後填寫項目名稱,以及maven項目仍是gradle 項目spring

最後選擇要用到何種依賴,此時選擇的服務發現的eureka-serverapp

到此項目就構建完成了,此時的build.gradle文件是這樣的:eclipse

buildscript {
	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.cloud:spring-cloud-starter-netflix-eureka-server')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

項目佈局是這樣的:maven

啓動主方法所在類EurekaServerApplication.javaspring-boot

package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

而後啓動一個服務註冊中心,在EurekaServerApplication類上面加一個註解@EnableEurekaServer;微服務

application.yml配置文件:佈局

server:
    port: 8791


spring:
  application:
    name: eureka-server


eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false   #由於默認狀況下一個eureka-server也是一個eureka-client,因此設置註冊eureka爲false
    fetch-registry: false
    service-url:
      defalutZone: http://localhost:8791/eureka

而後啓動該項目,訪問http://localhost:8791,此時標明尚未服務註冊進來fetch

 

2.建立服務提供者 eureka-client

build.gradle文件,增長了eureka-client依賴

buildscript {
	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   ,發現已經有服務註冊到服務註冊中心了。

相關文章
相關標籤/搜索