微服務之springCloud和docker-Eureka(一)

  1.  什麼是Spring Cloud?

Spring提供了一系列工具,可以幫助開發人員迅速搭建分佈式系統中的公共組件(比如:配置管理,服務發現,斷路器,智能路由,微代理,控制總線,一次性令牌,全局鎖,主節點選舉, 分佈式session, 集羣狀態)。協調分佈式環境中各個系統,爲各類服務提供模板性配置。使用Spring Cloud, 開發人員可以搭建實現了這些樣板的應用,並且在任何分佈式環境下都能工作得非常好,小到筆記本電腦, 大到數據中心和雲平臺。

Spring Cloud官網的定義比較抽象,我們可以從簡單的東西開始。Spring Cloud是基於Spring Boot的, 最適合用於管理Spring Boot創建的各個微服務應用。要管理分佈式環境下的各個Spring Boot微服務,必然存在服務的註冊問題。所以我們先從服務的註冊談起。既然是註冊,必然有個管理註冊中心的服務器,各個在Spring Cloud管理下的Spring Boot應用就是需要註冊的client

Spring Cloud使用erureka server,  然後所有需要訪問配置文件的應用都作爲一個erureka client註冊上去。eureka是一個高可用的組件,它沒有後端緩存,每一個實例註冊之後需要向註冊中心發送心跳,在默認情況下erureka server也是一個eureka client ,必須要指定一個 server。

  1.  創建Eureka Server

1).創建一個Maven工程helloworld.eureka.server, pom.xml內容如下:

 pom.xml

 2). 用Spring Boot創建一個服務類EurekaServerApplication,需要一個註解@EnableEurekaServer加在springboot工程的啓動類上

 1 package springcloud.helloworld.eureka.server;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 6 
 7 @EnableEurekaServer
 8 @SpringBootApplication
 9 public class EurekaServerApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(EurekaServerApplication.class, args);
13     }
14 }

3).eureka server的配置文件appication.yml,其中registerWithEureka:false和fetchRegistry:false表明自己是一個eureka server

1 server:
 2    port: 8761
 3
 4 eureka:
 5    instance:
 6        hostname: localhost
 7    client:
 8        registerWithEureka: false
 9        fetchRegistry: false
10        serviceUrl:
11            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4) eureka server的工程結構如下
微服務之springCloud和docker-Eureka(一)

5)啓動eureka server,然後訪問http://localhost:8761, 界面如下, "No instances available" 表示無client註冊
微服務之springCloud和docker-Eureka(一)

  1.  創建Eureka Client

1). 創建一個Maven工程helloworld.eureka.client, pom.xml內容如下:

 pom.xml

2).  創建主類EurekaClientApplication

使用@EnableEurekaClient註解表明是client

 1 package springcloud.helloworld.eureka.client;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestParam;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 @SpringBootApplication
12 @EnableEurekaClient
13 @RestController
14 
15 public class EurekaClientApplication {
16 
17     public static void main(String[] args) {
18         SpringApplication.run(EurekaClientApplication.class, args);
19     }
20 
21     @Value("${server.port}")
22     String port;
23     @RequestMapping("/")
24     public String home() {
25         return "hello world from port " + port;26     }
27 
28 }

3) eureka client的配置文件appication.yml

1 eureka:
2     client:
3         serviceUrl:
4             defaultZone: http://localhost:8761/eureka/
5 server:
6     port: 8762
7 spring:
8     application:
9         name: service-helloworld

4). Client啓動後, 可以訪問http://localhost:8762

微服務之springCloud和docker-Eureka(一)

5). 再次訪問服務器端口, 可以看到Service Helloworld已經自動註冊到之前的server中

微服務之springCloud和docker-Eureka(一)

整體架構如下:

微服務之springCloud和docker-Eureka(一)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章