首頁  >  文章  >  Java  >  建構分散式、高可用的Spring Cloud微服務體系

建構分散式、高可用的Spring Cloud微服務體系

王林
王林原創
2023-06-22 09:06:25959瀏覽

隨著網路業務的不斷發展,微服務架構成為了越來越多的企業首選的架構方式。而Spring Cloud作為一個基於Spring Boot的微服務框架,具有分散式、高可用等特性,越來越多的企業開始使用它來建立自己的微服務體系。

本文將介紹如何使用Spring Cloud建構一個高可用、分散式的微服務體系。

一、建置註冊中心

註冊中心是Spring Cloud微服務架構的核心元件之一,所有的微服務都需要向註冊中心註冊自己的訊息,然後其他服務才能通過註冊中心找到這個服務。在Spring Cloud中,Eureka是一個非常優秀的註冊中心元件,它具有高可用、分散式等特性,可以滿足各種不同場景下的需求,因此我們選擇使用Eureka來建立註冊中心。

在使用Eureka建立註冊中心之前,我們需要了解Eureka的一些基本概念:

  1. Eureka Server:Eureka的伺服器端,用於接受來自客戶端的註冊資訊並維護服務實例的資訊列表。
  2. Eureka Client:Eureka的客戶端,用於向Eureka Server註冊自身服務。
  3. 服務實例:微服務的一個運作實例,服務實例包含了服務的IP位址、連接埠號碼、健康狀況等資訊。

使用Eureka建置註冊中心的步驟如下:

  1. 新建一個Spring Boot專案。
  2. 新增依賴:在pom.xml檔案中加入以下依賴:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  1. 在application.yml檔案中新增下列設定:
server:
  port: 8761    #设置服务端口号

eureka:
  instance:
    hostname: localhost    #设置Eureka Server的主机名
  client:
    register-with-eureka: false    #设置是否注册自身服务,默认为true,这里设置为false
    fetch-registry: false    #设置是否获取注册列表,默认为true,这里设置为false
  server:
    enable-self-preservation: false    #设置是否启用自我保护机制,默认为true,这里设置为false
  1. 新建一個Spring Boot的啟動類,並加上@EnableEurekaServer註解,用於啟動Eureka服務:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

透過以上的步驟,我們就成功建置了一個基於Eureka的註冊中心。我們可以在瀏覽器中輸入http://localhost:8761造訪Eureka Server的控制台,看到目前沒有任何服務註冊到Eureka Server中。

二、 建置服務提供者

服務提供者是實現具體業務的微服務,它會向註冊中心註冊自己的訊息,讓其他的微服務能夠發現它並呼叫它提供的服務。

我們在這裡使用一個簡單的範例,建立一個HTTP服務提供者,它能夠接受HTTP請求,並傳回一個字串。

使用Spring Cloud建置服務提供者的步驟如下:

  1. 新建一個Spring Boot專案。
  2. 新增依賴:在pom.xml檔案中加入以下依賴:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  1. 在application.yml檔案中新增下列設定:
server:
  port: 8080    #设置服务端口号

spring:
  application:
    name: test-service    #设置服务名称,用于注册到Eureka Server中

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/    #设置Eureka Server地址
  1. 新建一個Controller類,並在其中新增一個傳回字串的介面:
@RestController
public class TestController {
    @GetMapping("/test")
    public String test() {
        return "Hello World";
    }
}
  1. 在啟動類別中加入@EnableDiscoveryClient註解,用於啟用服務發現功能:
@SpringBootApplication
@EnableDiscoveryClient
public class TestServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestServiceApplication.class, args);
    }
}

透過以上的步驟,我們就成功建置了一個服務提供者。我們可以在瀏覽器中輸入http://localhost:8080/test來存取它提供的服務,如果一切正常,就可以看到Hello World的回傳值。

三、 建構服務消費者

