本文節選自《瘋狂Spring Cloud微服務架構實戰》
京東購買地址::https://item.jd.com/12256011.html
噹噹網購買地址::http://product.dangdang.com/25201393.html
Spring Cloud教學視頻:http://blog.csdn.net/boxiong86/article/details/78399104
Spring Cloud電子書:http://blog.csdn.net/boxiong86/article/details/78488226
Spring Cloud集羣使用Zuul
在前面小節的例子中,Zuul將請求轉發到一個Web項目進行處理,如果實際處理請求的不是一個Web項目,而是整個微服務集羣,那麼Zuul將成爲整個集羣的網關。在加入Zuul前,Spring Cloud集羣的結構請見圖7-3。
圖7-3原來的Spring Cloud集羣結構
爲微服務集羣加入Zuul網關後,結構如圖7-4所示。
圖7-4加入Zuul後集羣結構
在深入學習Zuul前,先按圖7-4的搭建本章的測試項目。
集羣搭建
假設當前需要實現一個書本銷售業務,在銷售模塊中需要調用書本模塊的服務,用來查找書本,本小節案例以此爲基礎,建立以下項目:
zuul-eureka-server:Eureka服務器,應用端口爲8761,讀者可以到以下目錄取得源代碼:codes\07\03\zuul-eureka-server。
zuul-book-service:書本模塊,屬於服務提供者,提供“/book/{bookId}”服務,用於查找圖書,最後返回Book的JSON字符串,應用端口爲9000,代碼目錄codes\07\03\zuul-book-service。
zuul-sale-service:銷售模塊,屬於服務調用者,對外發布銷售服務“/sale-book/{bookId}”,該服務中會調用zuul-book-service來查找Book,應用端口爲9100,代碼目錄codes\07\03\zuul-sale-service。
書本模塊“zuul-book-service”發佈的服務,僅返回一個簡單的Book對象,控制器代碼如下:
@RequestMapping(value = "/book/{bookId}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Book findBook(@PathVariable Integer bookId) {
Book book = new Book();
book.setId(bookId);
book.setName("Workflow講義");
book.setAuthor("楊恩雄");
return book;
}
銷售模塊“zuul-sale-service”發佈的服務,相關代碼如下:
@FeignClient("zuul-book-service") //聲明調用書本服務
public interface BookService {
/**
*調用書本服務的接口,獲取一個Book實例
*/
@RequestMapping(method = RequestMethod.GET, value = "/book/{bookId}")
Book getBook(@PathVariable("bookId") Integer bookId);
}
@RestController
public class SaleController {
@Autowired
private BookService bookService;
@RequestMapping(value = "/sale-book/{bookId}", method = RequestMethod.GET)public String saleBook(@PathVariable Integer bookId) {
//調用book服務查找
Book book = bookService.getBook(bookId);
//控制檯輸入,模擬進行銷售
System.out.println("銷售模塊處理銷售,要銷售的圖書id: " + book.getId() + ", 書名:"
+ book.getName());
//銷售成功
return "SUCCESS";
}
}
銷售模塊的服務,使用Feign來調用書本模塊的服務來獲取Book實例,然後在控制檯中輸出信息,在實際應用中,銷售的過程會更爲複雜,例如有可能涉及支付等內容,本例爲了簡單起見,僅作簡單的輸出。接下來,創建網關項目。
路由到集羣服務
在前一小節的基礎上,新建一個名稱爲“zuul-gateway”的Maven項目(代碼目錄codes\07\03\zuul-gateway),在pom.xml中加入以下依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
新建應用類,如代碼清單7-3所示。
代碼清單7-3:codes\07\03\zuul-gateway\src\main\java\org\crazyit\cloud\GatewayApplication.java
@EnableZuulProxy
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(GatewayApplication.class).properties(
"server.port=8080").run(args);
}
}
應用類跟前面的例子一致,使用@EnableZuulProxy註解,但是,由於網關項目需要加入到集羣中,因此要修改配置文件,讓其註冊到Eureka服務器中。本例的配置文件如代碼清單7-4。
代碼清單7-4:codes\07\03\zuul-gateway\src\main\resources\application.yml
spring:
application:
name: zuul-gateway
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
sale:
path: /sale/**
serviceId: zuul-sale-service
使用eureka的配置,將自己註冊到8761的Eureka中。在配置Zuul時,聲明所有的“/sale/**”請求,將會被轉發到Id爲“zuul-sale-service”的服務進行處理。
一般情況下,配置了serviceId後,在處理請求的“routing”階段,將會使用一個名稱爲RibbonRoutingFilter的過濾器,該過濾器會調用Ribbon的API來實現負載均衡,默認情況下調用HttpClient來調用集羣服務。
按照以下順序啓動集羣:
啓動zuul-eureka-server(Eureka服務器)。
啓動zuul-book-service(服務提供者)。
啓動zuul-sale-service(服務調用者)。
啓動zuul-gateway(集羣網關)。
在瀏覽器中訪問:http://localhost:8080/sale/sale-book/1,返回“SUCCESS”字符串,在銷售模塊的控制檯,可以看到輸出如下:
銷售模塊處理銷售,要銷售的圖書id: 1,書名:Workflow講義
根據輸出可知,銷售模塊、書本模塊均被調用。本例涉及了4個項目,圖7-5展示了本例的結構,可幫助讀者理解本例。
圖7-5本例的結構
Zuul Http客戶端
我們知道,Ribbon用來實現負載均衡,Ribbon在選取了合適的服務器後,再調用REST客戶端API來調用集羣服務。在默認情況下,將使用HttpClient的API來調用集羣服務。除了HttpClient外,還可以使用OkHttpClient,以及com.netflix.niws.client.http.RestClient,RestClient目前已經不推薦使用,如果想啓用OkHttpClient,可以添加以下配置:ribbon.okhttp.enabled=true。除了該配置外,還要在pom.xml中加入OkHttpClient的依賴:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
注意:本小節的案例,也會在本章後面章節中使用。
本文節選自《瘋狂Spring Cloud微服務架構實戰》
Spring Cloud教學視頻:http://blog.csdn.net/boxiong86/article/details/78399104
Spring Cloud電子書:http://blog.csdn.net/boxiong86/article/details/78488226