1. 程式人生 > >Spring Cloud Eureka--服務發現

Spring Cloud Eureka--服務發現

一、Spring Cloud 

    Spring Cloud 為開發者提供了在分散式系統(如配置管理、服務發現、斷路器、智慧路由、微代理、控制匯流排、一次性 Token、全域性鎖、決策競選、分散式會話和叢集狀態)操作的開發工具。最關鍵的是它足夠簡單,一般的開發人員只需要幾天時間就可以學會它的基本用法。

    簡言之,spring cloud 就是一堆微服務框架的一個集合。

    微服務框架離不開服務治理-註冊與發現、智慧路由負載均衡等主題。Spring Cloud 就集成了這一圈開發微服務必不可少的框架,整合成一套名為spring cloud,致力於讓開發人員更快、更簡單的面向服務開發。

    所以spring cloud包含了多個子專案,每個專案都是針對分散式服務開發而生的開源產品,如Netflix公司貢獻的Netflix Eureka、Ribbon、Feign、Hystrix、Zuul都被spring 收納成為spring cloud 重要組成部分,發揮著各自不同的重要作用。

    下面筆者就一一介紹spring cloud中5大主要核心元件,江湖人稱“spring cloud 五大神獸”。

二、微服務架構

    簡單的說,微服務架構就是將一個完整的應用從資料儲存開始垂直拆分成多個不同的服務,每個服務都能獨立部署、獨立維護、獨立擴充套件,服務與服務間通過諸如RESTful API的方式互相呼叫。

三、Eureka 服務註冊與發現

    使用類似dubbo等微服務架構開發應用,離不開服務的註冊與發現。spring cloud Eureka (服務註冊與發現中心)在spring cloud大家庭中就扮演了服務註冊與發現的角色。

    這裡先演示一個Eureka demo,直觀的看看Eureka服務發現技能。

建立Eureka服務註冊中心

1、引入pom依賴

<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.4.0.RELEASE</version>
  	</parent>

	<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>Camden.SR2</version>
	        <type>pom</type>
	        <scope>import</scope>
	      </dependency>
	    </dependencies>
	  </dependencyManagement>
2、新增application.properties配置檔案
server.port=1111

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
3、編寫服務註冊中心
@EnableEurekaServer
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}
執行main方法,訪問http://localhost:1111/,顯示如下


這是spring cloud Eureka的服務端面板,可反饋服務發現、註冊例項情況;系統當前資源使用情況。看到這個介面就表示Eureka server已經正常啟動。那接下來就開始client客戶端,即服務的編寫,將每個服務註冊到這個server中心。

建立Eureka服務提供方

1、引入pom檔案

	<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.3.5.RELEASE</version>
	    <relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<dependencies>
	    <dependency>
	        <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-test</artifactId>
	    <scope>test</scope>
	    </dependency>
	
	    <dependency>
	        <groupId>org.springframework.cloud</groupId>
	  		<artifactId>spring-cloud-starter-eureka</artifactId>
	    </dependency>
	</dependencies>
	
	<dependencyManagement>
	    <dependencies>
	        <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-dependencies</artifactId>
	        <version>Brixton.RELEASE</version>
	        <type>pom</type>
	        <scope>import</scope>
	    </dependency>
	    </dependencies>
	</dependencyManagement>
2、新增application.properties配置檔案
spring.application.name=compute-service

#server.port=2222
server.port=2223

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
3、編寫服務類
@RestController
	public class ComputeController {

	    private final Logger logger = Logger.getLogger(getClass());

	    @Autowired
	    private DiscoveryClient client;

	    @RequestMapping(value = "/add" ,method = RequestMethod.GET)
	    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
	        ServiceInstance instance = client.getLocalServiceInstance();
	        Integer r = a + b;
	        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
	        return r;
	    }
	}
Springboot 啟動類
@EnableDiscoveryClient
@SpringBootApplication
public class ComputeServiceApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);
    }
}
執行該main方法後,重新整理http://localhost:1111/ 服務註冊面板,可見

當把服務提供工程的properties檔案修改服務埠為2222時,再啟動該main方法。可見2222、2223埠的兩個服務均註冊到了Eureka server端,顯示在了server面板頁面


四、Eureka原理分析

通過demo,as usual,spring使用@EnableEurekaServer和@EnableDiscoveryClient註解,暴露Eureka服務中心和Eureka服務客戶端,通過properties配置檔案,指定server連結、埠,client同樣匹配該服務連結,並指定自己的埠,啟動-註冊到EurekaServer並顯示到Eureka監控介面。


 五、總結

    Eureka Server提供服務註冊服務,各個服務節點啟動後,會在Eureka Server中進行註冊,這樣Eureka Server中就有了所有服務節點的資訊,並且Eureka有監控頁面,可以在頁面中直觀的看到所有註冊的服務的情況。

    同時Eureka有心跳機制,當某個節點服務在規定時間內沒有傳送心跳訊號時,Eureka會從服務登錄檔中把這個服務節點移除。       Eureka還提供了客戶端快取的機制,即使所有的Eureka Server都掛掉,客戶端仍可以利用快取中的資訊呼叫服務節點的服務。       Eureka一般配合Ribbon進行使用,Ribbon提供了客戶端負載均衡的功能,Ribbon利用從Eureka中讀取到的服務資訊,在呼叫服務節點提供的服務時,會合理的進行負載。 下面將會對spring cloud有一元件-Ribbon進行介紹