閱讀本章,請先閱讀【SpringCloud】快速入門(一)
本章使用Demo,是在SpringCloud單機版的基礎上,擴充的。
服務提供者集群
既然SpringCloud的是微服務結構,那么對於同一種服務,當然不可能只有一個節點,需要部署多個節點
架構圖如下:
由上可以看出存在多個同一種服務提供者(Service Provider)
搭建服務提供者集群
1、參考:【SpringCloud】SpringCloud 快速入門(一)搭建單機版的:Eureka Server、Service Provider、Service Consumer
2、根據支付模塊服務提供者(test-springcloud-provider-payment8001),在父工程中,同樣新建一個支付模塊的服務提供者(test-springcloud-provider-payment8002)
服務8001與8002的配置中,除端口外,其他都相同,且spring.application.name應用名稱必須相同,表明2個服務是同一種服務
服務8002配置文件如下:

1 # 端口 2 server: 3 port: 8002 4 5 spring: 6 application: 7 name: cloud-payment-service 8 # 數據源基本配置 9 datasource: 10 driver-class-name: com.mysql.cj.jdbc.Driver 11 url: jdbc:mysql://localhost:3306/test_springcloud?allowPublicKeyRetrieval=true&useSSL=true 12 username: admin 13 password: 123456 14 15 eureka: 16 client: 17 # 表示將自己注冊進Eureka Server默認為true 18 register-with-eureka: true 19 # 是否從Eureka Server抓去已有的注冊信息,默認是true 20 fetch-registry: true 21 # 設置與Eureka Server交互的地址查詢服務和注冊服務都需要依賴這個地址 22 service-url: 23 defaultZone: http://localhost:8761/eureka 24 25 mybatis: 26 mapperLocations: classpath:mapper/*Mapper.xml 27 # 所有entity別名類所在的包 28 type-aliases-pachage: com.test.springcloud.entities
3、重新啟動項目,打開Eureka查看,發現有2個支付服務
4、使用訂單模塊消費者調用支付服務
消費者部分代碼模塊如下:
1 @Configuration 2 public class AppConfig { 3 4 /** 5 * 注入restTemplate,請用請求rest接口 6 * @return 7 */ 8 @Bean 9 // 標注此注解后,RestTemplate就具有了客戶端負載均衡能力 10 // 負載均衡技術依賴於的是Ribbon組件~ 11 // RestTemplate都塞入一個loadBalancerInterceptor 讓其具備有負載均衡的能力 12 @LoadBalanced 13 public RestTemplate restTemplate(){ 14 return new RestTemplate(); 15 } 16 } 17 18 19 @RestController 20 @Slf4j 21 public class OrderController { 22 23 public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE"; 24 25 @Autowired 26 private RestTemplate restTemplate; 27 28 @GetMapping("/consumer/payment/get/{id}") 29 public CommonResult<Payment> getPayment(@PathVariable("id") Long id){ 30 return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class); 31 } 32 33 ... 34 }
由上可以,通過配置向容器中注入了RestTemplate對象,而RestTemplate又被標注此@LoadBalanced注解后,RestTemplate就具有了客戶端負載均衡能力,也就是說RestTemplate會輪流調用服務的各個節點,到達均衡的目的
5、測試調用,啟動項目,使用地址:http://localhost:8000/consumer/payment/get/1,進行訪問,可以看到已到達負載均衡的目的
服務發現Discovery
服務發現就是對於注冊進eureka里面的微服務,可以通過服務發現獲得該服務的信息
案例如下:
1、在微服務中使用@EnableDiscoveryClient注解,啟用服務發現
1 // Eureka客戶端 2 @EnableEurekaClient 3 // 啟用服務發現 4 @EnableDiscoveryClient 5 @SpringBootApplication 6 public class PaymentMain8001 { 7 public static void main(String[] args) { 8 SpringApplication.run(PaymentMain8001.class, args); 9 } 10 }
2、編輯Controller,啟用服務發現之后,它會自動向容器注入DiscoveryClient(服務發現客戶端)
通過調用DiscoveryClient的getServices方法,從注冊中心獲取服務列表
通過調用DiscoveryClient的getInstances方法,從注冊中心獲取服務實例集
1 public class PaymentController { 2 3 @Autowired 4 private DiscoveryClient discoveryClient; 5 6 @GetMapping(value = "/payment/discovery") 7 public Object discovery(){ 8 // 獲取服務列表 9 List<String> services = discoveryClient.getServices(); 10 for (String element : services) { 11 log.info("=====element:" + element); 12 } 13 14 // 獲取服務實例集 15 List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); 16 for (ServiceInstance instance : instances) { 17 log.info("服務發現" + "\n" 18 + "getServiceId === " + instance.getServiceId() + "\n" 19 + "getHost === " + instance.getHost() + "\n" 20 + "getPort === " + instance.getPort() + "\n" 21 + "getUri === " + instance.getUri() ); 22 } 23 24 return this.discoveryClient; 25 } 26 27 }
3、啟用服務,使用地址:http://localhost:8001/payment/discovery,進行訪問
頁面結果:
后台日志:
由上可知,通過DiscoveryClient能獲取到注冊進eureka里面的微服務信息。