2021年08月25日 微服务步骤

给新建的项目配置maven设置

image-20210825104710162

image-20210825104913641

image-20210825104825509

创建新Spring WEB项目

image-20210825104513032

image-20210825105227874

image-20210825105431948

image-20210825105547929

image-20210825105652346

编写代码

提升主类

创建Controller

创建controller包

image-20210825110302723

image-20210825110311988

创建Controller

image-20210825110502742

image-20210825110536649

TestController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {
@RequestMapping("test")
@ResponseBody
public String test(String input) {
return "YouInput:" + input;
}
}

创建静态页面

image-20210825110819272

index.html

image-20210825110845676

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Spring 微服务HelloWorld</title>
</head>
<body>
<h3>Spring 微服务 HelloWorld</h3>
</body>
</html>

设置端口

image-20210825111101071

idea中运行

image-20210825111147926

浏览器验证

主页

image-20210825111249869

控制器

image-20210825111409211

maven打包jar

复制项目路径

image-20210825111523641

打开cmd,进入项目的根路径,输入mvn package进行打包:

image-20210825111638093

打包成功效果

1
2
3
4
5
6
7
8
9
10
11
12
13
PS G:\dev2\idea_workspace\demo> mvn package
[INFO] Scanning for projects...
[INFO]
......
[INFO] --- spring-boot-maven-plugin:2.5.4:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.170 s
[INFO] Finished at: 2021-08-25T11:17:11+08:00
[INFO] ------------------------------------------------------------------------
PS G:\dev2\idea_workspace\demo>

打包会在项目根路径下生成一个target目录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
PS G:\dev2\idea_workspace\demo> ls


目录: G:\dev2\idea_workspace\demo


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2021.08.25 11:15 .idea
d----- 2021.08.25 10:57 .mvn
d----- 2021.08.25 10:57 src
d----- 2021.08.25 11:17 target
-a---- 2021.08.25 10:57 395 .gitignore
-a---- 2021.08.25 10:59 8128 demo.iml
-a---- 2021.08.25 10:57 891 HELP.md
-a---- 2021.08.25 10:57 10070 mvnw
-a---- 2021.08.25 10:57 6608 mvnw.cmd
-a---- 2021.08.25 10:57 1430 pom.xml


PS G:\dev2\idea_workspace\demo>

java -jar运行

进入target目录,可以看到生成的jar文件:demo-0.0.1-SNAPSHOT.jar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PS G:\dev2\idea_workspace\demo\target> ls


目录: G:\dev2\idea_workspace\demo\target


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2021.08.25 11:12 classes
d----- 2021.08.25 11:12 generated-sources
d----- 2021.08.25 11:12 generated-test-sources
d----- 2021.08.25 11:17 maven-archiver
d----- 2021.08.25 11:16 maven-status
d----- 2021.08.25 11:17 surefire-reports
d----- 2021.08.25 11:12 test-classes
-a---- 2021.08.25 11:17 17345973 demo-0.0.1-SNAPSHOT.jar
-a---- 2021.08.25 11:17 3739 demo-0.0.1-SNAPSHOT.jar.original


PS G:\dev2\idea_workspace\demo\target>

然后输入

1
java -jar .\demo-0.0.1-SNAPSHOT.jar

命令运行,这个jar:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
PS G:\dev2\idea_workspace\demo\target> java -jar .\demo-0.0.1-SNAPSHOT.jar

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.4)

2021-08-25 11:21:57.305 INFO 12348 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 11.0.4 on DESKTOP-SH3MQPI with PID 12348 (G:\dev2\idea_workspace\demo\target\demo-0.0.1-SNAPSHOT.jar started by 18190 in G:\dev2\idea_workspace\demo\target)
2021-08-25 11:21:57.305 INFO 12348 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-08-25 11:21:59.990 INFO 12348 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9000 (http)
2021-08-25 11:22:00.084 INFO 12348 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-08-25 11:22:00.146 INFO 12348 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.52]
2021-08-25 11:22:00.381 INFO 12348 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-08-25 11:22:00.381 INFO 12348 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2889 ms
2021-08-25 11:22:01.256 INFO 12348 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2021-08-25 11:22:01.553 INFO 12348 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9000 (http) with context path ''
2021-08-25 11:22:01.568 INFO 12348 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 5.231 seconds (JVM running for 6.034)

浏览器验证

运行的效果和在idea中运行一样

image-20210825112354798