1. 项目注册为 Linux服务

  • 可以开启 关闭 开机启动
mvn package
java -jar xxxx.jar

pom加executable

 <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><executable>true</executable></configuration></plugin></plugins></build>

设置 和 使用

放入文件:/var/apps/ch10-0.0.1-SNAPSHOT.jar
建立服务:/etc/systemd/system/ch10.service

[Unit]
Description=ch10
After=syslog.target[Service]
ExecStart= /usr/local/jdk1.8.0_271/bin/java -jar /var/apps/ch10-0.0.1-SNAPSHOT.jar[Install]
WantedBy=multi-user.target//服务名 ch10。在 日志后启动。服务的内容。多用户的。
  • 开启 关闭 状态 开机自启 查看日志
systemctl start ch10 #systemctl start ch10.servicesystemctl stop ch10systemctl status ch10systemctl enable ch10.servicejournalctl -u ch10.service
journal
英 /ˈdʒɜːn(ə)l/  美 /ˈdʒɜːrnl/  全球(美国)
简明 牛津 新牛津  韦氏  柯林斯 例句  百科
n. 杂志,期刊,报纸;日记,日志;(会计)日记账

java打 war包

<packaging>war</packaging><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope>
</dependency>
public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Ch10warApplication.class);}
}

2. docker 部署

Dockerfile 和 docker基本命令

  • 指令都是大写
from ubuntu #基镜像
maintainer guo #维护者
RUN #编译时 要运行的命令,安装软件包
CMD #运行时的动作,启动后,默认命令 及参数。可变动的参数。只能有一个。
entrypoint #运行时,一定会执行。一般不会被覆盖 可用 --entrypoint覆盖
expose 8080
env myName=guo #env myName guo
add test.txt /mydir/
  • run cmd entrypoint 两种写法
run /bin/bash -c "echo helloworld"
run ["/bin/bash","-c","echo hello"]-c 保证使用 bash shell。从$0开始
cmd echo "test"
可被: docker run -d imageName echo "not test" 覆盖
entrypoint ["/bin/echo"]
docker run -d imageName "a test" #向镜像 传递的参数是 a test
yum install docker #插入
systemctl start docker #开启
systemctl enable docker #开机自启

Dockerfile文件

FROM java:8MAINTAINER wyfADD ch10-0.0.1-SNAPSHOT.jar app.jarEXPOSE 8080ENTRYPOINT ["java","-jar","/app.jar"]
FROM openjdk:8-jdk-alpine
VOLUME /tmp #挂到 这个卷轴下
ADD ./target/demojenkins.jar demojenkins.jar
ENTRYPOINT ["java","-jar","/demojenkins.jar", "&"]
  • 构建 和 运行
docker build -t wisely/ch10docker .
docker run -d --name ch10 -p 8080:8080 wisely/ch10docker
http://192.168.44.146:8080/

项目中 运行脚本 和 dockerFile

src/main/docker/runboot.shsleep 10
java -Djava.security.egd=file:/dev/./urandom -jar /app/app.jar
src/main/docker/DockerfileFROM java:8
VOLUME /tmp
RUN mkdir /app
ADD config-1.0.0-SNAPSHOT.jar /app/app.jar
ADD runboot.sh /app/
RUN bash -c 'touch /app/app.jar'
WORKDIR /app
RUN chmod a+x runboot.sh
EXPOSE 8888
CMD /app/runboot.sh
VOLUME
英 /ˈvɒljuːm/  美 /ˈvɑːljuːmˌˈvɑːljəm/
简明 牛津 新牛津  韦氏  柯林斯 例句  百科
n. 体积,容积;总数,总量;音量,响度;(控制音量的)旋钮,控制杆;(成套图书中的)卷,册;(期刊)合订本;书籍;<史>(写在羊皮纸或纸草纸上的)书卷;(尤指头发的)厚,多
adj. 大量的
v. 以卷的形式发出;成团卷起

maven 安装docker插件

  • 父类,这样引入
    <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins><pluginManagement><plugins><plugin><groupId>com.spotify</groupId><artifactId>docker-maven-plugin</artifactId><version>0.2.9</version><configuration><skipDockerBuild>true</skipDockerBuild></configuration></plugin></plugins></pluginManagement></build>
    <build><plugins><plugin><groupId>com.spotify</groupId><artifactId>docker-maven-plugin</artifactId><configuration><imageName>${project.name}:${project.version}</imageName><dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory><skipDockerBuild>false</skipDockerBuild><resources><resource><directory>${project.build.directory}</directory><include>${project.build.finalName}.jar</include></resource></resources></configuration></plugin></plugins></build>

默认编译到本地,若是其他服务器:环境变量配置

DOCKER_HOST
tcp://192.168.1.68:2357
mvn  clean package docker:build -DskipTests

在自己电脑上失败了。在Linux 服务器上,成功了。

docker images 查看

docker compose 查看

  • docker-compose.yml
postgresdb:image: busyboxvolumes:- /var/lib/postgresql/datapostgres:name: postgresimage: postgreshostname: postgresvolumes_from:- postgresdb
#  ports:
#   - "5432:5432"environment:- POSTGRES_USER=postgres- POSTGRES_PASSWORD=postgresdiscovery:image: "discovery:1.0.0-SNAPSHOT"hostname: discoveryname: discoveryports:- "8761:8761"config:image: "config:1.0.0-SNAPSHOT"hostname: configname: configlinks:- discoveryenvironment:EUREKA_HOST: discoveryEUREKA_PORT: 8761
#  ports:
#    - "8888:8888"person:image: person:1.0.0-SNAPSHOThostname: personlinks:- discovery- config- postgresenvironment:EUREKA_HOST: discoveryEUREKA_PORT: 8761SPRING_PROFILES_ACTIVE: docker
#  ports:
#    - "8082:8082"ui:image: ui:1.0.0-SNAPSHOThostname: uilinks:- discovery- config- person- someenvironment:EUREKA_HOST: discoveryEUREKA_PORT: 8761SPRING_PROFILES_ACTIVE: dockerports:- "80:80"
docker-compose up -d 表示后台运行

3. boot测试

pom entity dao controller

@Spring Application Configuration(classes = Ch104Application.class) //1 替代 ContextConfiguration
     <dependency><groupId>org.hsqldb</groupId><artifactId>hsqldb</artifactId><scope>runtime</scope></dependency>
@Entity
public class Person {@Id@GeneratedValueprivate Long id;private String name;
}public interface PersonRepository extends JpaRepository<Person, Long> {}
@RestController
@RequestMapping("/person")
public class PersonController {@AutowiredPersonRepository personRepository;@RequestMapping(method = RequestMethod.GET,produces = {MediaType.APPLICATION_JSON_VALUE} )public List<Person> findAll(){return personRepository.findAll();}}

test 类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Ch104Application.class) //1 替代 ContextConfiguration
@WebAppConfiguration
@Transactional //2 确保每次测试后数据都会被回滚
public class Ch104ApplicationTests {@AutowiredPersonRepository personRepository;MockMvc mvc;@Autowired WebApplicationContext webApplicationContext;String expectedJson;@Before //3 测试之前的初始化public void setUp() throws JsonProcessingException{ Person p1 = new Person("wyf");Person p2 = new Person("wisely");personRepository.save(p1);personRepository.save(p2);expectedJson =Obj2Json(personRepository.findAll()); //4 期望返回jsonmvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();}protected String Obj2Json(Object obj) throws JsonProcessingException{//5将对象转换为jsonObjectMapper mapper = new ObjectMapper();return mapper.writeValueAsString(obj);}@Testpublic void testPersonController() throws Exception {String uri="/person";MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn(); //6 获取一个request的执行结果int status = result.getResponse().getStatus(); //7 状态String content = result.getResponse().getContentAsString(); //8 内容Assert.assertEquals("错误,正确的返回值为200",200, status); //9 预期结果,比较Assert.assertEquals("错误,返回值和预期返回值不一致", expectedJson,content); //10}}

4. boot监控

pom

 <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.hateoas</groupId><artifactId>spring-hateoas</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies>
processor
英 /ˈprəʊsesə(r)/  美 /ˈprɑːsesər/  全球(英国)
简明 牛津 新牛津  韦氏  柯林斯 例句  百科
n. (计算机的)处理器(机);处理程序;加工机(者);胶片冲洗公司,显影公司;文件处理者,文件经办人
  • HATEOAS全称是Hypermedia as the engine of application state,它是RESTful的终极变身状态,

actuator

http://localhost:8080/actuator{"links": [{"rel": "self","href": "http://localhost:8080/actuator"},{"rel": "metrics","href": "http://localhost:8080/metrics" },{"rel": "info","href": "http://localhost:8080/info" },{"rel": "shutdown","href": "http://localhost:8080/shutdown"},{"rel": "env","href": "http://localhost:8080/env"},{"rel": "dump","href": "http://localhost:8080/dump" },{"rel": "status","href": "http://localhost:8080/status"},{"rel": "mappings","href": "http://localhost:8080/mappings" },{"rel": "autoconfig","href": "http://localhost:8080/autoconfig" },{"rel": "health","href": "http://localhost:8080/health" },{"rel": "configprops","href": "http://localhost:8080/configprops" },{"rel": "beans","href": "http://localhost:8080/beans" #重要},{"rel": "trace","href": "http://localhost:8080/trace" #重要}]
}

shoutdown

  • 配置后,才可访问。
  • 使用post访问
endpoints.shutdown.enabled=true

端点的操作

endpoints.beans.id=mybeans  #通过 /mybeans 访问endpoints.enabled=false #关闭所有的端点endpoints.beans.enabled=true #打开beans
endpoints.beans.enabled=false #关闭 beans端点management.context-path=/manage #端点访问的前缀。 /manage/beansmanagement.port=8081 #自定义端口
management.port=-1 #关闭 http端点

5. 项目通过远程访问

JMX

控制台输入:jconsole

选择项目,选择 org.springframework.boot

SSH访问

pom 配置 和访问

     <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-remote-shell</artifactId></dependency>
shell.auth.simple.user.name=wyf
shell.auth.simple.user.password=wyf
使用:端口为 2000
help
metrics
endpoint list
endpoint invoke health
使用 Groovy语言开发 扩展命令。
Groovy 是 Spring 主导,运行于 JVM 的动态语言。
Srping boot 也可以用 Groovy开发
     def bootVersion = context.attributes['spring.boot.version'];def springVersion = context.attributes['spring.version']

6. maven 的 scope标签

Maven

  • Convention Over Configuration

  • 约定优于配置

compile

  • scope的默认值是compile,被依赖项目需要参与当前项目的编译,

    • 当然后续的测试,运行周期也参与其中,

    • 是一个比较强的依赖。打包的时候通常需要包含进去。

test

  • scope为test表示依赖项目仅仅参与测试相关的工作,包括测试代码的编译,执行。比较典型的如junit。

runntime

  • runntime表示被依赖项目无需参与项目的编译,不过后期的测试和运行周期需要其参与。与compile相比,跳过编译而已,

    • 比较常见的如JSR×××的实现,对应的API jar是compile的,具体实现是runtime的,compile只需要知道接口就足够了。
    • oracle jdbc驱动架包就是一个很好的例子,一般scope为runntime。
    • runntime的依赖通常和optional搭配使用,optional为true。我可以用A实现,也可以用B实现。

provided

  • provided意味着打包的时候可以不用包进去,

    • 别的设施(Web Container)会提供。
    • 事实上该依赖理论上可以参与编译,测试,运行等周期。相当于compile,但是在打包阶段做了exclude的动作。
  • system 参与角度与 provided相同,不过被依赖项不会从maven仓库抓,而是从本地文件系统拿,一定需要配合systemPath属性使用

循环依赖

A–>B–>C。当前项目为A,A依赖于B,B依赖于C。知道B在A项目中的scope,那么怎么知道C在A中的scope呢?答案是:
当C是test或者provided时,C直接被丢弃,A不依赖C;
否则A依赖C,C的scope继承于B的scope。

注册为Linux服务,docker部署,dockerfile,spring boot测试监控,actuator,项目远程访问,maven的scope标签相关推荐

  1. Spring Boot应用监控实战

    概述 之前讲过Docker容器的可视化监控,即监控容器的运行情况,包括 CPU使用率.内存占用.网络状况以及磁盘空间等等一系列信息.同样利用SpringBoot作为微服务单元的实例化技术选型时,我们不 ...

  2. 第8章Spring Boot整合监控

    8.1 使用actuator监控 8.1.1 actuator是什么 在Spring Boot的众多Starter POMs中有一个特殊的模块,不同于其他模块大多用于开发业务功能或连接一些其他外部资源 ...

  3. Docker容器及Spring Boot微服务应用

    2019独角兽企业重金招聘Python工程师标准>>> Docker容器及Spring Boot微服务应用 1 什么是Docker 1.1 Docker的出现 问题一:项目实施环境复 ...

  4. 带有PostgreSQL的Docker Compose for Spring Boot应用程序

    在此博客文章中,您将学习如何使用PostgreSQL配置Spring Boot应用程序以与Docker Compose一起运行. 这篇博客文章涵盖: Spring Boot应用程序Dockerfile ...

  5. linux 查看目录拓扑图,Linux服务篇之九:构建Cacti监控平台

    Linux服务篇之九:构建Cacti监控平台 作为一名Linux SA,日常最重要的就是保证网站正常稳定的运行,我们需要实时监控网站.服务器的运行状态,这时需要借助开源软件(cacti.nagios. ...

  6. Spring Boot 应用监控:Actuator与 Admin

    第 III 部分Spring Boot 系统监控.测试与运维 Spring Boot 应用监控:Actuator与 Admin <Spring Boot 实战开发>(陈光剑) -- 基于 ...

  7. sm4 前后端 加密_这7个开源的Spring Boot前后端分离项目整理给你

    来源|公众号:江南一点雨 前后端分离已经开始逐渐走进各公司的技术栈,不少公司都已经切换到前后端分离开发技术栈上面了,因此建议技术人学习前后端分离开发以提升自身优势.同时,也整理了 7 个开源的 Spr ...

  8. 使用Testcontainers和PostgreSQL,MySQL或MariaDB的Spring Boot测试

    Testcontainers是一个Java库,可轻松将Docker容器集成到JUnit测试中. 在Containerized World中 ,将测试配置与嵌入式数据库和服务复杂化几乎没有意义. 而是使 ...

  9. Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序

    1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...

最新文章

  1. TinyML-TVM是如何驯服Tiny的(上)
  2. Docker 容器的网络连接
  3. 如何为JBoss Developer Studio 8设置集成和SOA工具
  4. 类型全部为string_TypeScript 高级类型总结(含代码案例)
  5. 已知前序中序求层序 c语言递归,二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现...
  6. gitlab ProjectMembers
  7. mongodb修改最大连接数
  8. WCF服务编程-非WCF应用程序使用WCF服务
  9. Python安装注意事项
  10. 161128、Redis 4.0发布及其新功能介绍
  11. 如何让那些模糊的照片变得高清?不会PS也能解决
  12. html如何让英文自动换行,HTML如何让英文自动换行不断词
  13. 跨界打劫!中医保健店用一招免费洗车,快速引流进店,月赚20万
  14. HTTPS 加密、证书、签名与握手
  15. python正态分布拟合_用python拟合正态分布(已开源)
  16. QQ小程序开发者工具及官网
  17. Android左右声道切换流程
  18. 鱼眼深度估计!环视近场感知系列之几何预测
  19. 尚硅谷mycat2.0安装和配置
  20. 情不知所起,无以而终

热门文章

  1. Pytorch神经网络入门(1)参数的手动更新和自动更新
  2. 读《信息传》总结——决定我们未来发展的方法论
  3. 视觉SLAM总结,PNP:利用3D-2D点估计相机运动
  4. 跨境电商独立站推广YouTube 免费营销攻略!
  5. springboot项目实现导出pdf功能,这也太简单了吧
  6. java字符串转换成时间Could not parse date: Unparseable date: 2018-12-28]
  7. 双击映射坚果云网盘并打开的AHK源代码
  8. rman的配置和备份
  9. Redis实现消息队列的方式
  10. 医药信息技术项目管理重点题