服務消費者是呼叫其他微服務提供的服務的微服務,它會向註冊中心查詢所需的微服務,然後呼叫該微服務提供的服務。

使用Spring Cloud建立服務消費者的步驟如下:

  1. 新建一個Spring Boot專案。
  2. 新增依賴:在pom.xml檔案中加入以下依賴:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  1. 在application.yml檔案中新增下列設定:
server:
  port: 8090    #设置服务端口号

spring:
  application:
    name: test-consumer    #设置服务名称

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/    #设置Eureka Server地址
  1. 新增Service類,用於呼叫服務提供者:
@Service
public class TestService {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public String test() {
        return restTemplate.getForObject("http://test-service/test", String.class);
    }

    public String fallback() {
        return "fallback";
    }
}
  1. 新增一個Controller類,用於暴露服務介面:
@RestController
public class TestController {
    @Autowired
    private TestService testService;

    @GetMapping("/test")
    public String test() {
        return testService.test();
    }
}
  1. 在啟動類別中加入@EnableDiscoveryClient註解,用於啟用服務發現功能:
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker    #启用熔断器功能
public class TestConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced    #启用负载均衡功能
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

透過以上的步驟,我們就成功建構了一個服務消費者,它可以呼叫服務提供者的服務並傳回正確的結果。

四、 建構API網關

API網關是微服務系統的入口,它扮演了路由、負載平衡、安全控制等多種作用。在Spring Cloud中,Zuul是一個優秀的API網關元件,可以滿足我們的各種需求。

使用Spring Cloud建置API網關的步驟如下:

  1. 新建一個Spring Boot專案。
  2. 新增依賴:在pom.xml檔案中加入以下依賴:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
  1. 在application.yml檔案中新增下列設定:
server:
  port: 8888    #设置服务端口号

spring:
  application:
    name: api-gateway    #设置服务名称

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/    #设置Eureka Server地址

zuul:
  routes:
    test-service:
      path: /test/**
      serviceId: test-service    #设置服务提供者名称
  1. 在啟動類別中加入@EnableZuulProxy註解,用於啟用Zuul代理功能:
@SpringBootApplication
@EnableZuulProxy
public class ApiGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }
}

透過以上的步驟,我們就成功建置了一個API網關,它能夠將http://localhost:8888/test請求轉送到服務提供者,並傳回正確的結果。

五、 建置配置中心

配置中心能夠集中管理微服務系統中的配置訊息,透過配置中心,我們能夠更方便地對系統進行配置。

在Spring Cloud中,Config Server是一个优秀的配置中心组件,它可以与Eureka、Zuul等组件配合使用,构建一个完整的微服务体系。

使用Spring Cloud构建配置中心的步骤如下:

  1. 新建一个Spring Boot项目。
  2. 添加依赖:在pom.xml文件中添加如下依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  1. 在application.yml文件中添加如下配置:
server:
  port: 8888    #设置服务端口号

spring:
  application:
    name: config-server    #设置服务名称

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/    #设置Eureka Server地址

# 配置中心
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/{username}/{repository}.git    #设置git仓库地址
          username: {username}    #设置git用户名
          password: {password}    #设置git密码
          search-paths: respo1A/config, respo1B/config    #设置配置文件搜索路径
          default-label: main    #设置git分支
  1. 在启动类中添加@EnableConfigServer注解,用于启用Config Server功能:
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

通过以上的步骤,我们就成功构建了一个Config Server。我们可以将配置文件上传到git仓库中,然后通过http://localhost:8888/application-dev.properties的方式获取指定的配置文件。

六、 总结

通过以上的步骤,我们成功地构建了一个高可用、分布式的Spring Cloud微服务体系,包括了注册中心、服务提供者、服务消费者、API网关和配置中心。在实际应用过程中,我们可以通过这些组件自由组合,构建出更加复杂、高效的微服务体系。

以上是建構分散式、高可用的Spring Cloud微服務體系的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn