(2018年10月2日,從git上移到這裏)通過案例學習了基於Spring cloud進行web微服務項目的開發,乾貨很多。Spring cloud體系提供了完整的微服務開發的解決方案,整合了多個優秀組件,能夠很好的幫助我們提高開發效率。在此做以下總結。
Spring Cloud簡介
Spring Cloud是一個基於Spring Boot實現的微服務架構開發工具。它爲微服務架構中設計的配置管理,服務治理,斷路器,智能路由,微代理,控制總線,全局鎖,決策競選,分佈式會話和集羣狀態管理等操作提供了一種簡單的開發方式。
Spring Cloud子項目
① Spring Cloud Config:配置管理工具,支持使用Git存儲配置內容,可以使用它實現應用配置的外部化存儲,並支持客戶端配置信息刷新,加密/解密配置內容等。
② Spring Cloud Netflix:核心組件,對多個Netflix OSS開源套件進行整合。
- Eureka:服務治理組件,包含服務註冊中心、服務註冊與發現機制的實現。
- Hystrix:容錯管理組件,實現斷路器模式,幫助服務依賴中出現的延遲和爲故障提供強大的容錯能力。
- Ribbon:客戶端負載均衡的服務調用組件。
- Feign:基於Ribbon和Hystrix的聲明式服務調用組件。
- Zuul:網關組件,提供智能路由、訪問過濾等功能。
- Archaius:外部化配置組件。
③ Spring Cloud Bus:事件、消息總線,用於傳播集羣中的狀態變化或事件,以觸發後續的處理,比如用來動態刷新配置。
④ Spring Cloud Cluster:針對ZooKeeper、Redis、Hazelcast、Consul的選舉算法和通用狀態模式的實現。
⑤ Spring Cloud Consul:服務發現與配置管理工具。
⑥ Spring Cloud Stream:通過Redis、Rabbit或者Kafka實現的消費微服務,可以通過簡單的聲明式模型來發送和接收消息。
⑦ Spring Cloud Security:安全工具包,提供在Zuul代理中對OAuth2客戶端請求的中繼器。
⑧ Spring Cloud Sleuth:Spring Cloud 應用的分佈式跟蹤實現,可以完美整合 Zipkin。
⑨ Spring Cloud ZooKeeper:基於ZooKeeper 的服務發現與配置管理組件。
⑩ Spring Cloud Starters:Spring Cloud 的基礎組件,它是基於Spring Boot 風格項目的基礎依賴模塊。
組件架構
實戰項目簡介(Springcloud_Sell)
本次實戰以Springboot+maven爲基礎進行快速配置,採用分佈式微服務架構下的一站式解決方案Spring Cloud,集成組件Eureka+Config+Zuul+Feign/Ribbon+Hystrix+RabbitMQ+Redis+Sleuth+ZipKin。
開發環境:JDK1.8+MySQL5.7 + IDEA
Git地址:https://gitee.com/jiayuan1234/Springcloud_Sell
項目模塊主要有:api-gateway(網關),client(客戶端),config(配置中心),erurka(服務註冊與發現中心),order(訂單模塊),product(商品模塊),user(用戶模塊)
項目包結構:
搭建服務註冊中心(eureka)
創建一個基礎的springboot工程,命名爲eureka,並在pom.xml中引入相關依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<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>
通過@EnableEurekaServer註解啓動一個服務註冊中心提供給其他應用進行註冊,通過springboot工程一鍵部署和啓動
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
application.yml配置如下,在默認配置下,該服務註冊中心也會將自己作爲客戶端來嘗試註冊它自己,所以我們可以選擇禁用它的客戶端註冊行爲,即 eureka.client.register-with-eureka: false
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: false
server:
enable-self-preservation: false
spring:
application:
name: eureka
server:
port: 8761
在完成如上配置後,啓動工程,訪問 http://localhost:8761/ ,顯示eureka註冊中心面板如下
註冊服務提供者(client)
完成了註冊中心的搭建,嘗試將一個springboot應用註冊到eureka的服務治理體系中去,搭建一個springboot應用並加入如下依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
client端配置文件如下:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
#instance:
# hostname: clientName
spring:
application:
name: client
server:
port: 8081
通過@EnableEurekaClient註解啓動一個eureka客戶端,提供服務,進行服務註冊,@EnableEurekaClient本身就是用@EnableDiscoveryClient來實現的
/**啓動類
*
* @author [email protected]
* 2018-10-02 15:05
* @version 1.0.0
*/
@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
在eureka註冊中心頁面刷新,此時client已經將服務註冊到eureka上
統一配置中心(config)
創建一個springboot應用,pom.xml依賴以上面客戶端相同,同樣將自己作爲一個客戶端服務,也是要註冊到eureka中去的,application.yml配置文件如下,將遠程Git地址和賬號密碼配置到文件中去,用於向遠程文件拉取相關配置。注意,配置中心拉取的配置會與本地已有配置進行合併,若相同則覆蓋。
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://gitee.com/jiayuan1234/config-repo
username: [email protected]
password: **********
basedir: D:\MyData\workspace\IDEA\SpringCloud_Sell\config\basedir
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
server:
port: 8082
遠程Git配置文件
啓動config項目,獲取配置文件內容
數據庫表結構:
-- 類目
create table `product_category` (
`category_id` int not null auto_increment,
`category_name` varchar(64) not null comment '類目名字',
`category_type` int not null comment '類目編號',
`create_time` timestamp not null default current_timestamp comment '創建時間',
`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改時間',
primary key (`category_id`),
unique key `uqe_category_type` (`category_type`)
);
INSERT INTO `product_category` (`category_id`, `category_name`, `category_type`, `create_time`, `update_time`)
VALUES
(1,'熱榜',11,'2017-03-28 16:40:22','2017-11-26 23:39:36'),
(2,'好吃的',22,'2017-03-14 17:38:46','2017-11-26 23:39:40');
-- 商品
create table `product_info` (
`product_id` varchar(32) not null,
`product_name` varchar(64) not null comment '商品名稱',
`product_price` decimal(8,2) not null comment '單價',
`product_stock` int not null comment '庫存',
`product_description` varchar(64) comment '描述',
`product_icon` varchar(512) comment '小圖',
`product_status` tinyint(3) DEFAULT '0' COMMENT '商品狀態,0正常1下架',
`category_type` int not null comment '類目編號',
`create_time` timestamp not null default current_timestamp comment '創建時間',
`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改時間',
primary key (`product_id`)
);
INSERT INTO `product_info` (`product_id`, `product_name`, `product_price`, `product_stock`, `product_description`, `product_icon`, `product_status`, `category_type`, `create_time`, `update_time`)
VALUES
('157875196366160022','皮蛋粥',0.01,39,'好吃的皮蛋粥','//fuss10.elemecdn.com/0/49/65d10ef215d3c770ebb2b5ea962a7jpeg.jpeg',0,1,'2017-03-28 19:39:15','2017-07-02 11:45:44'),
('157875227953464068','慕斯蛋糕',10.90,200,'美味爽口','//fuss10.elemecdn.com/9/93/91994e8456818dfe7b0bd95f10a50jpeg.jpeg',1,1,'2017-03-28 19:35:54','2017-04-21 10:05:57'),
('164103465734242707','蜜汁雞翅',0.02,982,'好吃','//fuss10.elemecdn.com/7/4a/f307f56216b03f067155aec8b124ejpeg.jpeg',0,1,'2017-03-30 17:11:56','2017-06-24 19:20:54');
-- 訂單
create table `order_master` (
`order_id` varchar(32) not null,
`buyer_name` varchar(32) not null comment '買家名字',
`buyer_phone` varchar(32) not null comment '買家電話',
`buyer_address` varchar(128) not null comment '買家地址',
`buyer_openid` varchar(64) not null comment '買家微信openid',
`order_amount` decimal(8,2) not null comment '訂單總金額',
`order_status` tinyint(3) not null default '0' comment '訂單狀態, 默認爲新下單',
`pay_status` tinyint(3) not null default '0' comment '支付狀態, 默認未支付',
`create_time` timestamp not null default current_timestamp comment '創建時間',
`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改時間',
primary key (`order_id`),
key `idx_buyer_openid` (`buyer_openid`)
);
-- 訂單商品
create table `order_detail` (
`detail_id` varchar(32) not null,
`order_id` varchar(32) not null,
`product_id` varchar(32) not null,
`product_name` varchar(64) not null comment '商品名稱',
`product_price` decimal(8,2) not null comment '當前價格,單位分',
`product_quantity` int not null comment '數量',
`product_icon` varchar(512) comment '小圖',
`create_time` timestamp not null default current_timestamp comment '創建時間',
`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改時間',
primary key (`detail_id`),
key `idx_order_id` (`order_id`)
);
-- 用戶
CREATE TABLE `user_info` (
`id` varchar(32) NOT NULL,
`username` varchar(32) DEFAULT '',
`password` varchar(32) DEFAULT '',
`openid` varchar(64) DEFAULT '' COMMENT '微信openid',
`role` tinyint(1) NOT NULL COMMENT '1買家2賣家',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間',
PRIMARY KEY (`id`)
);