springboot整合eureka

服務端

1.maven依賴

  注意springboot和springcloud的版本對應

   
  <!--springboot的版本和springcloud的版本一定要對應,否則會報錯 -->

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



<!-- 管理springcloud依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


<!-- eureka server依賴 -->
     <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
     </dependency>

 

2.application.yml文件配置

###服務端口號
server:
  port: 8100
###eureka 基本信息配置
eureka:
  server:
    # 測試時關閉自我保護機制,保證不可用服務及時踢出
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000       #生產環境下一般默認90s
  instance:
    ###註冊到eurekaip地址
    hostname: 62.234.109.11
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    ###因爲自己是爲註冊中心,不需要自己註冊自己
    register-with-eureka: false
    ###因爲自己是爲註冊中心,不需要檢索服務
    fetch-registry: false

 

3.啓動類加上@EnableEurekaServer

 

4.啓動後訪問:http://62.234.109.11:8100 可以看到如下界面

 

 

客戶端

1.maven依賴

 

   
  <!--springboot的版本和springcloud的版本一定要對應,否則會報錯 -->

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



<!-- 管理springcloud依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


 <!--引入eureka客戶端依賴     -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

 

2.application.yml文件配置


#spring相關配置
spring:
  application:
    #服務名字(註冊到註冊中心)
    name: consumer

eureka:
  # 心跳檢測檢測與續約時間
  # 測試時將值設置設置小些,保證服務關閉後註冊中心能及時踢出服務
  instance:
    ###Eureka客戶端向服務端發送心跳的時間間隔,單位爲秒(客戶端告訴服務端自己會按照該規則)
    lease-renewal-interval-in-seconds: 1
    ####Eureka服務端在收到最後一次心跳之後等待的時間上限,單位爲秒,超過則剔除(客戶端告訴服務端按照此規則等待自己)
    lease-expiration-duration-in-seconds: 2        #生產環境下一般默認90s
  client:
    #是否需要從註冊中心獲取信息
    fetch-registry: true
    #需要註冊服務到服務中心
    register-with-eureka: true
    service-url:
      defaultZone: http://62.234.109.11:8100/eureka

 

3.啓動類加上@EnableEurekaClient

 

 

4.啓動項目後,再去訪問eureka服務端,可以看到多了一條服務

 

5.服務之間的調用,可以通過RestTemplate 或者feign客戶端(現在常用方式)來實現,後續會寫到。

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