1. The project is registered as a Linux service
- Can be turned on or off
mvn package java -jar xxxx.jar
pom plus executable
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> </plugins> </build>
Setup and use
Put in file: / var / apps / ch10-0.0 1-SNAPSHOT. jar
Create service: / 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 //Service name ch10. Start after logging. Service content. Multi user.
- On / off status: auto start to view log
systemctl start ch10 #systemctl start ch10.service systemctl stop ch10 systemctl status ch10 systemctl enable ch10.service journalctl -u ch10.service
journal Britain /ˈdʒɜːn(ə)l/ beautiful /ˈdʒɜːrnl/ Global(U.S.A) Concise Oxford Webster Collins Encyclopedia n. Magazines, periodicals, newspapers; Diary, diary; (Accounting) Journal
java war package
<packaging>war</packaging> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Ch10warApplication.class); } }
2. docker deployment
Dockerfile and docker basic commands
- Instructions are capitalized
from ubuntu #Base image maintainer guo #Maintainer RUN #Command to run at compile time, install package CMD #Run time actions. After startup, the default commands and parameters. Variable parameters. There can only be one. entrypoint #When running, it will be executed. Generally, it will not be overwritten. You can use -- entrypoint to overwrite it expose 8080 env myName=guo #env myName guo add test.txt /mydir/
- run cmd entrypoint can be written in two ways
run /bin/bash -c "echo helloworld" run ["/bin/bash","-c","echo hello"] -c Guaranteed use bash shell. from $0 start
cmd echo "test" Can be: docker run -d imageName echo "not test" cover
entrypoint ["/bin/echo"] docker run -d imageName "a test" #The parameter passed to the image is a test
yum install docker #insert systemctl start docker #open systemctl enable docker #Startup and self start
Dockerfile file
FROM java:8 MAINTAINER wyf ADD ch10-0.0.1-SNAPSHOT.jar app.jar EXPOSE 8080 ENTRYPOINT ["java","-jar","/app.jar"]
FROM openjdk:8-jdk-alpine VOLUME /tmp #Hang under this scroll ADD ./target/demojenkins.jar demojenkins.jar ENTRYPOINT ["java","-jar","/demojenkins.jar", "&"]
- Build and run
docker build -t wisely/ch10docker . docker run -d --name ch10 -p 8080:8080 wisely/ch10docker
http://192.168.44.146:8080/
Run the script and dockerFile in the project
src/main/docker/runboot.sh sleep 10 java -Djava.security.egd=file:/dev/./urandom -jar /app/app.jar
src/main/docker/Dockerfile FROM 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 Britain /ˈvɒljuːm/ beautiful /ˈvɑːljuːmˌˈvɑːljəm/ Concise Oxford Webster Collins Encyclopedia n. Volume, volume; Total amount; Volume, loudness; (volume control) knob, control lever; (volume, volume; (periodical) bound book; book;<history>(A scroll written on parchment or papyrus; (esp of hair) thick and thick adj. a large number of v. Issued in volume form; Roll up in groups
maven installs the docker plug-in
- The parent class is introduced in this way
<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>
Compile locally by default. If it is another server: environment variable configuration
DOCKER_HOST tcp://192.168.1.68:2357
mvn clean package docker:build -DskipTests
Failed on his computer. On the Linux server, it succeeded.
docker images see
docker compose view
- docker-compose.yml
postgresdb: image: busybox volumes: - /var/lib/postgresql/data postgres: name: postgres image: postgres hostname: postgres volumes_from: - postgresdb # ports: # - "5432:5432" environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres discovery: image: "discovery:1.0.0-SNAPSHOT" hostname: discovery name: discovery ports: - "8761:8761" config: image: "config:1.0.0-SNAPSHOT" hostname: config name: config links: - discovery environment: EUREKA_HOST: discovery EUREKA_PORT: 8761 # ports: # - "8888:8888" person: image: person:1.0.0-SNAPSHOT hostname: person links: - discovery - config - postgres environment: EUREKA_HOST: discovery EUREKA_PORT: 8761 SPRING_PROFILES_ACTIVE: docker # ports: # - "8082:8082" ui: image: ui:1.0.0-SNAPSHOT hostname: ui links: - discovery - config - person - some environment: EUREKA_HOST: discovery EUREKA_PORT: 8761 SPRING_PROFILES_ACTIVE: docker ports: - "80:80"
docker-compose up -d Indicates background operation
3. boot test
pom entity dao controller
@Spring Application Configuration(classes = Ch104Application.class) //1 replace ContextConfiguration
<dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>runtime</scope> </dependency>
@Entity public class Person { @Id @GeneratedValue private Long id; private String name; } public interface PersonRepository extends JpaRepository<Person, Long> { }
@RestController @RequestMapping("/person") public class PersonController { @Autowired PersonRepository personRepository; @RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE} ) public List<Person> findAll(){ return personRepository.findAll(); } }
test class
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Ch104Application.class) //1 replace ContextConfiguration @WebAppConfiguration @Transactional //2 ensure that the data will be rolled back after each test public class Ch104ApplicationTests { @Autowired PersonRepository personRepository; MockMvc mvc; @Autowired WebApplicationContext webApplicationContext; String expectedJson; @Before //3 initialization before test 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. Expected return json mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } protected String Obj2Json(Object obj) throws JsonProcessingException{//5 convert object to json ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); } @Test public void testPersonController() throws Exception { String uri="/person"; MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)) .andReturn(); //6 get the execution result of a request int status = result.getResponse().getStatus(); //7 status String content = result.getResponse().getContentAsString(); //8 content Assert.assertEquals("Error, the correct return value is 200",200, status); //9 expected results, comparison Assert.assertEquals("Error, the return value is inconsistent with the expected return value", expectedJson,content); //10 } }
4. boot monitoring
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 Britain /ˈprəʊsesə(r)/ beautiful /ˈprɑːsesər/ Global(britain) Concise Oxford Webster Collins Encyclopedia n. (Computer processor (machine); processing program; processing machine; film development company, development company; document processor, document handler
- The full name of HATEOAS is Hypermedia as the engine of application state, which is the ultimate transformation state of 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 "# important }, { "rel": "trace", "href": "http://localhost:8080/trace "# important } ] }
shoutdown
- After configuration, it can be accessed.
- Using post access
endpoints.shutdown.enabled=true
Operation of endpoint
endpoints.beans.id=mybeans #Access via / mybeans endpoints.enabled=false #Close all endpoints endpoints.beans.enabled=true #Open beans endpoints.beans.enabled=false #Close the beans endpoint management.context-path=/manage #Prefix for endpoint access/ manage/beans management.port=8081 #Custom port management.port=-1 #Close http endpoint
5. The project is accessed remotely
JMX
Console input: jconsole
Select project, select org springframework. boot
SSH access
pom configuration and access
<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
Use: Port 2000 help metrics endpoint list endpoint invoke health
use Groovy Language development extension command. Groovy yes Spring Leading, running on JVM Dynamic language. Srping boot It can also be used Groovy development
def bootVersion = context.attributes['spring.boot.version']; def springVersion = context.attributes['spring.version']
6. maven's scope label
Maven
-
Convention Over Configuration
-
Convention over configuration
compile
-
The default value of scope is compile. The dependent project needs to participate in the compilation of the current project,
-
Of course, subsequent tests and operation cycles are also involved,
-
Is a relatively strong dependence. Packaging usually needs to be included.
-
test
- scope is test, which means that the dependent project is only involved in testing related work, including the compilation and execution of test code. A typical example is junit.
runntime
- runntime means that the dependent project does not need to participate in the compilation of the project, but it needs to participate in the later test and run cycle. Compared with compile, it just skips compilation,
- More common, such as JSR ××× The corresponding API jar is compiled, and the specific implementation is runtime. Only knowing the interface is enough for compile.
- oracle jdbc driver rack package is a good example. Generally, the scope is runntime.
- runntime dependency is usually used with optional, which is true. I can implement it with A or B.
provided
-
provided means you don't have to pack it when you pack it,
- Other facilities (Web Container) will be provided.
- In fact, the dependency can theoretically participate in compiling, testing, running and other cycles. It is equivalent to compile, but the exclude action is performed in the packaging phase.
-
The perspective of system participation is the same as that of provided, but the dependent items will not be captured from maven warehouse, but from the local file system. They must be used in conjunction with systemPath property.
Cyclic dependence
A–>B–>C. The current project is a, a depends on B, and B depends on C. If you know the scope of B in project a, how do you know the scope of C in project a? The answer is:
When C is test or provided, C is directly discarded, and A does not depend on C;
Otherwise, A depends on C, and C's scope inherits B's scope.