Spring Cloud經過爲Eureka增長了Spring Boot風格的自動化配置,只須要經過簡單引入依賴和註解配置就能讓Spring Boot構建的微服務輕鬆地與Eureka服務治理體系進行整合。java
使用Gradle
建立一個基礎的SpringBoot工程,命名爲eureka-server,eureka-service/build.gradle
文件內容以下:spring
buildscript {
ext {
springBootVersion = '2.1.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'eureka-service'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.SR2'
}
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
複製代碼
啓動一個服務註冊中心很是簡單,只需在Spring boot應用的Application類添加@EnableEurekaServer
註解便可。代碼以下:shell
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
複製代碼
配置文件application.properties
內容以下:app
server.port=8761
# 該應用爲註冊中心,所以不向註冊中心註冊本身
eureka.client.register-with-eureka=false
# 註冊中心不須要檢索服務
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
複製代碼
啓動應用並訪問http://localhost:8761/,能夠看到Eureka信息面板。eclipse
接下來建立一個命名爲hello-service服務提供者,hello-service/build.gradle文件內容以下:maven
buildscript {
ext {
springBootVersion = '2.1.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'com.blockmao'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://repo.spring.io/milestone" }
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.SR2'
}
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
複製代碼
使用@EnableEurekaClient
代表該應用爲Eureka Client,代碼以下:ide
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
複製代碼
配置文件application.properties
內容以下:spring-boot
spring.application.name=hello-service
server.port=8080
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
複製代碼
啓動應用並訪問http://localhost:8761/,能夠看到服務的註冊信息,以下所示:微服務
最後,建立一個命名爲hello-consumer
服務消息者,hello-consumer/build.gradle
和服務提供者相同。fetch
@EnableEurekaClient
@SpringBootApplication
public class ConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
@RestController
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@RequestMapping(value = "/hello-consumer",method = RequestMethod.GET)
public String helloConsumer(String msg){
return restTemplate.getForEntity("http://MSC-SITE-API/hello?msg="+msg,String.class).getBody();
}
}
複製代碼
配置文件application.properties
內容以下:
spring.application.name=hello-consumer
server.port=9090
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
複製代碼
啓動應用並訪問http://localhost:9090/hello-consumer?msg=world,以下圖所示:
Eureka Server
的高可用是將本身做爲服務向其餘服務註冊中心註冊本身,這樣就能夠造成一組相互註冊的服務註冊中心,以實現服務清單的互相同步,達到高可用的效果。在單節點的註冊中心基礎之上進行擴展,構建一個雙節點的服務註冊中心集羣。
首先修改一下/etc/hosts
文件,添加以下:
127.0.0.1 peer1 peer2
複製代碼
建立application-peer1.properties
,做爲peer1服務中心的配置,並將serviceUrl指向peer2:
spring.application.name=eureka-server
server.port=1111
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1112/eureka
複製代碼
建立application-peer2.properties
,做爲peer2服務中心的配置,並將serviceUrl指向peer1:
spring.application.name=eureka-server
server.port=1112
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka
複製代碼
經過spring.profiles.active
屬性來分別啓動peer1和peer2:
java -jar eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=peer2
複製代碼
訪問peer1的註冊中心http://localhost:1111,以下圖所示: