1. 程式人生 > >不惑之年的硬體牛人轉到軟體自學之spring cloud:(一)如何搭建一個正確的eureka註冊中心(內有基礎spring boot專案)

不惑之年的硬體牛人轉到軟體自學之spring cloud:(一)如何搭建一個正確的eureka註冊中心(內有基礎spring boot專案)

        前言:筆者曾經有18年的硬體研發經驗,從(1)51微控制器到(2)FPGA到(3)嵌入式ARM(ARM9到CORTEX A9)全都研發設計過,產品從(1)B超的整機研發到(2)智慧家居系統到(3)無線電監測機到(4)平板電腦研發到(5)路燈智慧控制到(5)工業電腦均有涉及,從(1)普通的電子技術工程師到(2)副總工程師到(3)副總經理到(4)事業部總經理。。。目前已經步入不惑之年的我對於物聯網技術的熱衷,決定從硬體開始全面轉到物聯技術框架之一的spring cloud技術,把我的整個學習經歷和大家一起分享,也期待在之後有更多機會和大家一起合作,探討。

      今天是:2018年4月7日    研究主題:如何搭建一個正確的eureka註冊中心

      一、從網站: http://start.spring.io/   下載spring boot的基礎框架

      

            1、選擇“WEB”依賴:

                 

          2、形成工程:

                  

        3、將下載好的“demo.zip”解壓縮到我的工程目錄裡面,並改名為“cjb1”:

                   

      二、進入IDEA軟體的設定

              1、進入“setting”設定,主要目的在於使用阿里雲的下載設定,這樣會下載的相關依賴會快一些,相關這方面文件在  網上很多,請大家自行下載並設定:

             

           2、由於裡面“application.properties”檔案是空的,未進行任何配置,因此預設埠為“8080”這個是先跑一個基礎的spring boot,之後我們再進行eureka的編寫:

             (1)、我們先簡單的執行一下這個專案,並可以看到列印輸出在8080埠已經可以載入成功:

                    

                   

           (2)、以上雖然可以在本地8080埠可以跑起來,但不會有任何輸出,因為我們必須還要建一個controller,並指定路徑,輸出一個列印欄位才行:

                  新建一個class的類:cjbcongtroller

                         

                 程式碼內容如下:

package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class cjbcontroller {
    @RequestMapping("/hello")
    public String say(){
        return "cjb is good!";
}

}

         (3)OK,以上就是一個基礎的spring boot的簡單專案,繼續跑起來我們看一下結果,以上是見證奇蹟的時刻,在網頁上面輸入:127.0.0.1:8080/hello後將出現:

        

        以上專案已經成功!

     三、將以上cjb1專案重新複製一個eureka的專案,之前的那個專案作為後面註冊到eureka的服務,這個新建的eureka專案取名叫“cjb1-eureka”

      1、首先向“pom.xml”新增並更改如下內容:

<?xml version="1.0" encoding="UTF-8"?>
<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>

   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>demo</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.1.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
</parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <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-actuator</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>Brixton.SR5</version>
      <type>pom</type>
      <scope>import</scope>
   </dependency>
</dependencies>
</dependencyManagement>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>


</project>

2、向“application.properties”新增如下內容:

spring.application.name=eureka-server
server.port=1234
eureka.instance.hostname=localhost
# 關閉保護機制
#eureka.server.enable-self-preservation=false
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
logging.file=${spring.application.name}.log

註解:(1)、server.port為註冊中心的埠為1234;

         (2)、eureka.client.register-wiht-eureka:表示應用為註冊中心,由於不向註冊中心註冊自己,所以設定為false;

         (3)、eureka.client.fetch-registry:表示註冊中心去檢索服務,由於本身註冊中心的職責是維護服務例項,因此不需要去檢索,設定為false;

           3、向“DemoAppliation.class”中新增“@EnableEurekaServer”的註解,表示啟動一個服務註冊中心提供給其他應用進行對話。

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {

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

四、問題來了,會編譯出問題。經過研究,做如下更改:

 1、將“pom.xml”裡面的:

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.0.1.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

改為低版本“1.3.7.RELEASE”

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.3.7.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

2、去掉裡面的boot的test依賴,完整更改後版本如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>

   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>demo</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.3.7.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
</parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <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-actuator</artifactId>-->
      <!--</dependency>-->
</dependencies>

<dependencyManagement>
<dependencies>
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Brixton.SR5</version>
      <type>pom</type>
      <scope>import</scope>
   </dependency>
</dependencies>
</dependencyManagement>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>


</project>

3、更改“DemoApplication.class”裡面的"main"函式內容為,完整版本如下:

package com.example.demo;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {

   public static void main(String[] args) {
      new SpringApplicationBuilder(DemoApplication.class).web(true).run(args);
}

}

4、編譯執行,如下結果:

五,見證奇蹟的時刻出現了,在網址中輸入:127.0.0.1:1234 大工搞成!


六、如果需要完整程式碼的朋友,可以加入作者QQ群:智物聯的spring cloud,入群說明:spring cloud程式碼需求