思考:什么是分布式?什么是微服務?
一些概念:RPC-遠程過程調用,某台機器想要調用另一台機器所需要的一種服務,及分布式的服務框架,比如dubbo或者SpringCloud。
鋪天蓋地的分布式互聯網系統,使用較多的是zookeeper+dubbo組合,而Springboot推薦使用全棧Spring,就是Springboot+SpringCloud。
舉例說明一下基本原理:A想要獲得數據B,但是有10台機器都可能存放了數據B,那么我要去哪台取數據B呢?A和B中間就有了一個管理站,這個管理站類似一個分配和注冊中心,他可以告訴A想要的數據B在哪些機器里,A知道了以后就可以去這些機器里取了。同樣B想要把自己數據分享給A,那么可以咨詢這個管理站來知道可以分享給誰。
那么我們說的Dubbo或者SpringCloud就是上邊說到的RPC服務框架,而Zookeeper和SpringBoot就可以作為這個管理站點來使用。
Zookeeper+Dubbo
官方看文檔,或者網搜相關部署,比如:https://www.cnblogs.com/jaycekon/p/SpringBootDubbo.html
這篇文章主要以這個為主來講,上邊的自己看吧,有需要我再單獨補文章~
SpringBoot+SpringCloud
Cloud與Dubbo的區別,Dubbo解決的就是遠程過程調用的RPC服務,而Cloud更全面,它有一整套的分布式需要的對應的解決方案:配置管理、服務發現、熔斷、路由、微代理、控制總線、一次性token、全局鎖、leader選舉、分布式session、集群狀態。所以使用Cloud可以更快速的與雲平台進行對接。
SpringCloud五大常用組件:
-
- 服務發現-Netflix Eureka
- 客服端負載均衡-Netflix Ribbon
- 斷路器-Netflix Hystrix
- 服務網關-Netflix Zuul
- 分布式配置-SpringCloudConfig
那么我們來看SpringCloud怎么搞~
1)、配置-Eureka信息
首先我們創建幾個需要用到的module,一個注冊中心,一個服務提供方,一個服務使用方。然后在注冊中心進行一下配置:
這里可以使用編譯器中創建Spring Initializr的快捷模式
# 這里我使用了application.yml的配置,看起來會更清晰
server:
port: 8761 #啟動端口
eureka:
instance:
hostname: eureka-server #eureka實例主機名-注冊中心的名字
client:
register-with-eureka: false #不把自己注冊到注冊中心,因為本身就是作為注冊中心的存在
fetch-registry: false #不從eureka上獲取注冊信息,同上
service-url:
defaultZone: http://localhost:8761/eureka #配置默認的啟動路徑
/** * 注冊中心
* EnableEurekaServer啟動eureka服務 */ @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
2)、啟動主程序,打開瀏覽器測試一下(先看啟動信息,已經啟動了該服務):
2)、配置-provider信息
1.創建服務並將主程序啟動(這是服務中心的服務不要停,也是在啟動狀態的),然后再來看服務已經注冊進去了

import org.springframework.stereotype.Service; @Service public class TicketService { public String getTicket() { return "《大鯊魚》"; } }

import com.ice.provider.service.TicketService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TicketController { @Autowired TicketService ticketService; @GetMapping("/ticket") public String getTicket() { return ticketService.getTicket(); } }

server:
port: 8001 #提供方的啟動端口
spring:
application:
name: provider
eureka:
instance:
prefer-ip-address: true #注冊服務的時候使用服務ip地址
client:
service-url:
defaultZone: http://localhost:8761/eureka
2.來看,如果有多個應用呢?
修改一下server.port,一個8001,一個8002,分別打包啟動。然后再來看注冊中心里邊(兩個都在)
3)、配置-consumer信息
同樣,先把自己注冊到注冊中心,然后創建個controller,並啟動主程序,檢查:

server:
port: 8200 #提供方的啟動端口
spring:
application:
name: consumer
eureka:
instance:
prefer-ip-address: true #注冊服務的時候使用服務ip地址
client:
service-url:
defaultZone: http://localhost:8761/eureka

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient // 開啟發現服務 @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } @LoadBalanced // 啟用負載均衡服務 @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class UserController { @Autowired RestTemplate restTemplate; @GetMapping("/buy") public String buyTicket(String name) { // 從注冊中心獲取提供方的信息,http://提供方應用的名字/路徑,String類型 String s = restTemplate.getForObject("http://PROVIDER/ticket", String.class); return name + "購買了" + s; } }
如此,我們的分布式就說到這里,這么看,不是很難理解吧?
P.S:我們在上邊有一個負載均衡的注解,哪里可以看來有什么作用呢?如果你啟動了8001和8002的兩個服務,可以通過啟動窗口看到(我們有在兩個服務中加入不同的打印語句),兩個端口分別被調用,輪詢式的均衡調用~