10 minutes to create a distributed micro-service

Source: Internet
Author: User
Tags aliyun

Create a microservices service starting from 01. Create a web App with Cloudoll

Create a directory: Hello_world, enter the command line NPM init after you go in.

Introducing the Cloudoll Package

npm i cloudoll --save

Create a portal file/index.js

require(‘cloudoll‘).KoaApplication();

Create File/api/open/hello.js

module.exports = {  world: function *() {    this.echo("你好世界。form port: " + process.env.port );  }};

Start the service now:

node index.js

Now visit and try

Http://localhost:3000/open/hello/world

Cochie worried. The program created by Cloudoll is pure KOAJS application.

2. Run Cloudeer.

Download the source code from git:

git clone https://code.aliyun.com/cloudark/cloudeer.git

If HTTPS is not working, use HTTP.

Go to the table of contents and work on node foreplay.

npm install

Manually create a/data directory to store the data.

Run:

node index.js

Visit Http://localhost:8801/view

No need to change cloudeer any code.

Finish the call!

3. Distributed micro-service Hello_world

Well, next, we'll turn the program written in the first step into a micro service that can be deployed.

Create a file:/config/development.js

Note: Under the project created in the first step, oh, cochie the wrong position.

The contents are as follows:

module.exports = { app_name : "hello_world", debug : true, port : 3000, cloudeer :{ server: ‘http://127.0.0.1:8801‘ }, my_host : "127.0.0.1"};

The configuration of the Cloudeer node will turn this application into a distributed microservices service.

Reboot a bit.

Now visit the registration center to see:

Http://localhost:8801/view

And

Http://localhost:8801/methods

It seems very simple!

Long March just finished the first step.

If interested please continue.

4. Create another microservices (WOW) and invoke the Hello_world micro-service

Now create another microservices, the same steps as the previous Hello_world.

You can copy it right here. But some key points need to be changed.

The app_name in the configuration needs to be changed to another, and now it's wow so that he becomes another microservices.

Modify/api/open/hello.js

module.exports = {  world: function *() {    var res   = yield this.app.cloudeer.invokeCo("GET", "hello_world", "/open/hello/world");    this.echo("来自远方的问候: " + res);  }};

Start the service:

node index.js

Look at the output HTTP port of the console, similar to the output below, and look in the browser.

Koa Application 正在启动,尝试端口:3002Koa Application 启动成功!端口: 3002
5. Distributed Deployment Hello_world

Enter the Hello_world project.

Perform multiple times:

node index.js

Don't worry about port issues, he will automatically look for the right port.

6. Prove it.

Now you can go and execute the/open/hello/world of Wow.

You can see the result in a constant refresh in the browser.

The example uses a port to indicate which micro-service he is accessing from.

For more information, please visit this open source group

Https://code.aliyun.com/groups/cloudark

The above documents are constantly updated here:

Https://code.aliyun.com/cloudark/cloudoc

10 minutes to create a distributed micro-service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.