java web开发(从spring boot到spring cloud)

时间:2022-10-07 22:55:15

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

        spring mvc开发相比较servlet-jsp-mysql而言,是简单很多。但是它也有一个缺点,就是xml配置文件较多,比如说pom.xml、web.xml、***-servlet.xml、applicationContext.xml等等。所以在spring基础之上,为了进一步简化spring开发的流程,人们开发了spring boot。它相当于一个spring全家桶,大大简化了spring的开发步骤。今天正好来学习下。

        文章部分内容参考了这个链接,https://blog.csdn.net/hezhimin1124/article/details/103782745,在此表示感谢。

1、IDEA创建工程,选择Spring Initializr

java web开发(从spring boot到spring cloud)

2、略过group和artifact的设置,直接下一步,

java web开发(从spring boot到spring cloud)

3、选择web和spring web,下一步

java web开发(从spring boot到spring cloud)

                目前这里设置的选项是web和spring web。但是大家可以看下左下角的选项,这里出现了很多spring cloud的内容,这说明spring cloud的很多配置本身也是依赖于spring boot来开发的。

4、工程命名为Hello,

java web开发(从spring boot到spring cloud)

5、在com.example.demo添加HelloController.java文件

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String get(){
        return "Hello Spring Boot";
    }
}

6、设置内置web server启动端口为8090

java web开发(从spring boot到spring cloud)

        因为默认端口是8080,所以这部分一定要修改一下。

7、不需要继续配置tomcat server,直接使用默认server即可,单击绿色按钮,不出意外你会看到这样的打印,

"C:\Program Files (x86)\Java\jdk1.8.0_91\bin\java.exe" -Dserver.port=8090 -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.1\lib\idea_rt.jar=63113:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\rt.jar;C:\Users\feixiaoxing\Desktop\Hello\target\classes;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-starter-web\2.7.4\spring-boot-starter-web-2.7.4.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-starter\2.7.4\spring-boot-starter-2.7.4.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot\2.7.4\spring-boot-2.7.4.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-autoconfigure\2.7.4\spring-boot-autoconfigure-2.7.4.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-starter-logging\2.7.4\spring-boot-starter-logging-2.7.4.jar;D:\software\apache-maven-3.6.3-bin\repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\software\apache-maven-3.6.3-bin\repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\software\apache-maven-3.6.3-bin\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\software\apache-maven-3.6.3-bin\repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\software\apache-maven-3.6.3-bin\repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\software\apache-maven-3.6.3-bin\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\software\apache-maven-3.6.3-bin\repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-starter-json\2.7.4\spring-boot-starter-json-2.7.4.jar;D:\software\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\core\jackson-databind\2.13.4\jackson-databind-2.13.4.jar;D:\software\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.4\jackson-annotations-2.13.4.jar;D:\software\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\core\jackson-core\2.13.4\jackson-core-2.13.4.jar;D:\software\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.4\jackson-datatype-jdk8-2.13.4.jar;D:\software\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.4\jackson-datatype-jsr310-2.13.4.jar;D:\software\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.4\jackson-module-parameter-names-2.13.4.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-starter-tomcat\2.7.4\spring-boot-starter-tomcat-2.7.4.jar;D:\software\apache-maven-3.6.3-bin\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.65\tomcat-embed-core-9.0.65.jar;D:\software\apache-maven-3.6.3-bin\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.65\tomcat-embed-el-9.0.65.jar;D:\software\apache-maven-3.6.3-bin\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.65\tomcat-embed-websocket-9.0.65.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\spring-web\5.3.23\spring-web-5.3.23.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\spring-beans\5.3.23\spring-beans-5.3.23.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\spring-webmvc\5.3.23\spring-webmvc-5.3.23.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\spring-aop\5.3.23\spring-aop-5.3.23.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\spring-context\5.3.23\spring-context-5.3.23.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\spring-expression\5.3.23\spring-expression-5.3.23.jar;D:\software\apache-maven-3.6.3-bin\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\spring-core\5.3.23\spring-core-5.3.23.jar;D:\software\apache-maven-3.6.3-bin\repository\org\springframework\spring-jcl\5.3.23\spring-jcl-5.3.23.jar" com.example.demo.DemoApplication

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

2022-10-06 18:29:19.419  INFO 19036 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 1.8.0_91 on DESKTOP-IG2CVO4 with PID 19036 (C:\Users\feixiaoxing\Desktop\Hello\target\classes started by feixiaoxing in C:\Users\feixiaoxing\Desktop\Hello)
2022-10-06 18:29:19.422  INFO 19036 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2022-10-06 18:29:20.033  INFO 19036 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8090 (http)
2022-10-06 18:29:20.039  INFO 19036 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-10-06 18:29:20.039  INFO 19036 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-10-06 18:29:20.116  INFO 19036 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-10-06 18:29:20.116  INFO 19036 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 661 ms
2022-10-06 18:29:20.382  INFO 19036 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8090 (http) with context path ''
2022-10-06 18:29:20.389  INFO 19036 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.24 seconds (JVM running for 1.724)
2022-10-06 18:30:59.461  INFO 19036 --- [nio-8090-exec-4] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-10-06 18:30:59.461  INFO 19036 --- [nio-8090-exec-4] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-10-06 18:30:59.462  INFO 19036 --- [nio-8090-exec-4] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

8、打开一个浏览器,输入localhost:8090/hello,如果你看到这样的网页,那基本上代表配置ok了,那代表第一个spring boot程序ok了。

java web开发(从spring boot到spring cloud)

9、总结

        和之前spring mvc生成class、lib、resources文件,拷贝到tomcat开启服务不同,spring boot可以直接和web server打包,单独形成一个服务。所以这也就是所谓的微服务。微服务的功能不多,相互之间实现解耦,每一个服务除了对外的接口之外,没有共享的内容,我想着这也许就是微服务的由来把。

        spring boot是spring cloud的重要基础。spring boot一般用来开发单个服务,而spring cloud用于集成很多的服务,并在上面做流量管制,spring cloud上面继承的这些服务一般也是基于spring boot开发的,只是在上面添加了不同的注解,比如@RestController、@EnableConfigServer、@EnableEurekaServer、@EnableCircuitBreaker、@EnableFeinClients等等。

        如果只是开发单个web程序,学习spring boot就可以了。如果需要进一步深造,spring cloud是一个相对比较成熟的方案,值得学习一下。