MicroService實現技術:java
用springBoot來建立單個服務,用SpringCloud來管理這些微服務。web
##SpringCloud的五大神獸spring
#1.註冊/服務發現——Netflix Eureka
管理服務器地址和ip的springboot
#2.客服端負載均衡——Netflix Ribbon\Feign
服務請求的分配服務器
#3.斷路器——Netflix Hystrix
對有故障的服務進行處理app
#4.服務網關——Netflix Zuul
微服務的統一入口。
#5.分佈式配置——Spring Cloud Config
對微服務的配置文件作同一管理負載均衡
SpringCloud入門步奏maven
1.註冊中心
用於管理微服務的地址
1.1當微服務能夠解決註冊的註冊表(IP、端口、服務名稱)分佈式
1.2從每一個微服務註冊微服務地址清單spring-boot
1.3使用心跳機制微服務合同:每N秒發送一個請求到註冊表,告訴註冊表,
我還活着,若是微服務掛,有心跳合同失敗,
註冊表微服務地址列表,其餘服務將更新地址列表
2.用戶管理微服務
3.訂單管理微服務
1.註冊中心配置
1.1先建立個普通的maven項目父級模塊,
在pom.xml裏配置公用的jar包管理
<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> </properties> <!-- 1.管理SpringBoot的jar包 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <!-- 2.管理spring-cloud的jar包 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 測試 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
1.2.建立註冊中心 --項目名:eureka-server-1000
在父級模塊中建立個spring-boot項目,也是maven項目,
可是要選中quickstart
1.3在eureka-server-1000模塊中的pom.xml添加依賴
<parent> <artifactId>spring-cloud-parent</artifactId> <groupId>spring-cloud-parent</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-server-1000</artifactId> <name>eureka-server-1000</name> <dependencies> <!-- 註冊/服務發現的依賴 有了它,才能獲取到全部微服務的ip,端口,服務名 有了它,其餘的微服務才能獲取到全部微服務的ip,端口,服務名清單 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- springboot-web的環境依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
1.4再建個resources配置文件夾,
在配置文件夾中建個application.yml文件
在yml文件中添加
server: port: 1000 #端口號 eureka: instance: hostname: localhost #主機 client: registerWithEureka: false #禁止註冊中心向本身註冊 fetchRegistry: false #禁止註冊中心拉取註冊地址清單 serviceUrl: #微服務向註冊中心註冊的地址 defaultZone: http://localhost:1000/eureka/
1.5在java中建個配置類添加
/** * @EnableEurekaServer * 啓動註冊中心,默認關閉 */ @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class,args); } }
啓動main方法能夠看見頁面
在網頁上輸入http://localhost:1000/顯示以下頁面說明配置成功
2..用戶管理微服務
模塊名:order-server-2000
和建立註冊模塊同樣但配置不同
2.1在pom.xml中添加依賴
<parent> <artifactId>spring-cloud-parent</artifactId> <groupId>spring-cloud-parent</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>order-server-2000</artifactId> <name>order-server-2000</name> <dependencies> <!-- 註冊的微服務控制器 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- springboot的web環境配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2.2在resources配置文件夾中新建個application.yml
在yml中添加配置
eureka: client: serviceUrl: #指定註冊中心的地址 defaultZone: http://localhost:1000/eureka/ instance: #是否顯示ip地址 prefer-ip-address: true server: #端口號 port: 2000 spring: application: #服務名 name: orders-server cloud: discovery: client: simple: local: service-id: orders-server:2000 #顯示的id
2.3在java文件夾中建個
OrdersServerApplication.java
添加以下代碼
@SpringBootApplication @RestController public class OrdersServerApplication { @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { SpringApplication.run(OrdersServerApplication.class); } }
啓動main方法能夠看見頁面
在網頁上輸入http://localhost:2000/頁面顯示
Hello world
說明配置成功
再重啓動註冊模塊的main方法
在網頁上輸入http://localhost:1000/頁面顯示以下就說明微服務配置成功
3.訂單管理微服務
項目名:user-server-3000
和建立註冊模塊同樣但配置不同
3.1在pom.xml中添加依賴
<parent> <artifactId>spring-cloud-parent</artifactId> <groupId>spring-cloud-parent</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>user-server-3000</artifactId> <name>user-server-3000</name> <dependencies> <!-- 註冊的微服務控制器 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- springboot的web環境配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
3.2在resources配置文件夾中新建個application.yml
在yml中添加配置
eureka: client: serviceUrl: defaultZone: http://localhost:1000/eureka/ instance: prefer-ip-address: true server: port: 3000 spring: application: name: user-server cloud: discovery: client: simple: local: service-id: user-server:3000
3.3在java文件夾中建個
OrdersServerApplication.java
添加以下代碼
/** * @EnableEurekaClient * 啓動微服務; * 默認開啓 */ @SpringBootApplication @RestController @EnableEurekaClient public class UserServerApplication { @RequestMapping("/") public String home() { return "Hello world2"; } public static void main(String[] args) { SpringApplication.run(UserServerApplication.class); } }
啓動main方法能夠看見頁面
在網頁上輸入http://localhost:3000/頁面顯示
Hello world2
說明配置成功
再重啓動註冊模塊的main方法
在網頁上輸入http://localhost:1000/頁面顯示以下就說明微服務配置成功