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