在五分钟内创建,运行和部署Spring微服务

通过优锐课中的java学习分享中,我们可以看到在五分钟内创建,运行和部署Spring微服务的一些相关信息,码了很多知识点,整理给大家参考学习。对微服务架构又有了更深的了解,其中的架构思维导图梳理起来太清晰了。

使用IBM Cloud Developer Tools CLI插件创建,运行和部署Spring微服务

学习目标

使用IBM Cloud Developer Tools CLI生成Spring微服务,为其配置和绑定服务,然后部署到IBM Cloud。 了解所生成的代码,以及如何为你开始添加自己的代码提供一个很好的起点。

先决条件

安装IBM Cloud Developer Tools CLI。

预计时间

阅读和遵循本操作步骤中的步骤大约需要一个小时。

流程

步骤1.生成代码

如果你尚未尝试安装,则需要按照说明安装IBM Cloud Developer Tools CLI。 你可以使用如下所示的CLI生成Spring微服务,并将生成的代码放入当前目录下的文件夹中。

扫描二维码关注公众号,回复: 8320328 查看本文章
spring $bx dev create
? Select a pattern:
1. Web App
2. Mobile App
3. Backend for Frontend
4. Microservice
5. MFP
Enter a number> 4

? Select a starter:
1. Basic
Enter a number> 1

? Select a language:
1. Java ‑ MicroProfile / Java EE
2. Node
3. Python
4. Java ‑ Spring Framework
Enter a number> 4

? Enter a name for your project> springmsdemo
? Enter a hostname for your project> springmsdemo
? Do you want to add services to your project? y/n
? Select a service:
1. Cloudant NoSQL Database
2. Object Storage
Enter a number> 1

? Select a service plan:
1. Lite
2. Standard
3. Dedicated Hardware
Enter a number> 1

Successfully added service to project.

? Do you want to add another service? y/n
The project, springmsdemo, has been successfully saved into the current directory.
OK

在继续之前,让我们看一下我们刚刚选择的内容和生成的内容。 前两个选择确定了我们需要微服务,并且它将使用Spring。 提供服务选项后,事情会变得很有趣。 如果选择一种服务(在本例中为Cloudant),则不仅会生成绑定到该服务的代码,而且还可以对其进行调配以供使用。

在docker容器中本地运行应用程序时,将使用生成的其他文件。 自述文件是一个很好的起点,它将提供有关已生成内容的概述以及有关所选服务的更多信息。

步骤2.运行微服务

然后使用CLI在本地构建和运行微服务。 构建阶段将创建一个docker容器,该容器提供构建微服务所需的工具,然后使用第二个容器来运行构建的容器。

springmsdemo $bx dev build
Deleting the container named 'bx‑dev‑springmsdemo‑tools' ...
Creating image bx‑dev‑java‑maven‑tools based on Dockerfile‑tools...
Image will have user added
OK
Creating a container named 'bx‑dev‑springmsdemo‑tools' from that image...
OK
Starting the 'bx‑dev‑springmsdemo‑tools' container...
OK
Building the project in the current directory started at Wed Aug 23 13:49:37 2017
OK
Stopping the 'springmsdemo' container...
The 'springmsdemo' container was not found
Stopping the 'bx‑dev‑springmsdemo‑tools' container...
OK
springmsdemo $bx dev run
Stopping the 'springmsdemo' container...
The 'springmsdemo' container was not found
Creating image springmsdemo based on Dockerfile...
OK
Creating a container named 'springmsdemo' from that image...
OK
Starting the 'springmsdemo' container...
OK
Executing run command started at Wed Aug 23 13:50:17 2017

  .   _                        
 /\ / '   ()     \ \ \ 
( ( )__ | ' | '| | ' \/ ` | \ \ \ 
 \/  )| |)| | | | | || (| |  ) ) ) )
  '  |_| .|| ||| |\, | / / / /
 =========||==============|__/=///_/
 :: Spring Boot ::        (v1.5.4.RELEASE)

2017‑08‑23 12:50:22.167  INFO 17 ‑‑‑ [           main] application.SBApplication                : Starting SBApplication v1.0‑SNAPSHOT on d6e23df14534 with PID 17 (/project/springmsdemo‑1.0‑SNAPSHOT.jar started by root in /project)
...
(output snipped)

你现在可以通过转至http:// localhost:8080 / v1 / cloudant在本地测试微服务。

真正的好处是,尽管数据库列表为空,但该列表是从先前提供的Cloudant服务实例中读取的。

@RestController
public class Example {

  @Autowired @ServiceName(name="springmsdemo‑cloudantNoSQLDB‑4dce")
  private CloudantClient client;

  @RequestMapping("v1/")
  public @ResponseBody ResponseEntity<String> example() {
    List<String> list = new ArrayList<>();
    //return a simple list of strings
    list.add("Some data");
    return new ResponseEntity<String>(list.toString(), HttpStatus.OK);
  }

  @RequestMapping("v1/cloudant")
  public @ResponseBody ResponseEntity<String> cloudant(){
      List<String> list = new ArrayList<>();
      try {
          list = client.getAllDbs();
      } catch (NullPointerException e) {
          return new ResponseEntity<String>("Server Error", HttpStatus.INTERNAL_SERVER_ERROR);
      }
      return new ResponseEntity<String>("Available databases : " + list.toString(), HttpStatus.OK);
  }

}

步骤3.部署到IBM Cloud

最后一步是将微服务部署到IBM Cloud。 只需在命令行中输入bx dev deploy即可。 然后,你可以在IBM Cloud控制台(以及创建的服务)中查看微服务。
摘要

在本方法中,你学习了如何使用IBM Cloud Developer Tools CLI插件来创建绑定到Cloudant数据库的Spring微服务,在本地构建和运行它,然后在五分钟内将其部署到IBM Cloud中。

猜你喜欢

转载自blog.51cto.com/14634606/2461964