SpringCloud Eureka

使用SpringCloud搭建Eureka服務

楊凱傑 2018/04/30 09:00:00
7052

使用SpringCloud搭建Eureka服務


簡介

本文將介紹如何使用SpringCloud搭建Eureka-Server及Eureka-Client實現簡單的微服務調用

作者

楊凱傑


1.Eureka簡介

Eureka是Netflix開源的一個RESTful服務,包括Server和Client兩部分,在Spring Cloud子項目Spring Cloud Netflix中。
Eureka服務器是用來當作一個服務註冊中心。Eureka客戶端是一個java客戶端,用來簡化與服務器的交互、作為輪詢負載均衡器,並提供服務的故障切換支持。

2.開始前準備

  • IntelliJ IDEA 
  • JDK1.8
  • Maven
  • SpringBoot版本:1.5.10.RELEASE

3.撰寫程式

(1)搭建Server端

創建一個名稱為eureka-server的Module
pom.xml如下:
 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.tp</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka-server</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR2</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

 接者寫一個啟動類,其中@EnableEurekaServer聲明這是一個Eureka服務器
 
package com.tp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
 最後寫配置文件application.yml,這邊將port設為8888
server:
port: 8888
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
 運行EurekaServerApplication.java,成功後打開瀏覽器,輸入localhost:8888,出現下圖及表示已成功啟動服務

(2)搭建Client端(服務提供者)

創建一個名稱為service-provider-01的Module當作服務提供者
pom.xml如下:
 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.tp</groupId>
<artifactId>service-provider-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka-server</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR2</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
接這寫啟動類,其中@EnableEurekaClient聲明這是一個Eureka客戶端
 
package com.tp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ServiceProvider01Application {

public static void main(String[] args) {
SpringApplication.run(ServiceProvider01Application.class, args);
}
}
 application.yml :
 
eureka:
client:
serviceUrl:
# 將服務註冊到eureka-server
defaultZone: http://localhost:8888/eureka/
server:
port: 8761
spring:
application:
name: service-provider
 編寫一個TestController
 
package com.tp.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

@GetMapping("test")
public String test() {
return "test";
}
}
   運行ServiceProvider01Application.java,成功後打開瀏覽器,輸入localhost:8888,可發現該服務已被註冊

(3)搭建Client端(服務調用者)

創建一個名稱為service-invoker的Module當作服務調用者
pom.xml同service-provider-01
 
application.yml:
 
eureka:
client:
serviceUrl:
# 將服務註冊到eureka-server
defaultZone: http://localhost:8888/eureka/
server:
port: 8762
spring:
application:
name: service-invoker
 啟動類:
 
package com.tp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class ServiceInvokerApplication {

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

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
 
 編寫一個Controller:
 
package com.tp.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class InvokerController {

@Autowired
private RestTemplate restTemplate;

@GetMapping("router")
public String router() {
//這邊會根據名稱(service-provider)去eureka-server取得對應的URL
String url = "http://service-provider/test";
return restTemplate.getForObject(url, String.class);
}

}
  運行ServiceInvokerApplication .java,成功後打開瀏覽器,輸入localhost:8888,可發現該服務已被註冊
 
最後輸入 http://localhost:8762/router  可看見成功返回test字串
 整個流程架構:
    1.先啟動eureka-server
    2.啟動service-provider-01,此時會向eureka-server註冊一個服務
    3.啟動service-invoker,此時會向eureka-server註冊一個服務
    4.瀏覽器輸入http://localhost:8762/router 調用service-invoker
    5. service-invoker會跟eureka-server獲取service-provider的資訊,接著調用 service-provider-01中的api
楊凱傑