实例工程达到的效果是:在Web上通过访问http://localhost:8080/hello/michael/zhang,返回Json字符串{"message":"Hello michael zhang"}
当然,最主要的是通过配置Docker,并且运行在docker上,其中详细讲解了maven plugin在pom.xml起到的作用 。
实例代码已上传至Github:https://github.com/michaelzhanghe/springcloud-01-hello
本实例已在ubuntu17.10和Mac OS下通过测试。
关于docker的安装,可参考:在Ubuntu17.10上安装Docker
或者参考官网文档都非常简单。
--------------------------------在Windows10下的操作 start-----------------------------
在STS中创建Spring Starter Project
工程创建后的文件结构:
加入Controller以实现web端调用
package com.cloudlearning; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication //Spring boot startup annotation @RestController //enable rest call capability @RequestMapping(value="hello")//all rest call request will point to http://localhost:8080/hello in this class public class SpringcloudhelloApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudhelloApplication.class, args); } //when we want to call this hello() method, we have to request http://localhost:8080/hello/{firstName}/{lastName} //firstName & lastName are the parameters in the URI via GET request method //also can use @GetMapping(value="/{firstName}/{lastName}") instead of @RequestMapping //when you look into @GetMapping, you will find @RequestMapping(method = RequestMethod.GET) when defining //That's why @GetMapping can omit the parameter of "method = RequestMethod.GET" @RequestMapping(value="/{firstName}/{lastName}",method = RequestMethod.GET) public String hello( @PathVariable("firstName") String firstName, @PathVariable("lastName") String lastName) { //define a Json format string as return value return String.format("{\"message\":\"Hello %s %s\"}", firstName, lastName); } }
在STS中运行SpringcloudhelloApplication -》 run as Application
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.1.RELEASE) 2018-04-16 15:31:42.371 INFO 8856 --- [ main] c.c.SpringcloudhelloApplication : Starting SpringcloudhelloApplication on OG64OJN3S45ZZO2 with PID 8856 (C:\work\workspace_ibm_ms\springcloudhello\target\classes started by Administrator in C:\work\workspace_ibm_ms\springcloudhello) 2018-04-16 15:31:42.373 INFO 8856 --- [ main] c.c.SpringcloudhelloApplication : No active profile set, falling back to default profiles: default 2018-04-16 15:31:42.429 INFO 8856 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2357d90a: startup date [Mon Apr 16 15:31:42 CST 2018]; root of context hierarchy 2018-04-16 15:31:43.367 INFO 8856 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-04-16 15:31:43.390 INFO 8856 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-04-16 15:31:43.391 INFO 8856 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29 2018-04-16 15:31:43.399 INFO 8856 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_151\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_151/bin/server;C:/Program Files/Java/jre1.8.0_151/bin;C:/Program Files/Java/jre1.8.0_151/lib/amd64;C:\Program Files\IBM\WebSphere MQ\java\lib;C:\Program Files\IBM\WebSphere MQ\java\lib64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\work\dev tools doc\apache-maven-3.5.2\bin;C:\work\dev tools doc\sqlplus_client_12_2;C:\work\dev tools doc\sqlplus_client_12_2\instantclient_12_2_sdk;C:\Windows\system32\wbem;C:\Program Files (x86)\IVT Corporation\BlueSoleil\Mobile;C:\Program Files\IBM\WebSphere MQ\bin64;C:\Program Files\IBM\WebSphere MQ\bin;C:\Program Files\IBM\WebSphere MQ\tools\c\samples\bin;C:\Program Files\IBM\Bluemix\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;;C:\Program Files (x86)\SSH Communications Security\SSH Secure Shell;C:\work\dev tools doc\spring-tool-suite-3.9.1.RELEASE-e4.7.1a-win32-x86_64\sts-bundle\sts-3.9.1.RELEASE;;.] 2018-04-16 15:31:43.486 INFO 8856 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-04-16 15:31:43.486 INFO 8856 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1059 ms 2018-04-16 15:31:43.606 INFO 8856 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-04-16 15:31:43.609 INFO 8856 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-04-16 15:31:43.609 INFO 8856 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-04-16 15:31:43.609 INFO 8856 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-04-16 15:31:43.609 INFO 8856 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-04-16 15:31:43.720 INFO 8856 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-16 15:31:43.912 INFO 8856 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2357d90a: startup date [Mon Apr 16 15:31:42 CST 2018]; root of context hierarchy 2018-04-16 15:31:43.974 INFO 8856 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/{firstName}/{lastName}],methods=[GET]}" onto public java.lang.String com.cloudlearning.SpringcloudhelloApplication.hello(java.lang.String,java.lang.String) 2018-04-16 15:31:43.976 INFO 8856 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-04-16 15:31:43.977 INFO 8856 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-04-16 15:31:43.993 INFO 8856 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-16 15:31:43.993 INFO 8856 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-16 15:31:44.124 INFO 8856 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-04-16 15:31:44.162 INFO 8856 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-04-16 15:31:44.165 INFO 8856 --- [ main] c.c.SpringcloudhelloApplication : Started SpringcloudhelloApplication in 2.054 seconds (JVM running for 2.41) 2018-04-16 15:31:55.096 INFO 8856 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2018-04-16 15:31:55.096 INFO 8856 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2018-04-16 15:31:55.109 INFO 8856 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 13 ms
Tomcat started on port(s): 8080
服务端口默认为8080
浏览器访问后可以得到预期的结果:
以上就是一个简单的spring boot starter工程,实现了一个简单的
-------------------------------下面整合Maven和Docker插件生成docker镜像---------------------------------
这里用到的是Spotify提供的docker-maven-plugin, 优点是能够和Maven整合直接生成镜像文件。
官方网址:https://github.com/spotify/docker-maven-plugin
此处是完整的pom.xml内容,下面有对plugin的详细讲解:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cloudlearning</groupId> <artifactId>springcloud-01-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-01-hello</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <docker.image.name>springcloud-01/hello</docker.image.name> <docker.image.tag>${project.version}</docker.image.tag> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-resources</id> <!-- here the phase you need --> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/dockerfile</outputDirectory> <resources> <resource> <directory>src/main/docker</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.10</version> <configuration> <imageName>${docker.image.name}:${docker.image.tag}</imageName> <dockerDirectory>${basedir}/target/dockerfile</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> </project>
详细讲解各个plugin的作用:
<docker.image.name>springcloud-01/hello</docker.image.name>
docker image的名字,docker build后,可以同过docker images 查看
<docker.image.tag>${project.version}</docker.image.tag>
docker image 的 tag,也可以认为是版本,docker build后,可以同过docker images 查看
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
spring-boot-maven-plugin能够将Spring Boot应用打包为可执行的jar或war文件
<plugin> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-resources</id> <!-- here the phase you need --> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/dockerfile</outputDirectory> <resources> <resource> <directory>src/main/docker</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
maven-resources-plugin是处理静态的资源文件的,在这个例子中,是把src/main/docker路径下的文件copy到${basedir}/target/dockerfile路径下
<resources> <resource> <directory>src/main/docker</directory> <filtering>true</filtering> </resource> </resources>
maven 默认的资源路径是src/main/resources,所以需要使用resource指定我们的路径src/main/docker
filtering=true的作用是将dockerfile中的变量“@project.build.finalName@”替换成实际的jar文件名“springcloud-01-hello-0.0.1-SNAPSHOT”,“project.build.finalName”是maven的内置属性变量,它的值是“${artifaceId}-${version}”。
<artifactId>springcloud-01-hello</artifactId> <version>0.0.1-SNAPSHOT</version>
所以:@project.build.finalName@ = ${artifaceId}-${version} = springcloud-01-hello-0.0.1-SNAPSHOT
<goal>copy-resources</goal>
copy-resources是maven resources插件中3个goal其中的一个,作用是将resources 路径下的文件copy到output directory路径下面:
<phase>validate</phase>
它的含义是copy resouces这步操作是在Maven生命周期的validate阶段执行,也就是第一阶段执行,关于maven生命周期的详细资料可参考maven官方文档:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
<plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.10</version> <configuration> <imageName>${docker.image.name}:${docker.image.tag}</imageName> <dockerDirectory>${basedir}/target/dockerfile</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin>
如果上面的都看明白了,那么再这个插件就简单了。
imageName就是之后生成的镜像名字
dockerDirectory是指定docker生成镜像是使用的dockerfile,其中定义了生成镜像时要执行的步骤和内容
resource还是一样,就是copy资源文件
targetPath是目标路径,就是docker server的根目录“/”
directory是工程的资源路径,可以理解为源路径
include是我们打包生成的springcloud-01-hello-0.0.1-SNAPSHOT.jar
那么当这个docker-maven-plugin插件执行完成后,它将会在docker 本地库里面创建一个名为springcloud-01-hello:0.0.1-SNAPSHOT的镜像。
------------------------------dockerfile--------------------------------
下面是原始的,我们自己编写的dockerfile
FROM openjdk:8-jre-alpine MAINTAINER Michael Zhang <[email protected]> RUN mkdir -p /home/michael01/springcloudroot/ ADD @[email protected] /home/michael01/springcloudroot/ ENTRYPOINT ["/usr/bin/java", "-jar", "/home/michael01/springcloudroot/@[email protected]"]
From是必须要有的,而且必须在第一行,指定运行的基础镜像,可以理解为运行在什么系统上,alpine是一个linux系统非常的小
openjdk:8-jre-alpine
我们的基础镜像就是从docker的中央仓库pull一个安装了openjdk的alpine操作系统
MAINTAINER
这是作者的名字,可有可无,但是对于production生产环境来说还是需要的,以备之后运维时使用。
RUN mkdir -p /home/michael01/springcloudroot/
在docker镜像内的基础镜像,也就是alpine OS上创建目录
ADD @[email protected] /home/michael01/springcloudroot/
将jar加入到镜像的这个路径下
ENTRYPOINT ["/usr/bin/java", "-jar", "/home/michael01/springcloudroot/@[email protected]"]
镜像在启动时(docker run <镜像id |或者| 镜像名>)使用的命令,其实就是运行一个可执行的jar包,EntryPoint和CMD都是在镜像的启动命令,而且只能在dockerfile中执行一次,如果有多个的话,只执行最后一个,其他的全部忽略。
好了,在windows 10上的操作就结束了,下面是在linux上的操作了
--------------------------------在Windows10下的操作 end-----------------------------
--------------------------------在linux下的操作 start-----------------------------
由于代码都上传到了github上,所以我们第一步是下载工程代码。
git clone https://github.com/michaelzhanghe/springcloud-01-hello
然后在springcloud-01-hello路径下执行:
docker clean package docker:build
michael01@michael01-ubuntu01:~/springcloudroot/springcloud-01-hello$ mvn clean package docker:build [INFO] Scanning for projects... [INFO] [INFO] ---------------< com.cloudlearning:springcloud-01-hello >--------------- [INFO] Building springcloud-01-hello 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ springcloud-01-hello --- [INFO] Deleting /home/michael01/springcloudroot/springcloud-01-hello/target [INFO] [INFO] --- maven-resources-plugin:3.0.1:copy-resources (copy-resources) @ springcloud-01-hello --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ springcloud-01-hello --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ springcloud-01-hello --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /home/michael01/springcloudroot/springcloud-01-hello/target/classes [INFO] [INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ springcloud-01-hello --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /home/michael01/springcloudroot/springcloud-01-hello/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ springcloud-01-hello --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /home/michael01/springcloudroot/springcloud-01-hello/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ springcloud-01-hello --- [INFO] [INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ springcloud-01-hello --- [INFO] Building jar: /home/michael01/springcloudroot/springcloud-01-hello/target/springcloud-01-hello-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default) @ springcloud-01-hello --- [INFO] [INFO] --- docker-maven-plugin:0.4.10:build (default-cli) @ springcloud-01-hello --- [INFO] Copying /home/michael01/springcloudroot/springcloud-01-hello/target/springcloud-01-hello-0.0.1-SNAPSHOT.jar -> /home/michael01/springcloudroot/springcloud-01-hello/target/docker/springcloud-01-hello-0.0.1-SNAPSHOT.jar [INFO] Copying /home/michael01/springcloudroot/springcloud-01-hello/target/dockerfile/Dockerfile -> /home/michael01/springcloudroot/springcloud-01-hello/target/docker/Dockerfile [INFO] Building image springcloud-01/hello:0.0.1-SNAPSHOT Step 1/6 : FROM openjdk:8-jre-alpine ---> b1bd879ca9b3 Step 2/6 : MAINTAINER Michael Zhang <[email protected]> ---> Using cache ---> 7e6bb6ed2380 Step 3/6 : RUN apk update && apk upgrade && apk add netcat-openbsd ---> Using cache ---> f2045850aeb3 Step 4/6 : RUN mkdir -p /home/michael01/springcloudroot/ ---> Using cache ---> cc72e0bbb89e Step 5/6 : ADD springcloud-01-hello-0.0.1-SNAPSHOT.jar /home/michael01/springcloudroot/ ---> dd0f02316b88 Step 6/6 : ENTRYPOINT ["/usr/bin/java", "-jar", "/home/michael01/springcloudroot/springcloud-01-hello-0.0.1-SNAPSHOT.jar"] ---> Running in 56a9122f2525 Removing intermediate container 56a9122f2525 ---> a524d56e3fd4 ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null} Successfully built a524d56e3fd4 Successfully tagged springcloud-01/hello:0.0.1-SNAPSHOT [INFO] Built springcloud-01/hello:0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12.055 s [INFO] Finished at: 2018-04-16T22:07:54+08:00 [INFO] ------------------------------------------------------------------------ michael01@michael01-ubuntu01:~/springcloudroot/springcloud-01-hello$
如果成功的话,执行docker images命令,就可以看到我们镜像了
michael01@michael01-ubuntu01:~$ docker images | grep -E "REPOS|hello" REPOSITORY TAG IMAGE ID CREATED SIZE springcloud-01/hello 0.0.1-SNAPSHOT a524d56e3fd4 2 hours ago 101MB
通过imageid启动镜像,注意必须加上-p参数,第一个8080是对外的端口,也就是我们要访问的端口,第二个是docker镜像里面tomcat的web端口。
michael01@michael01-ubuntu01:~$ docker run -p 8080:8080 a524d56e3fd4 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.1.RELEASE) 2018-04-16 16:23:46.243 INFO 1 --- [ main] c.c.SpringcloudhelloApplication : Starting SpringcloudhelloApplication v0.0.1-SNAPSHOT on c58921fd8cba with PID 1 (/home/michael01/springcloudroot/springcloud-01-hello-0.0.1-SNAPSHOT.jar started by root in /) 2018-04-16 16:23:46.264 INFO 1 --- [ main] c.c.SpringcloudhelloApplication : No active profile set, falling back to default profiles: default 2018-04-16 16:23:46.506 INFO 1 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4c3e4790: startup date [Mon Apr 16 16:23:46 GMT 2018]; root of context hierarchy 2018-04-16 16:23:49.618 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-04-16 16:23:49.718 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-04-16 16:23:49.720 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29 2018-04-16 16:23:49.756 INFO 1 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 2018-04-16 16:23:49.926 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-04-16 16:23:49.931 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3449 ms 2018-04-16 16:23:50.188 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-04-16 16:23:50.194 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-04-16 16:23:50.200 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-04-16 16:23:50.201 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-04-16 16:23:50.202 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-04-16 16:23:50.470 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-16 16:23:51.000 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4c3e4790: startup date [Mon Apr 16 16:23:46 GMT 2018]; root of context hierarchy 2018-04-16 16:23:51.161 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/{firstName}/{lastName}],methods=[GET]}" onto public java.lang.String com.cloudlearning.SpringcloudhelloApplication.hello(java.lang.String,java.lang.String) 2018-04-16 16:23:51.173 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-04-16 16:23:51.174 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-04-16 16:23:51.236 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-16 16:23:51.237 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-16 16:23:51.592 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-04-16 16:23:51.710 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-04-16 16:23:51.721 INFO 1 --- [ main] c.c.SpringcloudhelloApplication : Started SpringcloudhelloApplication in 6.691 seconds (JVM running for 7.821)
开启另一个ssh终端通过curl验证,结果正确返回:
michael01@michael01-ubuntu01:~$ curl http://localhost:8080/hello/michael/zhang {"message":"Hello michael zhang"}
此时可以通过docker ps命令查看docker容器的状态如下:
michael01@michael01-ubuntu01:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c58921fd8cba a524d56e3fd4 "/usr/bin/java -jar …" 3 minutes ago Up 3 minutes 0.0.0.0:8080->8080/tcp musing_torvalds
关闭容器的命令是docker stop 容器id
michael01@michael01-ubuntu01:~$ docker stop c58921fd8cba c58921fd8cba
再次执行docker ps,刚刚的容器已经没有了,表示当前没有正在运行的容器了
michael01@michael01-ubuntu01:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
要查看容器的历史情况的话,可以执行:
michael01@michael01-ubuntu01:~$ docker ps -a | grep a524d56e3fd4 c58921fd8cba a524d56e3fd4 "/usr/bin/java -jar …" 8 minutes ago Exited (143) 3 minutes ago musing_torvalds 8acf93aa0cad a524d56e3fd4 "/usr/bin/java -jar …" 8 minutes ago Exited (130) 8 minutes ago blissful_kare bc9aac7bfbd6 a524d56e3fd4 "/usr/bin/java -jar …" 2 hours ago Exited (130) 15 minutes ago boring_keller 8d74c9290c25 a524d56e3fd4 "/usr/bin/java -jar …" 2 hours ago Exited (143) 2 hours ago silly_karea524d56e3fd4是我们的镜像id
--------------------------------在linux下的操作 end-----------------------------
提醒:
我在配置环境的时候,由于使用了root来安装jdk,和用root解压缩的maven,所以用root从git上pull代码,导致了没有权限maven build, 因为mvn命令不能sudo,因为mvn是staff用户,所以没有权限访问root下来的代码,并且无法创建target。
michael01@michael01-ubuntu01:~$ which mvn /opt/soft/apache-maven-3.5.3/bin/mvn michael01@michael01-ubuntu01:~$ ls -l /opt/soft/apache-maven-3.5.3/bin/mvn -rwxrwxrwx 1 501 staff 5741 2月 25 03:46 /opt/soft/apache-maven-3.5.3/bin/mvn michael01@michael01-ubuntu01:~$ sudo mvn -v [sudo] password for michael01: The JAVA_HOME environment variable is not defined correctly This environment variable is needed to run this program NB: JAVA_HOME should point to a JDK not a JRE michael01@michael01-ubuntu01:~$ mvn -v Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00) Maven home: /opt/soft/apache-maven-3.5.3 Java version: 1.8.0_162, vendor: Oracle Corporation Java home: /opt/soft/java/jdk1.8.0_162/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "4.13.0-38-generic", arch: "amd64", family: "unix"