前言
spring cloud netflix 是分佈式解決方案中的其中之一。
spring cloud netflix 系列技術棧的鏈接
spring cloud netflix 系列技術棧
但是2018年12月12日,Netflix公司宣佈spring cloud netflix 系列技術棧進入維護模式,項目進入維護模式就意味着不會在添加新的功能,到現在基本只有 spring-cloud-netflix-eureka-*和spring-cloud-netflix-concurrency-limits模塊還在更新。
我們以後的採用的分佈式解決方案也該不會在選擇spring cloud netflix了,但是在我的認知中,這是我最早接觸到的一套分佈式解決方案了。所以對spring cloud netflix做一個學習筆記。
創建統一的依賴管理的前置準備
-
創建文件夾(spring-cloud-netflix)來存放Netflix系列技術棧的各個服務
問:創建這個文件夾的原因?
答:將與Netflix技術棧的相關服務存放到該文件夾下,方便管理。 -
使用IntelliJ IDEA打開這個文件夾
採用Open的方式來打開文件夾
選擇spring-cloud-netflix文件夾並打開
創建統一的依賴管理
對於微服務項目來說,是按照功能,依賴maven來創建工程的。一個服務便會有一個pom文件,所以我們需要把每個pom中公共的依賴放到一個統一的服務中進行管理。這便是創建統一依賴管理的原因。
-
在intelliJ IDEA中創建spring-cloud-netflix-dependencies文件夾
選擇Directory
-
在spring-cloud-netflix-dependencies中創建pom文件
選擇file
這步報錯的原因是現在還沒有將pom.xml文件交由IntelliJ IDEA託管,下面的步驟會解決。
將依賴寫入pom文件中
<?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.baiyang</groupId>
<artifactId>spring-cloud-netflix-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- 表明這是一個pom依賴-->
<packaging>pom</packaging>
<!-- 補充信息 -->
<name>spring-cloud-netflix-dependencies</name>
<url>https://blog.csdn.net/weixin_41756573</url>
<inceptionYear>2019-Now</inceptionYear>
<!-- spring boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<properties>
<!-- Environment Settings -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring cloud Settings -->
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- Compiler 插件, 設定 JDK 版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!-- 打包 jar 文件時,配置 manifest 文件,加入 lib 包的 jar 依賴 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
<executions>
<execution>
<configuration>
<archive>
<manifest>
<!-- Add directory entries -->
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<!-- resource -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<!-- install -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<!-- clean -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<!-- ant 現在阿里巴巴的倉庫下載該插件有問題
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
</plugin>
-->
<!-- dependency -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- Java Document Generate -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- YUI Compressor (CSS/JS壓縮) -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<jswarn>false</jswarn>
<nosuffix>true</nosuffix>
<linebreakpos>30000</linebreakpos>
<force>true</force>
<includes>
<include>**/*.js</include>
<include>**/*.css</include>
</includes>
<excludes>
<exclude>**/*.min.js</exclude>
<exclude>**/*.min.css</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<!-- 資源文件配置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>aliyun-repos</id>
<name>Aliyun Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>sonatype-repos</id>
<name>Sonatype Repository</name>
<url>https://oss.sonatype.org/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>sonatype-repos-s</id>
<name>Sonatype Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-repos</id>
<name>Aliyun Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
- 將pom.xml交由IntelliJ IDEA 託管
刷新服務
此時的spring-cloud-netflix-dependencies是一個pom依賴的服務。
總結
其他項目的引用
<parent>
<groupId>com.baiyang</groupId>
<artifactId>spring-cloud-netflix-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- 路徑 -->
<relativePath>../spring-cloud-netflix-dependencies/pom.xml</relativePath>
</parent>
在其他項目中pom中使用 parent 標籤引用即可。
補充說明
查看spring boot版本的網址
spring boot 版本
其中CURRENT|GA表示正式發佈的版本;
SNAPSHOT表示快照版,隨時更新。
查看spring cloud netflix版本的網址
spring cloud netflix 版本