【Spring Boot】27.spring cloud

簡介

Spring Cloud是一個分佈式的總體解決方案。Spring Cloud 爲開發者提供了在分佈式系統(配置管理,服務發現,熔斷,路由,微代理,控制總線,一次性token,全局瑣,leader選舉,分佈式session,集羣狀態)中快速構建的工具,使用Spring Cloud的開發者能夠快速的啓動服務或構建應用、同時可以快速和雲平臺資源進行對接。java

SpringCloud分佈式開發五大經常使用組件

  • 服務發現——Netflix Eureka
  • 客服端負載均衡——Netflix Ribbon
  • 斷路器——Netflix Hystrix
  • 服務網關——Netflix Zuul
  • 分佈式配置——Spring Cloud Config

快速體驗

搭建項目環境

  1. 建立空項目
  2. 建立註冊中心模塊:eureka-server,模塊選擇Eureka-Server
  3. 建立服務提供者模塊: provider-ticket,模塊選擇Eureka-Discovery、web模塊。(服務提供者是從服務中心註冊本身)
  4. 建立服務消費者模塊:consumer-ticket,模塊選擇Eureka-Discovery、web模塊。(服務提供者是從服務中心中獲取提供者)

配置註冊中心模塊

  1. 修改配置文件application.yml:
server:
  port: 8761 # eureka server defort port
eureka:
  instance:
    hostname: eureka-server # eureka instance host name
  client:
    register-with-eureka: false # don't set myself to eureka, default is true
    fetch-registry: false # don't get register info to eureka server, because we will set this item to be server
    service-url:
      defaultZone: http://localhost:8761/eureka/ # register center server url, default value is http://localhost:8761/eureka/

注意我修改properties爲yml,這是比較容易閱讀的語言,層次更加的清晰。web

  1. 啓動類處啓用euraka server註解
package com.zhaoyi.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

// 啓用註冊中心功能(enable eureka server)
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}
  1. 運行項目,訪問: localhost:8761 便可查看註冊中心網站。接下來,咱們配置服務提供者,將服務註冊到註冊中心。

配置服務提供者模塊

  1. 配置文件application.yml
spring:
  application:
    name: provider-ticket
server:
  port: 8086 # provider server port
eureka:
  instance:
    prefer-ip-address: true # register server use ip style.
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/ # register center url.

該配置中:spring

  1. 編寫service,和咱們以前分佈式的測試例子同樣,是個買票的簡單程序
// 接口類
package com.zhaoyi.providerticket.service;
public interface TicketService {
    public String buy();
}
// 接口實現類
package com.zhaoyi.providerticket.service;

import org.springframework.stereotype.Service;

@Service
public class TicketServiceImpl implements TicketService {
    @Override
    public String buy() {
        return "《Grimgar of Fantasy and Ash》";
    }
}
  1. 編寫controller,用於顯示服務運行端口以及使用買票服務
package com.zhaoyi.providerticket.controller;

import com.zhaoyi.providerticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TicketController {

    @Autowired
    TicketService ticketService;

    @Value("${server.port}")
    private Integer serverPort;

    @GetMapping("/ticket")
    public String ticket(){

        return "server:" + serverPort + " and buy a ticket:" + ticketService.buy() ;
    }
}

啓動提供模塊的時候,請保證註冊中心模塊處於運行狀態。session

爲了往服務中心多註冊幾個服務提供者,咱們修改不一樣端口8086以及8087,並經過maven發佈多2個jar包,將其複製到外部文件夾中,而後一一使用java -jar啓動起來。注意:經過啓動參數修改端口號沒有意義的,由於咱們就是想體驗返回內容的不一樣,因此仍是改改端口,發不一樣的jar包好些—— 模擬多個provider服務。app

多發佈幾個provider,同時運行,咱們能夠看到,註冊中心註冊了咱們的多個provider。負載均衡

這時候訪問IP:8761,就能夠看到咱們的兩個提供者實例都註冊上了:maven

Application AMIs Availability Zones Status
PROVIDER-TICKET n/a(2) (2) UP (2)-DESKTOP-Q1PPPMM:provider-ticket:8087, DESKTOP-Q1PPPMM:provider-ticket:8086

配置服務消費模塊

  1. 配置文件配置
spring:
  application:
    name: consumer-ticket
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: false
server:
  port: 8899

一樣的,咱們開啓8899端口,服務名設置爲consumer-ticket,註冊中心地址註冊爲http://localhost:8761/eureka,使用ip方式進行註冊。分佈式

  1. 主啓動類開啓發現服務功能而且添加RestTmeplate組件
package com.zhaoyi.consumerticket;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
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 ConsumerTicketApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerTicketApplication.class, args);
    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplateBuilder().build();
    }
}

咱們開啓了發現服務註解,而且在容器中添加了一個類型爲RestTemplate的Bean,經過其爲咱們調用註冊中心的具體實現。ide

@LoadBalanced註解能夠實現對調用服務的負載均衡訪問。工具

  1. 編寫controller,消費註冊中心爲咱們提供的服務
package com.zhaoyi.consumerticket.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 IndexController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/buy")
    public String buy(){
        return "恭喜您,購買了" + restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class
        );
    }
}

PROVIDER-TICKET是咱們註冊的服務的名字,能夠從你的服務中心首頁的註冊項的application欄目查找到;

ticket是提供服務者監聽的服務地址,即至關於咱們訪問服務IP:8086/ticket同樣的效果。

運行效果,咱們訪問消費者ip:8899/buy,獲得以下輸出:

恭喜您,購買了server:8086 and buy a ticket:《Grimgar of Fantasy and Ash》

同時,多訪問幾回,你會看到8086 8087的控制檯會分別相應各請求,也能夠經過打印的端口號看出具體是哪一個端口上的服務響應的請求,這取決於負載均衡的流量導向。

結束語

spring cloud算是目前分佈式開發的一個很好的解決方案,還有不少內容須要咱們去了解,但願本文可以爲您開啓一個初始的印象,迎頭而上,在springcloud的使用道路上一路凱歌。

相關文章
相關標籤/搜索