spring cloud+.net core搭建微服務架構:Api網關(三)

前言

國慶假期,一直沒有時間更新。
根據羣裏面的同窗的提問,強烈推薦你們先熟悉下spring cloud。文章下面有純潔大神的spring cloud系列。
上一章最後說了,由於服務是不對外暴露的,因此在外網要訪問服務必須經過API網關來完成,而spring cloud 提供了現成的Api網關組件zuul。它包含了路由,受權,壓力測試等一系列功能。以下圖所示,Api網關在整個應用環境的位置。
imagehtml

業務場景

咱們先模擬一個業務場景,客戶端(web,ios,android...)經過Api網關訪問訂單服務,訂單服務有兩個節點,爲了模擬集羣效果,這兩個節點分別返回不一樣的數據。那麼咱們一共須要建立4個應用程序。服務中心(Java)、Api網關(Java)、訂單服務1(.NET Core)、訂單服務2(.NET Core)。java

代碼部分

服務中心

使用intellij idea建立一個spring boot項目,建立服務中心。設置端口爲5000。
pom.xmlandroid

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

ServiceCenterApplication.javaios

@EnableEurekaServer
@SpringBootApplication
public class ServiceCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceCenterApplication.class, args);
    }
}

application.propertiesnginx

spring.application.name=service-center
server.port=5000

Api網關

使用intellij idea建立一個spring boot項目,建立Api網關服務。設置端口爲5555。
pom.xmlgit

<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.javagithub

@SpringBootApplication
@EnableZuulProxy
public class ServiceGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceGatewayApplication.class, args);
    }
}

application.propertiesweb

spring.application.name=service-gateway
server.port=5555
eureka.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/**,知足這一規則的會自動代理,如上面的配置徹底能夠不用寫,這樣大量的服務就不用一個一個配置了。spring

訂單服務1

使用vs2017建立 .NET Core Web Api應用程序json

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個應用程序:

  1. eureka服務中心,端口5000,應用名稱service-center
  2. zuul網關服務,端口5555,應用名稱service-gateway
  3. 訂單服務1,端口8010,應用名稱order
  4. 訂單服務2,端口8011,應用名稱order

其中訂單服務1和訂單服務2組成了訂單服務集羣

分別啓動這4個應用程序。打開eureka服務:http://localhost:5000/,
以下圖所示都註冊成功。

image

打開http://localhost:5555/order,返回"order1"

image

刷新後返回"order2",反覆屢次刷新,"order1"和"order2"依次返回。

image

後記

經過上面的例子說明Api網關服務已經生效,而且實現了負載均衡。結合具體的業務場景,咱們的生產環境只對外暴露5555端口,客戶端訪問Api網關,由Api網關去路由到各個服務節點,這樣全部的客戶端調用都統一了入口。

示例代碼

全部代碼均上傳github。代碼按照章節的順序上傳,例如第一章demo1,第二章demo2以此類推。
求推薦,大家的支持是我寫做最大的動力,個人QQ羣:328438252,交流微服務。

傳送門

參考資料

java部分

.net部分

相關文章
相關標籤/搜索