SpringCloud教程:服務治理Eureka
Spring Cloud通過為Eureka增加了Spring Boot風格的自動化配置,只需要通過簡單引入依賴和註解配置就能讓Spring Boot構建的微服務輕鬆地與Eureka服務治理體系進行整合。
使用示例
搭建服務註冊中心
使用Gradle
建立一個基礎的SpringBoot工程,命名為eureka-server,eureka-service/build.gradle
檔案內容如下:
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
註解即可。程式碼如下:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class,args);
}
}
複製程式碼
配置檔案application.properties
內容如下:
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資訊面板。
註冊服務提供者
接下來建立一個命名為hello-service服務提供者,hello-service/build.gradle檔案內容如下:
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,程式碼如下:
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class,args);
}
}
複製程式碼
配置檔案application.properties
內容如下:
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
和服務提供者相同。
@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,如下圖所示: