微服務項目搭建連續劇 -- 第二集:eureka註冊中心配置

   上一集我們已經有3個module,其中springcloud-eureka-service(下面簡稱:eureka)就是我們這集的重點扶貧對象

1.打開我們父項目(springcloud-20200321)的pom.xml文件導入以下依賴

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
        <mysql.version>5.1.47</mysql.version>
        <druid.version>1.1.16</druid.version>
        <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--springboot 2.2.2-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--springcloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--springcloud alibaba-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!--druid-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <optional>true</optional>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.打開eureka的pom.xml文件,導入以下依賴,這裏要注意,springboot和springcloud的版本直接是存在兼容關係的,所以這裏版本需要選取的時候需注意,如何確定版本請看文章末尾

<properties>
        <spring-cloud-netflix-version>2.2.1.RELEASE</spring-cloud-netflix-version>
        <spring-boot-dependencies-version>2.2.2.RELEASE</spring-boot-dependencies-version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>${spring-cloud-netflix-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot-dependencies-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>${spring-boot-dependencies-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>${spring-boot-dependencies-version}</version>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring-boot-dependencies-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

2.待我們的依賴所需的jar包下載完畢之後,在eureka的src/main/resources目錄下新建一個application.yml文件,當然有些人是application.properties文件,也是一樣的,推薦yml文件,閱讀性較好

3. 在文件中編寫內容:(溫馨提示:yml是一個格式很嚴格的文件,要注意一下我下面內容的對齊方式,每個  :   跟後面的內容都是有個空格的,建議手動填寫)

server:
  port: 7001  #訪問端口

eureka:
  instance:
    hostname: eurekaservice #eureka服務端實例名稱
  client:
    register-with-eureka: false  #false表示不將自己註冊到註冊中心
    fetch-registry: false         #false表示自己爲註冊中心
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka/   #註冊中心訪問地址  單機模式
  server:
    enable-self-preservation: false   #關閉eureka自我保護機制
    eviction-interval-timer-in-ms: 20000  #默認90秒

4. 至此applicaiton.yml文件配置結束,在eureka的src/main/java建一個我們的啓動類main.class,添加註解@SpringBootApplication和@EnableEurekaServer

5. 啓動我們剛剛新建的啓動類main.class後

6.訪問在application.yml配置文件中eureka.client.service-url.defaultZone後面的http://ip:port,比如我配置的是:http://127.0.0.1:7001(只需要IP和端口,去掉後面的/eureka),如果能夠看到如下頁面,則表示成功了

附註:

如何確定springcloud和springboot的版本對應關係?

1. 訪問地址:https://start.spring.io/actuator/info   在這個地址裏,你看到的是一個JSON串

2.我們將JSON串複製出來,用格式化工具將其進行格式化,這裏可以推薦一個在線個格式化工具:https://www.json.cn/#

格式化後,我們可以找到spring-cloud節點,比如我們這裏springboot的版本選擇的是2.2.2.RELEASE,那麼我們後面的springcloud版本將是Hoxton.SR3

 

 

 

 

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