什么是Spring Cloud微服務架構
文章目錄
- 什么是Spring Cloud微服務架構
- 前言
- 一、什么是 Spring-Cloud?
- 二、提供的是一些快速構建微服務應用的工具
- 三、搭建微服務專案
- 專案總體結構介紹
- 1.創建微服務注冊中心Euerka專案工程
- 2.創建微服務訊息生產者(提供資料方)
- 3.創建微服務消費者(呼叫生產者服務方)
- 4.創建微服務網關Zull專案工程
- 4.啟動專案
- 四、補充問題(重點)
- 總結
前言
為什么學習微服務架構?學習微服務架構需要那些技術儲備?
1、在我們剛接觸的專案當中大部分都是單體架構(除了一線互聯網公司),單體結構的特點就是以滿足業務需求是第一位,技術實作其次所以業務量不是跟大比如一些公司的內部使用系統,設想如果用戶量、業務量很大呢單體架構很難支撐,所以我們要采用微服務架構解決高并發的問題,
2、技術儲備首先要學習Spring Boot框架(可查看我的文章SpringBoot篇),因為Spring Cloud架構是在Spring Boot就框架的基礎上進行開發和搭建,也可以理解為多個Spring Boot專案放在一起就是一個分布式Spring Cloud專案
提示:以下是本篇文章正文內容,下面案例可供參考
一、什么是 Spring-Cloud?
其實Spring Cloud是在Spring Boot基礎上構建的,用于快速構建分布式系統的通用模式的工具集,
他的特點:約定優于配置,
組件豐富,功能齊全,SpringCloud為微服務,列如Euerk架構提供了非常完整的支持,列如配置管理、服務發現、斷路器、微服務網關等,
二、提供的是一些快速構建微服務應用的工具
1、服務注冊發現
2、遠程服務呼叫
3、負載均衡
4、斷路器
5、分布式訊息
6、配置中心
7、鏈路監控
所以, spring cloud 提 供 了 一 些 解 決 這 類 問 題 的 工 具 ,
比 如 服 務 注 冊 提 供 了Eureka/Consoul/zookeeper;
遠程呼叫基于 RestTemplate 針對 http 協議呼叫的封裝;
負載均衡采用 Ribbon、斷路器采用 hystrix;
分布式訊息基于 kafka、rabbitMQ;
配置中心基于config;
鏈路監控基于 sleuth.
三、搭建微服務專案
專案總體結構介紹
1.創建微服務注冊中心Euerka專案工程
pom.xml添加依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
專案工程目錄結構圖

1.1、創建啟動類EurekaApp,如果是SpringBoot專案構建時會生成,也可自己創建,
注意@SpringBootApplication(含義:這是SpringBoot專案的啟動器) @EnableEurekaServer(含 義:這是網關服務的服務service)
代碼如下(示例):
@SpringBootApplication
@EnableEurekaServer
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class,args);
}
}
1.2、組態檔在resource檔案夾下創建application.yml組態檔,配置如下,
server:
port: 10000 #埠號
spring:
application:
name: eurekaapp #服務名
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10000/eureka
register-with-eureka: false #不注冊自己,串列當中看不到自己
fetch-registry: false #不注冊自己
2.創建微服務訊息生產者(提供資料方)
pom.xml添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--添加jsp的依賴-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!--添加mysql資料庫-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
專案工程目錄結構圖

2.1、創建啟動類ShengApp ,如果是SpringBoot專案構建時會生成,也可自己創建,
注意@SpringBootApplication(含義:這是SpringBoot專案的啟動器) @EnableEurekaClient(含 義:我方作為客戶端向網關服務注冊自己)
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableEurekaClient
public class ShengApp {
public static void main(String[] args) {
SpringApplication.run(ShengApp.class ,args);
}
2.2、組態檔在resource檔案夾下創建application.yml組態檔,配置如下,
spring:
application:
name: shengchanzhe #服務名稱
datasource:
username: root ##資料庫用戶,改成自己的
password: root ##資料庫密碼,改成自己的
url: jdbc:mysql://localhost:MySql/MySql?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
server:
port: 8083 #服埠號
eureka:
client:
service-url: # EurekaServer地址
defaultZone: http://127.0.0.1:10000/eureka
instance:
prefer-ip-address: true # 當呼叫getHostname獲取實體的,回傳ip而不是host名稱
ip-address: 127.0.0.1 # 指定自己的ip資訊,不指定的話會自己尋找
2.3專案訪問Controller層請求url
@RestController
public class HelloController {
@Autowired
private IgoodsQuery igoodsQuery;
@GetMapping("/hello")
public String hellow(){
return "hello world!!!8087第一個服務器";
}
}
3.創建微服務消費者(呼叫生產者服務方)
pom.xml添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
專案工程目錄結構圖

3.1、創建啟動類XiaoApp,如果是SpringBoot專案構建時會生成,也可自己創建,
注意@SpringBootApplication(含義:這是SpringBoot專案的啟動器) @EnableEurekaClient(含義:我方作為客戶端向網關服務注冊自己)
@EnableFeignClients(含義:使用Feign方式進行服務之間的通信)
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class XiaoApp {
public static void main(String[] args) {
SpringApplication.run(XiaoApp.class ,args);
}
}
3.2、組態檔在resource檔案夾下創建application.yml組態檔,配置如下,
spring:
application:
name: xiaofeizhe #服務名稱
server:
port: 8082 #埠號
eureka:
client:
service-url: # EurekaServer地址
defaultZone: http://127.0.0.1:10000/eureka
instance:
prefer-ip-address: true # 當呼叫getHostname獲取實體的,回傳ip而不是host名稱
ip-address: 127.0.0.1 # 指定自己的ip資訊,不指定的話會自己尋找
3.3撰寫消費者服務-------->呼叫訊息生產者服務Controller層
@RestController
public class HelloController {
@Autowired
HelloService helloService; //注入依賴
@GetMapping("/xhello")
public String hello(){
return helloService.hello();
}
}
3.4撰寫消費者服務-------->呼叫訊息生產者服務Service層
@Service
public class HelloService {
@Autowired
IHelloFeign helloDao;
public String hello(){
return helloDao.hello();
}
}
3.5撰寫消費者服務-------->呼叫訊息生產者服務dao(資料持久)層
@FeignClient注解實作被呼叫方的服務注入
//更優雅、靈活、方便的呼叫服務
@FeignClient("SHENGCHANZHE") #我們呼叫消費者的服務名稱
public interface IHelloFeign {
@GetMapping("/hello")
public String hello();
@GetMapping("/goodsQuery")
public String queryGoodsController();
}
3.6其中3.5章節FeignClient請求方式只是服務之間通信一種,另為一種為RestTemplate方式,要創建一個配置類
@Configuration //表示這是一個配置類,專案啟東時就加載
public class ljqConfig {
@Bean //交給Bean管理
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
如果采用RestTemplate方式最為服務之間的通信,那么3.5章節的請求方式修改如下:
@Repository
public class HelloDao {
@Autowired
RestTemplate restTemplate; //注入依賴
/**
* 生產者服務的請求路徑 http://SHENGCHANZHE/hello
*/
public String hello(){
return restTemplate.getForObject("http://SHENGCHANZHE/hello",String.class);
}
}
4.創建微服務網關Zull專案工程
pom.xml添加依賴
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-zuul -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
專案工程目錄結構圖

4.1、創建啟動類ZuulApp,如果是SpringBoot專案構建時會生成,也可自己創建,
注意@SpringBootApplication(含義:這是SpringBoot專案的啟動器) @EnableEurekaClient(含義:我方作為客戶端向網關服務注冊自己)
@EnableZuulProxy(含義:這是Zull網關服務)
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApp {
public static void main(String[] args) {
SpringApplication.run(ZuulApp.class , args);
}
}
4.2、組態檔在resource檔案夾下創建application.yml組態檔,配置如下,
spring:
application:
name: zuulserver ##服務名稱
server:
port: 10088 ##埠號
eureka:
client:
service-url: # EurekaServer地址
defaultZone: http://127.0.0.1:10000/eureka
instance:
prefer-ip-address: true # 當呼叫getHostname獲取實體的,回傳ip而不是host名稱
ip-address: 127.0.0.1 # 指定自己的ip資訊,不指定的話會自己尋找
#instance-id: zuul.com
zuul:
#ignored-services:
prefix: /api #請求前綴
routes:
SHENGCHANZHE: /aa/** #通過http://localhost:10088/api/aa/hello
XIAOFEIZHE: /bb/** #通過http://localhost:10088/api/bb/xhello訪問
4.啟動專案
4個服務已經啟動

點擊Euerka服務埠號,可進入注冊串列,可以看見zull、生產者、消費者服務都已經實作注冊,

點擊http://localhost:10088/api/aa/hello,就可以實作服務的呼叫

四、補充問題(重點)
1、要想引入組件Ribbon,客戶端負載均衡技術,服務之間訪問要使用RestTemplate方式實作,
2、Hystrix,客戶端容錯保護,特性有服務降級、服務熔斷、請求快取、請求合并、依賴隔離,可在生產者服務的Controler,實作通過回呼函式方式實作降級、熔斷,
3、Feign,宣告式服務呼叫,本質上就是 Ribbon+Hystrix
4、Stream,訊息驅動,有 Sink、Source、Processor 三種通道,特性有訂閱發布、消費組、訊息磁區,
5、Bus,訊息總線,配合 Config 倉庫修改的一種 Stream 實作,
6、Sleuth,分布式服務追蹤,需要搞清楚 TraceID 和 SpanID 以及抽樣,如何與 ELK 整合,
總結
提示:當然這只是闡述一下微服務專案開發的一個基本的流程,一個微服務的專案服務會有跟多,所以維護起來還是挺耗時間的:
例如:以上就是今天要講的內容,本文僅僅簡單介紹了pandas的使用,而pandas提供了大量能使我們快速便捷地處理資料的函式和方法,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/294438.html
標籤:其他
下一篇:怎么實作分布式事務?
