Spring CloudSpring Cloud是一個(gè)分布式的整體解決方案。Spring Cloud 為開(kāi)發(fā)者提供了在分布式系統(tǒng) (配置管理,服務(wù)發(fā)現(xiàn),熔斷,路由,微代理,控制總線,一次性token,全局瑣,leader選舉, 分布式session,集群狀態(tài))中快速構(gòu)建的工具,使用Spring Cloud的開(kāi)發(fā)者可以快速的啟 動(dòng)服務(wù)或構(gòu)建應(yīng)用、同時(shí)能夠快速和云平臺(tái)資源進(jìn)行對(duì)接。? SpringCloud分布式開(kāi)發(fā)五大常用組件 服務(wù)發(fā)現(xiàn)——Netflix Eureka? 客服端負(fù)載均衡——Netflix Ribbon? 斷路器——Netflix Hystrix? 服務(wù)網(wǎng)關(guān)——Netflix Zuul? 分布式配置——Spring Cloud Config? 新建工程: 服務(wù)發(fā)現(xiàn)(注冊(cè)中心):Eureka此時(shí)需要引入: ? 配置文件 server.port=8761 #主機(jī)名 eureka.instance.hostname=server #不做高可用不進(jìn)行設(shè)置 #不把本身注冊(cè)在注冊(cè)中心 eureka.client.register-with-eureka=false #不從eureka上獲取服務(wù)的注冊(cè)信息 eureka.client.fetch-registry=false #服務(wù)中心地址 eureka.client.service-url.DEFAULT_ZONE=http://localhost:8761/eureka/ ? 開(kāi)啟服務(wù): @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } } ? 訪問(wèn)網(wǎng)頁(yè): 此時(shí)的服務(wù)是開(kāi)啟的?。?! ? ?服務(wù)提供者: ? TicketService.java package com.cr.provider.service; import org.springframework.stereotype.Service; @Service public class TicketService { public String buyTicket(){ return "戰(zhàn)狼2"; } } ? ? ?TicketController.java import org.springframework.web.bind.annotation.RestController; @RestController public class TicketController { @Autowired TicketService ticketService; //通過(guò)http協(xié)議進(jìn)行發(fā)送的 @GetMapping("/buy") public String getTicket(){ return ticketService.buyTicket(); } } ? 配置文件: server.port=8081 ? 啟動(dòng)服務(wù)訪問(wèn):@SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } } ? 此時(shí)查看注冊(cè)證中心: ? ? 此時(shí)打包兩個(gè)jar文件分別未8081、8082端口,分別進(jìn)行多個(gè)服務(wù)的注冊(cè) 此時(shí):同一個(gè)應(yīng)用的兩個(gè)實(shí)例 ? 服務(wù)消費(fèi)者: ? ? UserController.java package com.cr.consumer.controller; 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 { //用于獲取http請(qǐng)求的信息 @Autowired RestTemplate restTemplate; @GetMapping("/buyTicket") public String buyTicket(String name){ String ticket = restTemplate.getForObject("http://PROVIDER/buy", String.class); return name "購(gòu)買了" ticket ; } } ? ? 配置文件: server.port=8088spring.application.name=consumer ? 主類: package com.cr.consumer; 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; //開(kāi)啟發(fā)現(xiàn)服務(wù)功能 @EnableDiscoveryClient @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } //http請(qǐng)求 @LoadBalanced//使用負(fù)載均衡機(jī)制 @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } } ? ? 啟動(dòng)服務(wù): ? ? ? 來(lái)源:http://www./content-4-136401.html |
|
來(lái)自: 印度阿三17 > 《開(kāi)發(fā)》