spring cloud+dotnet core搭建微服務架構:Api閘道器(三)
前言
國慶假期,一直沒有時間更新。
根據群裡面的同學的提問,強烈推薦大家先熟悉下spring cloud。文章下面有純潔大神的spring cloud系列。
上一章最後說了,因為服務是不對外暴露的,所以在外網要訪問服務必須通過API閘道器來完成,而spring cloud 提供了現成的Api閘道器元件zuul。它包含了路由,授權,壓力測試等一系列功能。如下圖所示,Api閘道器在整個應用環境的位置。
業務場景
我們先模擬一個業務場景,客戶端(web,ios,android...)通過Api閘道器訪問訂單服務,訂單服務有兩個節點,為了模擬叢集效果,這兩個節點分別返回不同的資料。那麼我們一共需要建立4個應用程式。服務中心(Java)、Api閘道器(Java)、訂單服務1(.NET Core)、訂單服務2(.NET Core)。
程式碼部分
服務中心
使用intellij idea建立一個spring boot專案,建立服務中心。設定埠為5000。
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId></dependency>
ServiceCenterApplication.java
@EnableEurekaServer@SpringBootApplication public class ServiceCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceCenterApplication.class, args);
}
}
application.properties
spring.application.name=service-center
server.port=5000
Api閘道器
使用intellij idea建立一個spring boot專案,建立Api閘道器服務。設定埠為5555。
pom.xml
<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>
ServiceGatewayApplication.java
@SpringBootApplication@EnableZuulProxypublic class ServiceGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceGatewayApplication.class, args);
}
}
application.properties
spring.application.name=service-gateway
server.port=5555eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/#下面的程式碼可以註釋zuul.routes.order.path=/order/**
zuul.routes.order.serviceId=order
上面配置是不是和nginx很像。zuul還提供了預設規則,
http://ZUUL_HOST:ZUUL_PORT/serviceId/**
,滿足這一規則的會自動代理,如上面的配置完全可以不用寫,這樣大量的服務就不用一個一個配置了。
訂單服務1
使用vs2017建立 .NET Core Web Api應用程式
appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"spring": {
"application": {
"name": "order"
}
},
"eureka": {
"client": {
"serviceUrl": "http://localhost:5000/eureka/"
},
"instance": {
"port": 8010
}
}
}
ValuesController.cs
[Route("/")]
public class ValuesController : Controller{
[HttpGet] public string Get() {
return "order1";
}
}
訂單服務2
同訂單服務1的建立過程,修改埠為8011和返回結果。
appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"spring": {
"application": {
"name": "order"
}
},
"eureka": {
"client": {
"serviceUrl": "http://localhost:5000/eureka/"
},
"instance": {
"port": 8011
}
}
}
ValuesController.cs
[Route("/")]public class ValuesController : Controller{
[HttpGet] public string Get() { return "order2";
}
}
篇幅有限,以上程式碼均有精簡,完整程式碼請去Github上獲取。
我們現在一共有4個應用程式:
eureka服務中心,埠5000,應用名稱service-center
zuul閘道器服務,埠5555,應用名稱service-gateway
訂單服務1,埠8010,應用名稱order
訂單服務2,埠8011,應用名稱order
其中訂單服務1和訂單服務2組成了訂單服務叢集
分別啟動這4個應用程式。開啟eureka服務:http://localhost:5000/,
如下圖所示都註冊成功。
開啟http://localhost:5555/order,返回"order1"
重新整理後返回"order2",反覆多次重新整理,"order1"和"order2"依次返回。
後記
通過上面的例子說明Api閘道器服務已經生效,並且實現了負載均衡。結合具體的業務場景,我們的生產環境只對外暴露5555埠,客戶端訪問Api閘道器,由Api閘道器去路由到各個服務節點,這樣所有的客戶端呼叫都統一了入口。
示例程式碼
所有程式碼均上傳github。
求推薦,你們的支援是我寫作最大的動力,我的QQ群:328438252,交流微服務。
相關文章:
原文地址:http://www.cnblogs.com/longxianghui/p/7646870.html
.NET社群新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注