SpringCloud之Eureka(一)

目錄

服務治理

 Netflix Eureka

搭建服務註冊中心(單機模式)

 註冊服務提供者

服務治理

服務治理是微服務架構中非常重要的模塊,主要用來實現服務的自動註冊與發現。在最初構建微服務系統的時候服務可能並不多,可以通過簡單的靜態配置來維護服務間的相互調用,但是當系統功能越來越複雜,微應用也不斷增加,靜態配置已經不能起到很好的作用,服務間的調用關係越來越難以理清,並且集羣規模,服務位置,服務命名等都可能發生變化。而服務治理就是專門爲解決這些問題而產生的。

服務註冊:在服務治理模塊中,通常會有一個註冊中心,每個服務都會向註冊中心提供自己的主機地址,版本號,通信協議等信息,註冊中心按照服務名來組織服務清單。例如如果有兩個提供服務A的進程運行在192.168.0.1:9000和192.168.0.2:9001上,還有兩個提供服務B的進程運行在192.168.3.2:9000和192.168.0.4:9001上,那麼註冊中心就會維護一個類似以下的服務清單。並且註冊中心還會一直監測清單中的服務是否可用,若不可用就需要從服務清單中剔除。

服務名 位置
服務A 192.168.0.1:9000,192.168.0.2:9001
服務B 192.168.3.2:9000,192.168.0.4:9001

服務發現:當服務調用方想從服務提供方獲取服務時,它不能通過接口直接調用服務方的服務,因爲它不知道服務提供方的具體位置,因此服務調用方需要通過服務註冊中心,獲取服務清單。如上,如果服務C想調用服務A,C需要向註冊中心發起請求獲取服務A的可用位置192.168.0.1:9000,192.168.0.2:9001,然後服務C通過某種負載均衡策略來進行服務調用。

 Netflix Eureka

Spring Cloud Eureka,使用Netflix Eureka來實現服務註冊與發現,它既包含了服務端組件也包含客戶端組件,並且服務端和客戶端都是採用java編寫。

Eureka服務端:我們也稱爲服務註冊中心。支持高可用配置。如果Eureka以集羣模式部署,當集羣中有分片出現故障時,那麼Eureka就轉入自我保護模式。它允許在分片故障期間繼續提供服務的發現和註冊,當故障分片恢復運行時,集羣中的其他分片會把狀態同步給故障恢復的分片。

Eureka客戶端:主要是服務的註冊與發現。客戶端向註冊中心註冊自身所提供的服務信息,並週期性發送心跳。同時也能從服務端查詢當前註冊的服務信息,並把它們緩存到本地並週期性的刷新服務信息。

搭建服務註冊中心(單機模式)

  •  1.引入依賴
!--設置springcloud版本-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 <!--服務治理-->
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka-server</artifactId>
 </dependency>
  • 2.使用@EnableEurekaServer

 通過@EnableEurekaServer註解啓動一個註冊中心,只需在一個普通的Spring Boot應用中添加這個註解就能開啓此功能。

@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {

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

在默認設置下,該服務註冊中心也會將自己註冊爲客戶端,所以需要設置禁用這種行爲,可以在application.properties中增加如下配置:

#Eureka-Server配置
eureka.instance.hostname=server1
#註冊中心維護服務實例,設置爲false不需要檢索服務
eureka.client.fetch-registry=false
#註冊中心不需要註冊自己設置爲false
eureka.client.register-with-eureka=false
server.port=8089
#高可用時,此時不能使用localhost,此處server2需要在host文件中設置
eureka.client.serviceUrl.defaultZone=http://server1:8090/eureka/

完成上面的配置後,啓動應用並訪問 http://localhost:8089/。可以看到如下所示頁面。當前的 Instances currently registered with Eureka是空的,說明這個註冊中心還沒有註冊任何服務。

 註冊服務提供者

  • 1.引入依賴
 <!--設置springcloud版本-->
    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
   </dependencyManagement>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--服務治理-->
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
</dependencies>
  • 2.編寫一個服務方法
@RestController
public class TempController {
    private final Logger logger = Logger.getLogger(getClass());
    @Autowired
    private DiscoveryClient client;
        @RequestMapping("/get")
        public String temp(){
            ServiceInstance localServiceInstance = client.getLocalServiceInstance();
            logger.info(localServiceInstance.getHost()+":"+localServiceInstance.getPort()+
":"+localServiceInstance.getServiceId());
            return "hello world";
        }
}
  • 3.使用@EnableDiscoveryClient註解,激活Eureka中的DiscoveryClient實現,實現上面Controller的信息輸出。
@EnableDiscoveryClient
@SpringBootApplication
//因爲我的啓動類沒有放在根目錄下面所以不能自動掃描組件,這裏手動掃描一下
@ComponentScan(basePackages = {"com.example.controller"})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • 4.配置properties
spring.application.name=client
server.port = 1111
#指定服務註冊地址
eureka.client.serviceUrl.defaultZone  = http://localhost:8089/eureka/
  • 5.啓動服務

啓動之後可以看到註冊中心控制檯輸出以下信息,表示該服務註冊成功。

刷新註冊中心頁面,可以看到該服務的註冊信息:

 通過訪問http://localhost:1111/get直接向該服務發起請求,在控制檯可以看到如下輸出:

 這些輸出內容就是之前在Controller中注入的DiscoveryClient對象從服務註冊中心獲取的服務相關信息。

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