项目作者: huawei-microservice-demo

项目描述 :
An example to illustrate the functionalities of Mesher
高级语言: Go
项目地址: git://github.com/huawei-microservice-demo/mesher-example.git
创建时间: 2017-11-07T01:59:34Z
项目社区:https://github.com/huawei-microservice-demo/mesher-example

开源协议:Apache License 2.0

下载


Mesher Example

Making your service resilient using Mesher

This example shows you how to make your service as a Micro-Service using Mesher. In this example we have two simple services called client and server. We make this service use Mesher to do the communication between them and helps you to make your service robust and resilient.
You can watch a short demonstration of this example in this video and full tutorial of running this example with CSE Governance Console here.

Running Service in Cloud Service Engine(CSE) or with open source solution

You can run these services in Huawei Public Cloud or in your local Machine and register these micro-services in CSE. And make use of CSE Governance Console to mange and monitor the services.

Or you can connect to Service center without CSE, but you will lose monitoring, Hot-reconfigurarion and Governance web console features

Get Started

Run with Docker-Compose

One of the great options to run this example is to use docker-compose, you can follow this guideline to run this example.

Run locally

You can follow the below steps to run the services in your local VM’s and use the CSE Service-Center to register the micro-service, you can also use the Config-Center of CSE to manage your configurations. CSE also provider monitoring sever which can help you to monitor your service statistics.

Notice: you should prepare 2 VM to run this demo

Step 1: Clone the code in VM1

  1. export GOPATH=$PWD
  2. go get github.com/huawei-microservice-demo/mesher-example/client

Clone in VM2

  1. export GOPATH=$PWD
  2. go get github.com/huawei-microservice-demo/mesher-example/server

Step 2: Start the server in VM2

  1. cd bin
  2. ./server

this will start the Server exposing the below API’s on 3000 port

  1. {Name: "latency", Method: rata.GET, Path: "/latency"},
  2. {Name: "error", Method: rata.GET, Path: "/errors"},
  3. {Name: "latency2", Method: rata.POST, Path: "/latency/:duration"},
  4. {Name: "error2", Method: rata.POST, Path: "/errors/:status"},

Step 3: Download and start the Mesher in both VM
You can download the Mesher release from here and choose one of the below options for running the Mesher.

Running Mesher with Open Source Service-Center

One way for bringing up the Mesher easily is to download the open-source service-center and follow the guide to bring up the service-center locally.
Once the Service-Center is running then you can follow the below steps to run Mesher in VM2.

1.Export the following variables in VM2

  1. export CSE_REGISTRY_ADDR=http://127.0.0.1:30100
  2. #tell mesher where is your service listen at
  3. export SPECIFIC_ADDR=127.0.0.1:3000

2.edit conf/chassis.yaml
change advertiseAddress and listenAddress to external IP
Example:

  1. protocols:
  2. http:
  3. listenAddress: 192.168.1.1:30101

3.edit conf/microservice.yaml
change name to demoServer

4.Run mesher

  1. ./mesher

this will make the Mesher Provider run at 30101 port

Running Mesher with CSE

Another way to bring up the Mesher is to use the CSE Service-Center and Governance Console. For registering your microservice to CSE service-center you will need the AK/SK of your project which can be found by following the steps here.
Once you got the AK/SK then you need to configure the AK/SK in mesher conf/auth.yaml by following the steps here

1.Export the following variables in VM2

  1. #tell mesher where is your service listen at
  2. export SPECIFIC_ADDR=127.0.0.1:3000

2.edit conf/chassis.yaml
change advertiseAddress and listenAddress to external IP
Example:

  1. protocols:
  2. http:
  3. listenAddress: 192.168.1.1:30101

3.edit conf/microservice.yaml
change name to demoServer

4.Run mesher

  1. ./mesher

this will make the Mesher Provider run at 30101 port

Step 4: Start the Mesher in VM1
Based on your selection of Service-Center in Step 3 you can configure the Mesher in VM1

Running Mesher with Open Source Service-Center

Export the following variables

  1. #Based on where your opensource service-center is running you can configure the below IP
  2. export CSE_REGISTRY_ADDR=http://127.0.0.1:30100

Run mesher

  1. ./mesher

This will bring up the Mesher in 30101 port

Running Mesher with CSE

You need to configure AK/SK as per steps given in Step3 and then follow the below commands.

1.edit conf/microservice.yaml
change name to demoClient
Run mesher

  1. ./mesher

This will bring up the Mesher in 30101 port

Step 5:
Start the Client in VM1

  1. cd src/github.com/huawei-microservice-demo/mesher-example/client
  2. vi conf/app.conf
  3. ## Edit the below addr
  4. PROVIDER_ADDR=http://demoServer
  5. ## Save the file and come out
  6. ### Export the http_proxy so that all calls going from Client is proxied through MesherConsumer
  7. export http_proxy=127.0.0.1:30101
  8. ./client

This will bring up Client on 3000 port exposing the below Api’s

  1. {Name: "latency", Method: rata.GET, Path: "/TestLatency"},
  2. {Name: "error", Method: rata.GET, Path: "/TestErrors"},

Congratulations, Now all your applications are running with Mesher

now you can call the Client Api’s

  1. root@mesher-01-eip:~# curl -v 117.78.44.191:3000/TestLatency
  2. * Trying 117.78.44.191...
  3. * Connected to 117.78.44.191 (117.78.44.191) port 3000 (#0)
  4. > GET /TestLatency HTTP/1.1
  5. > Host: 117.78.44.191:3000
  6. > User-Agent: curl/7.47.0
  7. > Accept: */*
  8. >
  9. < HTTP/1.1 200 OK
  10. < Date: Fri, 17 Nov 2017 11:33:21 GMT
  11. < Content-Length: 110
  12. < Content-Type: text/plain; charset=utf-8
  13. <
  14. The Latency for this request is : 100ms
  15. * Connection #0 to host 117.78.44.191 left intact
  16. The host serving this request is mesher-02 and the IP is 192.168.1.155
  1. root@mesher-01-eip:~# curl -v 117.78.44.191:3000/TestErrors
  2. * Trying 117.78.44.191...
  3. * Connected to 117.78.44.191 (117.78.44.191) port 3000 (#0)
  4. > GET /TestErrors HTTP/1.1
  5. > Host: 117.78.44.191:3000
  6. > User-Agent: curl/7.47.0
  7. > Accept: */*
  8. >
  9. < HTTP/1.1 500 Internal Server Error
  10. < Date: Fri, 17 Nov 2017 11:34:02 GMT
  11. < Content-Length: 208
  12. < Content-Type: text/plain; charset=utf-8
  13. <
  14. * Connection #0 to host 117.78.44.191 left intact

You can manage your Micro-Service in Governance Console in CSE.

Please follow the steps here to Configure your application to monitor metrics in Grafana.

mesher-examples