java
web
spring
apache
tomcat
服務器
網絡
架構
app
負載均衡
建立好的目錄層次結構:
pom文件:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>custom-server</groupId> 6 <artifactId>custom-server</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <!-- 引入spring boot的依賴 --> 9 <parent> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-parent</artifactId> 12 <version>2.0.7.RELEASE</version> 13 </parent> 14 <dependencies> 15 <!-- Eureka-server依賴 --> 16 <dependency> 17 <groupId>org.springframework.cloud</groupId> 18 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 19 </dependency> 20 <!-- 密碼配置依賴 --> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-security</artifactId> 24 </dependency> 25 </dependencies> 26 <!-- 引入spring cloud的依賴,不能少,主要用來管理Spring Cloud生態各組件的版本 --> 27 <dependencyManagement> 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-dependencies</artifactId> 32 <version>Finchley.SR2</version> 33 <type>pom</type> 34 <scope>import</scope> 35 </dependency> 36 </dependencies> 37 </dependencyManagement> 38 39 <!-- 添加spring-boot的maven插件,不能少,打jar包時得用 --> 40 <build> 41 <plugins> 42 <plugin> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-maven-plugin</artifactId> 45 </plugin> 46 </plugins> 47 </build> 48 49 </project>
這裏使用的是Finchley.SR2版本,這是在網上找了許多後選擇了一個相對較新的版本。
security庫不用理會,後面寫註冊中心時會有說明,其餘spring-boot、eureka、spring-cloud都是必須的庫
在resources編寫application.yml配置文件:
server:
port: 8080
spring:
eureka:
instance:
hostname: localhost
client:
# 是否要註冊到其餘Eureka Server實例
register-with-eureka: false
# 是否要從其餘Eureka Server實例獲取數據
fetch-registry: false
service-url:
# 下面倆種方式隨便一種均可以,可是後綴【eureka】必不可少
#defaultZone: http://{hostname}:{server.port}/eureka/
defaultZone: http://localhost:8080/eureka/
spring.security.user.name跟password先不用理會,後面講述註冊中心會有提到。
建包編寫入口類(入口類必須放在頂級的包下面,不然會沒法掃描自動注入):
package com.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication // spring-boot啓動註解 @EnableEurekaServer // eureka組件服務註解 public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } }
編寫好後的註冊中心總體結構:
運行ServerApplication後,訪問localhost:8080若是看到下面畫面就說明註冊中心搭建好了:
紅色字體先不用理會,如今沒有使用帳號跟密碼,任何服務都能註冊到註冊中心,下面建立一個服務註冊到註冊中心。
2、搭建一個服務註冊到註冊中心:
一樣建立一個名爲custom-client的maven-project
pom文件:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>custom-client</groupId> 6 <artifactId>custom-client</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <!-- 引入spring boot的依賴 --> 9 <parent> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-parent</artifactId> 12 <version>2.0.7.RELEASE</version> 13 </parent> 14 <properties> 15 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 16 <java.version>1.8</java.version> 17 </properties> 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-starter-web</artifactId> 22 </dependency> 23 <!-- eureka服務組件 --> 24 <dependency> 25 <groupId>org.springframework.cloud</groupId> 26 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 27 </dependency> 28 </dependencies> 29 <!-- 引入spring cloud的依賴,不能少,主要用來管理Spring Cloud生態各組件的版本 --> 30 <dependencyManagement> 31 <dependencies> 32 <dependency> 33 <groupId>org.springframework.cloud</groupId> 34 <artifactId>spring-cloud-dependencies</artifactId> 35 <version>Finchley.SR2</version> 36 <type>pom</type> 37 <scope>import</scope> 38 </dependency> 39 </dependencies> 40 </dependencyManagement> 41 42 <!-- 添加spring-boot的maven插件,不能少,打jar包時得用 --> 43 <build> 44 <plugins> 45 <plugin> 46 <groupId>org.springframework.boot</groupId> 47 <artifactId>spring-boot-maven-plugin</artifactId> 48 </plugin> 49 </plugins> 50 </build> 51 </project>
application.yml:
1 server: 2 port: 8001 3 spring: 4 application: 5 # 指定註冊到eureka server上的服務名稱 6 name: custom-client 7 eureka: 8 client: 9 service-url: 10 # 指定eureka server通訊地址,注意/eureka/小尾巴不能少 11 defaultZone: http://localhost:8080/eureka/ 12 instance: 13 # 是否註冊IP到eureka server,如不指定或設爲false,那就會註冊主機名到eureka server 14 prefer-ip-address: true
*Application.java:
1 package com.client; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.web.client.RestTemplate; 7 8 @SpringBootApplication 9 @EnableEurekaClient // eurekaClient註解 10 public class UserClientApplication { 11 12 13 public static void main(String[] args) { 14 SpringApplication.run(UserClientApplication.class, args); 15 } 16 }
編寫完成好啓動UserClientApplication類,而後訪問註冊中心,查看服務是否註冊到了中心,訪問localhost:8080:
圖上標註的就是剛纔啓動並註冊的服務,一個最基本的架構已經完成了,後面陸續添加其餘微服務的基礎篇章,你們一塊兒學習、一塊兒進步。