Spring Cloud 微服務(wù)架構(gòu)的五臟六腑!
Spring Cloud 是一個(gè)基于 Spring Boot 實(shí)現(xiàn)的微服務(wù)框架,它包含了實(shí)現(xiàn)微服務(wù)架構(gòu)所需的各種組件。
注:Spring Boot 簡單理解就是簡化 Spring 項(xiàng)目的搭建、配置、組合的框架。因?yàn)榕c構(gòu)建微服務(wù)本身沒有直接關(guān)系,所以本文不對(duì) Spring Boot 進(jìn)行展開。
另外本文有一些例子涉及到 Spring 和 Spring Boot,建議先了解一下 Spring 和 Spring Boot 再閱讀本文。
本文的閱讀對(duì)象主要是沒有接觸過服務(wù)架構(gòu),想對(duì)其有一個(gè)宏觀的了解的同學(xué)。
本文將從 Spring Cloud 出發(fā),分兩小節(jié)講述微服務(wù)框架的「五臟六腑」:
第一小節(jié)「服務(wù)架構(gòu)」旨在說明的包括兩點(diǎn),一服務(wù)架構(gòu)是什么及其必要性;二是服務(wù)架構(gòu)的基本組成。為什么第一節(jié)寫服務(wù)架構(gòu)而不是微服務(wù)架構(gòu)呢?原因主要是微服務(wù)架構(gòu)本身與服務(wù)架構(gòu)有著千絲萬縷的關(guān)系,服務(wù)架構(gòu)是微服務(wù)架構(gòu)的根基。 第二小節(jié)「五臟六腑」則將結(jié)合 Spring Cloud 這個(gè)特例來介紹一個(gè)完整的微服務(wù)框架的組成。
面向服務(wù)的架構(gòu)和微服務(wù)架構(gòu)
服務(wù)在哪?(服務(wù)治理問題) 怎么調(diào)用?(服務(wù)調(diào)用問題)
@EnableEurekaServer 表示該 Spring Boot 應(yīng)用是一個(gè)注冊中心。@SpringBootApplication
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
eureka.client.registerWithEureka: false 和fetchRegistry: false 來表明自己是一個(gè) eureka server。
port: 8080
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
service-hello 服務(wù)
@EnableEurekaClient 表示他是一個(gè) Eureka 客戶端,它會(huì)在注冊中心注冊自己。@RestController 表示這是一個(gè)控制器,@RequestMapping("/hello") 表示匹配到請(qǐng)求 '/hello' 時(shí)會(huì)調(diào)用該方法進(jìn)行響應(yīng)。@EnableEurekaClient
@RestController
public class ServiceHelloApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHelloApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hello")
public String home(@RequestParam String name) {
return "hello "+name+",i am from port:" +port;
}
}
http://localhost:8080/eureka/,也就是上面我們定義的。服務(wù)名為 service-hello,將會(huì)被調(diào)用者使用。client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
server:
port: 8081
spring:
application:
name: service-hello
服務(wù)消費(fèi)者 service-ribbon
HelloControler 接收到請(qǐng)求,并調(diào)用 HelloService 中的 helloService 方法,HelloService 中通過定義的 restTemplate 去調(diào)用 http://service-hello/hello。此處要注意的是 @LoadBalanced 注解,它表示啟用負(fù)載均衡。@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String helloService(String name) {
return restTemplate.getForObject("http://service-hello/hello?name="+name,String.class);
}
}
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hello")
public String hello(@RequestParam String name){
return helloService.helloService(name);
}
}
服務(wù)“雪崩”與斷路器
服務(wù)暴露與路由網(wǎng)關(guān)
服務(wù)配置與配置中心
Spring Cloud 提供了 Spring Cloud Config 組件,它支持配置服務(wù)放在配置服務(wù)的內(nèi)存中(即本地),也支持放在遠(yuǎn)程 Git 倉庫中,幫助我們管理服務(wù)的配置信息。信息同步與消息總線
問題定位與鏈路追蹤

怎么接私活?這個(gè)渠道你100%有用!請(qǐng)收藏!
喜歡文章,點(diǎn)個(gè)在看
評(píng)論
圖片
表情


