SpringCloud知識點分析之項目實戰2

承接SpringCloud知識點分析之項目實戰1,該章節主要是將springCloud中各個維度的組件應用到項目之中。

一、組件Eureka

   1.Eureka服務註冊與發現

      1.介紹

         Netflix在設計Eureka時遵守的就是AP原則,Eureka是Netflix的一個子模塊,也是核心模塊之一。Eureka是一個

         基於REST的服務,用於定位服務,以實現雲端中間層服務發現和故障轉移。服務註冊與發現對於微服務架構來

        說是非常重要的,有了服務發現與註冊,只需要使用服務的標識符就可以訪問到服務,而不需要修改服務調用的

        配置文件了,功能類似於dubbo的註冊中心,比如Zookeeper。

     2.原理

        Eureka採用了C-S的設計架構。Eureka Server作爲服務註冊功能的服務器,它是服務註冊中心。相當一家物業公

        司,所有的微服務就相當要入駐物業公司管理的產業園,需要在物業公司中註冊繳費才能入,如下圖:Eureka

        Server是物業公司,Service Provier是入駐的公司,Service Consumer是入駐公司中的員工。

    

    

     Eureka包含兩個組件:Eureka Server和Eureka Client

     Eureka Server:提供服務註冊服務,各個節點啓動之後,會在EurekaServer中進行註冊,這樣 Eureka Server中的

                             服務註冊表將會存儲所有可用服務節點的信息,服務節點的信息可以在界面中直觀的看到。

     Eureka Client:是一個Java客戶端,用於簡化Eureka Server的交互,客戶端同時也具備一個內置的、使用輪詢(

                            round-robin)負載算法的負載均衡器。在應用啓動之後,將會向Eureka Server發送心跳(默認週期)

                            爲30秒)。如果Eureka Server在多個心跳週期內沒有接收到某個節點的心跳,Eureka Server將會

                           從服務註冊表中把這個服務節點移除(默認90秒)。

   3.三大角色

                      EurekaServer提供服務註冊和發現

                      Service Provider服務提供方將自身註冊到Eureka,從而使服務消費方能夠找到。

                      Service Consumer服務消費方從Eureka獲取註冊服務列表,從而能夠消費服務。

2.步驟構建

  1.創建服務註冊中心

     新建服務註冊中心子工程Module:microservicecloud-eureka-7001

     pom.xml

                   需要引入cloud的一個新的技術組件基本由兩步:

                                  第一步:在pom中新增一個相關的maven座標,例如如下

                                                

     創建application.yml

     創建啓動類

     測試

   2.將已有的部門微服務註冊進eureka服務中心

      ①修改microservicecloud-provider-dept-8001

                pom.xml

                application.yml

                啓動類

 

二、創建

1.創建服務註冊中心

  1. 新建Module:microservicecloud-eureka-7001(同其他模塊)

   2.pom.xml

<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>
  <parent>
    <groupId>com.atguigu.springcloud</groupId>
    <artifactId>microservicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservicecloud-eureka-7001</artifactId>
  
   <dependencies>
          <!-- eureka-server服務端 -->
          <dependency>
	            <groupId>org.springframework.cloud</groupId>
	            <artifactId>spring.cloud-starter-eureka-server</artifactId>
          </dependency>
          <!-- 熱部署 -->
          <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>springloaded</artifactId>
          </dependency>
          <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-devtools</artifactId>
          </dependency>
  
  </dependencies>
</project>

    3.application.yml

       在src/main/source下新建application.yml

server:
  port: 7001
  
eureka:
  instance:
     hostname: localhost #eureka服務端的實例名稱
  client:
     register-with-eureka: false #false表示不向註冊中心註冊自己
     fetch-registry: false #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
     service-url:
         defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/   #設置與Eureka Server交互的地址查詢服務和註冊服務都需要依賴這個地址

    4.啓動類

       在com.atguigu.springcloud下新建EurekaServer7001_app

       在啓動類上面,標註的啓動該新組件技術的相關注解標籤@enableEurekaServer

package com.atguigu.springcloud;

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

@SpringBootApplication
@EnableEurekaServer  //EurekaServer服務端啓動類,接受其他微服務註冊進來
public class EurekaServer7001_app {
	public static void main(String[] args) {
		SpringApplication.run(EurekaServer7001_app.class, args);
		
	}
	
}

    5.測試

      、服務註冊

2.服務註冊

    1.修改微服務消費者

        在微服務消費工程中的pom.xml中加入以下依賴信息:

<!-- 將微服務provider註冊進eureka -->
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
 <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

        application.yml中增加一下信息:

eureka:   #前方無空格
  client: #客戶端註冊進eureka服務列表內
    service-url:
      defalutZone: http//localhost://7001/eureka          #該註冊地址已經在服務註冊中心Module的yml文件中提供了

        在啓動類中加上@EnableEurekaClient,表示本服務會自動註冊進eureka服務中

       

    2.測試

       

        

 

     

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