SpringCloud是一組微服務架構的框架,由Spring官方提供,它基於SpringBoot提供了一系列微服務治理工具,如服務註冊與發現、配置中心、斷路器、網關等等,使得微服務架構開發變得更加容易。下面將從不同的角度來介紹SpringCloud的一些關鍵概念和用法。
一、服務註冊與發現
服務註冊與發現是微服務最基本的功能之一,SpringCloud提供了Eureka和Consul兩個註冊中心。下面以Eureka為例,更深入地介紹如何使用服務註冊和發現機制。
1. 註冊Eureka Server
第一步是註冊一個Eureka Server,我們可以在pom.xml中加入下面的依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
然後,我們可以通過@Configuration來啟用Eureka Server。下面是一個簡單的實現:
@Configuration
@EnableEurekaServer
public class EurekaServerConfig {
public static void main(String[] args) {
SpringApplication.run(EurekaServerConfig.class, args);
}
}
啟動程序後,訪問http://localhost:8761可以看到Eureka Server的控制台。
2. 註冊Eureka Client
有了Eureka Server,我們可以在各自的服務中註冊Eureka Client。下面是示例代碼:
// 添加Eureka Client 依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
// 在程序啟動類上添加@EnableDiscoveryClient註解
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
啟動Eureka Client程序後,我們可以在Eureka Server的控制台上看到它的註冊信息。
二、配置中心
在微服務架構中,配置中心起着至關重要的作用。SpringCloud提供了Config Server與Config Client實現了分佈式集中式配置的功能,下面我們來看一下如何使用。
1. 註冊Config Server
第一步是還是加入SpringCloud Config Server的依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
然後,我們可以通過@EnableConfigServer註解啟用Config Server,下面是一個簡單的實現:
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
Config Server需要配置一個Git或SVN倉庫。我們可以在application.properties或者application.yml文件中配置,如下面所示:
spring.cloud.config.server.git.uri=https://git.example.com/config-repo
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
spring.cloud.config.server.git.search-paths=config
spring.cloud.config.server.git.clone-on-start=true
2. 註冊Config Client
接下來,我們需要在各個服務中註冊Config Client。同樣,我們先添加依賴,在程序啟動類上添加@EnableConfigClient註解,示例代碼如下:
// 添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
// 在程序啟動類上添加@EnableConfigClient註解
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
接着,在bootstrap.properties或者bootstrap.yml文件中添加關於Config Server的配置,如下面所示:
server.port=8080
spring.cloud.config.uri=http://config-server.example.com
spring.application.name=myapp
spring.profiles.active=dev
spring.cloud.config.label=master
啟動Config Client程序後,它會自動從Config Server中獲取配置文件,並將配置文件中的屬性注入到應用程序中。
三、斷路器
斷路器是為了處理微服務中的容錯和彈性而出現,SpringCloud提供了Hystrix來實現斷路器的功能。
1. 註冊Hystrix Dashboard
在使用Hystrix之前,我們可以先搭建一個Hystrix Dashboard用於監控各個服務的運行情況。示例代碼如下:
// 添加Hystrix Dashboard依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
// 在程序啟動類上添加@EnableHystrixDashboard註解
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
啟動Hystrix Dashboard後,我們可以訪問http://localhost:8080/hystrix查看Dashboard。
2. 使用斷路器
在服務中使用Hystrix非常簡單,我們只需要使用@EnableCircuitBreaker註解和使用@HystrixCommand註解即可實現,示例代碼如下:
// 添加Hystrix依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
// 在程序啟動類上添加@EnableCircuitBreaker註解
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
@Autowired
private ServiceClient serviceClient;
@HystrixCommand(fallbackMethod = "fallback")
public String hello() {
return serviceClient.hello();
}
public String fallback() {
return "fallback message";
}
}
在上面代碼中,@HystrixCommand註解代表使用斷路器的功能。如果ServiceClient的hello方法發生異常,將會調用fallback方法返回fallback message。
四、網關
在微服務架構中,網關類似於路由器的作用,它可以映射和重定向請求、調用相應服務,並將結果返回給客戶端。
1. 註冊Zuul Gateway
SpringCloud提供了Zuul來實現微服務的網關,我們可以在程序啟動類上添加@EnableZuulProxy註解來啟用Zuul Gateway,示例代碼如下:
// 添加Zuul依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
// 在程序啟動類上添加@EnableZuulProxy註解
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
Zuul Gateway通過配置路由實現請求的轉發和調用,可以在application.properties或者application.yml文件中配置路由,如下面所示:
zuul.routes.book.path=/app/**
zuul.routes.book.url=http://localhost:9000
上面代碼中,/app/**路徑的請求將被轉發到http://localhost:9000服務。
2. 使用Zuul Redirect
使用Zuul重定向請求也非常簡單,我們只需要在目標服務中使用@RestController註解和@RequestMapping註解,然後返回重定向地址即可,示例代碼如下:
@RestController
@RequestMapping("/redirect")
public class RedirectController {
@GetMapping
public String redirect() {
return "redirect:https://www.baidu.com";
}
}
上面代碼中,當我們訪問/redirect路徑時,將會被重定向到https://www.baidu.com。
原創文章,作者:UDDLQ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/370171.html