微服務分散式雲架構spring cloud - commonservice-eureka 專案構建過程

it小飛俠的微博發表於2019-03-01

我們針對於HongHu cloud的eureka專案做以下構建,整個構建的過程很簡單,我會將每一步都構建過程記錄下來,希望可以幫助到大家:願意瞭解原始碼的朋友直接求求交流分享技術一七九一七四三三八零

  1. 建立一個名為particle-common-eureka的maven專案,繼承particle-commonservice,具體的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> 
   
    <parent> 
        <groupId>com.ml.honghu</groupId> 
        <artifactId>particle-commonservice</artifactId> 
        <version>0.0.1-SNAPSHOT</version> 
    </parent> 
   
    <artifactId>particle-commonservice-eureka</artifactId> 
    <packaging>jar</packaging> 
   
    <name>particle-commonservice-eureka</name> 
    <description>particle-commonservice project for Spring Boot</description> 
   
    <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-security</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-devtools</artifactId> 
        </dependency> 
           
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-test</artifactId> 
            <scope>test</scope> 
        </dependency> 
   
    </dependencies> 
   
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
                <executions> 
                    <execution> 
                        <id>1</id> 
                        <goals> 
                            <goal>repackage</goal> 
                        </goals> 
                    </execution> 
                    <execution> 
                        <id>2</id> 
                        <goals> 
                            <goal>build-info</goal> 
                        </goals> 
                    </execution> 
                </executions> 
                <configuration> 
                    <executable>true</executable> 
                </configuration> 
                   
            </plugin> 
        </plugins> 
    </build> 
</project> 
複製程式碼
  1. 在啟動類入口引用eureka的相關配置,程式碼如下:
package com.ml.honghu; 
   
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 
   
@EnableEurekaServer 
@SpringBootApplication 
public class ServiceApplication { 
   
    public static void main(String[] args) { 
        SpringApplication.run(ServiceApplication.class, args); 
    } 
} 
複製程式碼
  1. 配置application.yml檔案
# server (eureka 預設埠為:8761) 
server: 
  port: 8761 
   
# spring 
spring: 
  application: 
    name: particle-commonservice-erueka 
   
# eureka 
eureka:  
  client:  
    # 是否註冊到eureka 
    register-with-eureka: true 
    # 是否從eureka獲取註冊資訊 
    fetch-registry: false 
    availability-zones:  
      honghu: honghuZone 
    service-url:  
      honghuZone: http://honghu:123456@localhost:8761/eureka/ 
      defaultZone: http://honghu:123456@localhost:8761/eureka/ 
  instance: 
    prefer-ip-address: true 
    hostname: localhost 
    metadataMap: 
      zone: honghuZone 
      user: ${security.user.name} 
      password: {security.user.password} 
         
  # 指定環境 
  environment: dev 
  #指定資料中心 
  datacenter: honghu 
  # 關閉自我保護模式 
  server:  
    enable-self-preservation: false 
  #設定清理無效節點的時間間隔,預設60000,即是60s 
    eviction-interval-timer-in-ms: 60000 
   
# 服務認證 
security:  
  basic:  
    enabled: true 
  user:  
    name: honghu 
    password: 123456 
   
management: 
  security: 
    enabled: false 
複製程式碼
  1. 增加專案的log機制和打包執行機制(後面我們會詳細編寫針對於Linux Centos下的打包部署機制)
  2. 自此整個專案部署完成,通過手動方式進行Run As --> Spring Boot App,執行結果如下: 控制檯執行結果:
    在這裡插入圖片描述
    控制檯執行效果:
    在這裡插入圖片描述
    在這裡插入圖片描述

相關文章