隨着互聯網技術和市場的發展,傳統的單體應用已經無法滿足業務的需求,微服務架構應運而生。Spring Cloud作為目前常用的微服務框架,提供了一整套的解決方案,方便我們快速構建分佈式系統。本文將從搭建環境、註冊中心、配置中心、網關、服務調用等多個方面詳細介紹Spring Cloud的使用。
一、搭建環境
在使用Spring Cloud之前,需要先搭建好環境。我們以Maven項目為例,需要在pom.xml中添加Spring Cloud相關依賴:
<!--Spring Cloud--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>2020.0.0</version> <type>pom</type> <scope>import</scope> </dependency>
其次,在主類上添加@EnableDiscoveryClient註解,開啟服務註冊與發現功能:
@SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
二、註冊中心
註冊中心是微服務的核心,用於統一管理註冊的服務,簡化服務的調用和維護。Spring Cloud提供了Eureka作為服務註冊中心,可以快速地進行搭建和配置。
在pom.xml中添加Eureka相關依賴:
<!--Eureka Server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
在主類上添加@EnableEurekaServer註解,使其成為Eureka Server。
@SpringBootApplication @EnableEurekaServer public class EurekaServer { public static void main(String[] args) { SpringApplication.run(EurekaServer.class, args); } }
完整的Eureka Server配置文件如下:
spring.application.name=eureka-server server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
三、配置中心
配置中心用於統一管理分佈式系統中的配置,方便配置的修改和更新。Spring Cloud提供了Config作為配置中心,支持多種數據源。
在pom.xml中添加Config相關依賴,此處使用Git作為數據源:
<!--Config Client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--Config Server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config-server</artifactId> </dependency>
在主類上添加@EnableConfigServer註解,使其成為Config Server。
@SpringBootApplication @EnableConfigServer public class ConfigServer { public static void main(String[] args) { SpringApplication.run(ConfigServer.class, args); } }
完整的Config Server配置文件如下:
spring.application.name=config-server server.port=8888 spring.cloud.config.server.git.uri=https://github.com/your/repo spring.cloud.config.server.git.searchPaths=config-repo spring.cloud.config.server.git.username=your-username spring.cloud.config.server.git.password=your-password
通過訪問Config Server的REST接口,就可以獲取到配置文件,如:
http://localhost:8888/{application}/{profile}[/{label}] https://myhost:8888/app-dev.yml
四、網關
網關是微服務架構中的重要組件,用於轉發請求、認證鑒權和限流流控等功能。Spring Cloud提供了Zuul作為網關,可以方便地進行配置和擴展。
在pom.xml中添加Zuul相關依賴:
<!--Zuul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
在主類上添加@EnableZuulProxy註解,使其成為Zuul。
@SpringBootApplication @EnableZuulProxy public class ZuulProxy { public static void main(String[] args) { SpringApplication.run(ZuulProxy.class, args); } }
完整的Zuul配置文件如下:
spring.application.name=zuul-proxy server.port=8080 zuul.routes.service-id.path=/api/** zuul.routes.service-id.stripPrefix=false
其中,service-id為可在Eureka Server上註冊的服務實例ID,path為轉發路徑,stripPrefix表示是否去掉轉發路徑的前綴。
五、服務調用
服務調用是微服務架構中的基礎,事實上Spring Cloud提供了多種方案,如Feign、LoadBalancer等。
在pom.xml中添加Feign相關依賴:
<!--Feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
在主類上添加@EnableFeignClients註解,開啟Feign功能。
@SpringBootApplication @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
使用Feign調用服務,只需要定義一個接口,如:
@FeignClient(value = "service-id") public interface ServiceClient { @GetMapping("/service") String getService(); }
其中,value為服務實例ID,GetMapping為調用方式,getService為調用方法名,/service為服務端提供的接口。
然後,在需要調用服務的地方注入ServiceClient即可:
@Autowired private ServiceClient serviceClient;
然後,在需要調用服務的地方調用ServiceClient的方法即可:
String result = serviceClient.getService();
結語
總而言之,通過以上幾個方面的詳細介紹,相信大家已經掌握了使用Spring Cloud搭建分佈式系統的方法。當然,Spring Cloud還有更多的功能和擴展,建立在此基礎之上,我們可以深入學習和探究。
原創文章,作者:SMXY,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/149568.html