ãã®èšäºã§ã¯ãSpring Bootã§å°ããªãããžã§ã¯ããäœæããæŠéãµãŒããŒã«å±éããããã«å¿ èŠãªãã¹ãŠã®æé ã®æŠèŠã説æããŸãã
JavaãšSpringã®å²åŠã«é¢ããé·ãåå¥æ²ãåŒãããåºããªãã§ãããã«ããžãã¹ã«åãããããŸãã
ãŸããå¿ èŠãªãã¹ãŠã®ãã¯ãããžãŒåç©åïŒå°ãªããšãSpringãJPAãJDBCïŒãå®è£ ããŠãã¢ããªã±ãŒã·ã§ã³ãã¬ãŒã ã¯ãŒã¯ãäœæããå¿ èŠããããŸãã ã¹ããªã³ã°ããŒããç»å Žããåã¯ããã¡ããã³ãŒããã³ã«äœæ¥çšã®ãã©ããããªãéããããã«å€ãã®æéãè²»ããå¿ èŠããããŸããã ãããŠãå€ãã®äººãJavaã§å°ããªWebãããžã§ã¯ããéçºããã®ãæ¢ããã®ã¯ããã®ãããªãã¬ãŒã ã¯ãŒã¯ãäœæããè€éãã§ãã ãã¡ãããããã©ãŒãã³ã¹ã®äœäžïŒhiã®åŽé¢ïŒã«äŒŒããã¬ãŒã ã¯ãŒã¯ãäœæããããšãã§ããªãã£ãäžèªç±ãªæ¥ã®ã«ãŒããããšã¯ãããããã§ãæ§æãã¡ã€ã«ã®æ°ãšè€éãã«ãããèšç·ŽãããŠããªãéçºè ã¯é·ãéããããçèããŸããã ãã ããBootãšSpring 4ã®ç»å Žã«ãããç掻ãå°ã楜ã«ãªããæ§æãã¡ã€ã«ã®æ°ãèããæžå°ããŸããã
ããããã¬ãŒã ãã¯ãã
Intellij Idea 14.1ããæã¡ã®å Žåããã¬ãŒã ã¯ãŒã¯ã«åé¡ã¯ãªãã¯ãã§ãããããžã§ã¯ããäœæããããã®ç¹å¥ãªãŠã£ã¶ãŒãïŒFile-New-Project ...- Spring InitializrïŒã䜿çšããŠãã¹ãŠãå®è¡ã§ããŸãã 次ã«ããããžã§ã¯ãã®ååã瀺ããé¢å¿ã®ãããã¯ãããžïŒWebãJDBCãJPAãPostgreSQLïŒãéžæããŠãããžã§ã¯ããäœæããã ãã§ãã
ãã®IDEããªãå Žåã¯ã Spring Boot CLIãããŠã³ããŒãããŠãINSTALL.txtã®æ瀺ã«åŸã£ãŠãã ããã ã·ã¹ãã å€æ°SPRING_HOMEïŒbinãã©ã«ããŒã§ã¯ãªããSpring Bootã®ãã©ã«ããŒãžã®ãã¹ïŒïŒãèšå®ããWindowsã®PATHã·ã¹ãã å€æ°ã«SPRING_HOME / binãžã®ãã¹ãè¿œå ããå¿ èŠããããŸãã
ããã§ãSpring Consoleãã»ããã¢ããããŸãããä»åºŠã¯ãããžã§ã¯ããäœæããŸãã ãããè¡ãã«ã¯ã次ã®ã³ãã³ãã䜿çšããŸãã
spring init --dependencies=web,data-jpa,jdbc yourapp
æŽæ°
ããã«ãã³ã¡ã³ãã§æžããããã«ãWebã³ã³ã¹ãã©ã¯ã¿ããããŸãïŒ start.spring.io
次ã«ãçµæã®ãã¬ãŒã ã¯ãŒã¯ããæ°ã«å ¥ãã®IDEã«ã€ã³ããŒãããããŒãºã«åãããŠå€æŽãéå§ããŸãã
æåã«ãwebappsãã©ã«ããŒãsrc /ã¡ã€ã³ãã£ã¬ã¯ããªã«è¿œå ããŸãã æ¥ãæãããã«ãresourcesãã©ã«ããŒã§ã¯ãªãããã®äžã«ãã¹ãŠã®WebãªãœãŒã¹ãäœæããŸãã å®éããªãœãŒã¹ãã©ã«ããŒã«ãã¡ã€ã«ãäœæãããšããµãŒããŒãåèµ·åããã«WebãªãœãŒã¹ã«å ããããå€æŽã確èªããæ©äŒã倱ãããŸãã ãŸããWebããŒãžäžã®å€æŽãããããã¹ãã衚瀺ããããã«WebãµãŒããŒãåèµ·åããå¿ èŠãããå Žåãããã¯ã€ã©ã€ã©ããå¯èœæ§ããããŸãã
次ã«ãwebappsãã©ã«ããŒã«ãé©åãªãªãœãŒã¹ãé 眮ããindex.htmlãã¡ã€ã«ãšcssãjsãfontãimagesãã©ã«ããŒãäœæããŸãã
ããšãã°ãæãåçŽãªindex.htmlãã¬ãŒã ã¯ãŒã¯ãäœæããŸãããã
<!DOCTYPE html> <html lang="en"> <head> <title>Yourapp</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <h1> HELLO WORLD </h1> </body> </html>
pom.xmlãã¡ã€ã«ãå€æŽããŸã
次ã®ãããªãã®ãåŸãããã¯ãã§ãã
<?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.yourcompany</groupId> <artifactId>yourapp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>YourApp</name> <description></description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <start-class>com.yourcompany.Application</start-class> <java.version>1.8</java.version> </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-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.4-1201-jdbc41</version> <scope>runtime</scope> </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> <version>2.6</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/classes/static</outputDirectory> <resources> <resource> <directory>src/main/webapp</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
pomãã¡ã€ã«ãã次ã®ããšãããããŸãã
Java 8ã䜿çšããŸãïŒè©ŠããŠã¿ãŸãããïŒã ã¢ããªã±ãŒã·ã§ã³ã¯ã©ã¹ã¯com.yourcompany.ApplicationãšåŒã°ããŸãïŒããšãã°ãDemoApplicationãšåŒã°ããæšæºçæã¯ã©ã¹ã®ååãå¿ããªãã§ãã ããïŒã
postgresql 9.4ã䜿çšããŸãïŒã䜿ãã®ãã·ã³ã«ããŒã«ã«ã«ã€ã³ã¹ããŒã«ããã®ãããã§ãããïŒã ããŒã¿ããŒã¹ãšã®ããåãã®ããã®æ¥ç¶ããŒã«ã¯ãæããã¡ãã·ã§ããã«ã§çç£æ§ã®é«ãïŒHikariCPïŒã§ãã ããã«ãç¹å¥ãªãã©ã°ã€ã³ã䜿çšããŠãæçµçãªjarãçæãããšãã«ãã¹ããªã³ã°ããŒããå¿ èŠãšããããã«ããã¹ãŠã®ããŒã¿ãwebappããresources / staticã«è»¢éããŸãã ããããªããšãjarããã¯ããŒã ã®å®è¡æã«webappsãã©ã«ããŒã«äœæãããã¹ãŠã®WebããŒãžã衚瀺ã§ããŸããã
æ§æããã±ãŒãžãè¿œå ãããã®äžã«JpaConfigã¯ã©ã¹ãäœæããŸãã
@Configuration @EnableTransactionManagement @EnableJpaRepositories(basePackageClasses = Application.class) public class JpaConfig implements TransactionManagementConfigurer { @Value("${dataSource.driverClassName}") private String driver; @Value("${dataSource.url}") private String url; @Value("${dataSource.username}") private String username; @Value("${dataSource.password}") private String password; @Value("${hibernate.dialect}") private String dialect; @Value("${hibernate.hbm2ddl.auto}") private String hbm2ddlAuto; @Bean public DataSource configureDataSource() { HikariConfig config = new HikariConfig(); config.setDriverClassName(driver); config.setJdbcUrl(url); config.setUsername(username); config.setPassword(password); return new HikariDataSource(config); } @Bean public LocalContainerEntityManagerFactoryBean configureEntityManagerFactory() { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(configureDataSource()); entityManagerFactoryBean.setPackagesToScan("com.yourcompany"); entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); Properties jpaProperties = new Properties(); jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, dialect); jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, hbm2ddlAuto); entityManagerFactoryBean.setJpaProperties(jpaProperties); return entityManagerFactoryBean; } @Bean public PlatformTransactionManager annotationDrivenTransactionManager() { return new JpaTransactionManager(); } }
ããã«ãapplication.propertiesãã¡ã€ã«ã«æ¬¡ã®è¡ãè¿œå ããŸãã
dataSource.driverClassName=org.postgresql.Driver dataSource.url=jdbc:postgresql://<ip- , PostgreSQL>:5432/yourapp_data dataSource.username=postgres dataSource.password= hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect hibernate.hbm2ddl.auto=update
æåŸã«ãApplication.javaã§åæåæååã次ã®ããã«å€æŽããŸãã
SpringApplication.run(new Class<?>[] {Application.class, JpaConfig.class}, args);
ãããã£ãŠãPostgreSQL DBMSãžã®æ¥ç¶ãæ§æããŸããã
ããŒã¿ããŒã¹èªäœãšåçŽãªããŒãã«ãäœæããããšãå¿ããªãã§ãã ããã ããã¯ãPgAdminã䜿çšããŠè¡ãã®ãæã䟿å©ã§ãã
空ã®yourapp_dataããŒã¿ããŒã¹ãäœæããåŸãããŒãã«äœæã¹ã¯ãªãããå®è¡ããŸãã
CREATE TABLE yourapp_data ( data_id uuid NOT NULL, data_description character varying(100) NOT NULL, CONSTRAINT yourapp_data_pk PRIMARY KEY (data_id) ) WITH ( OIDS=FALSE ); ALTER TABLE yourapp_data OWNER TO postgres;
ããããããžã§ã¯ãã«å°ãè©°ã蟌ã¿ãŸãããã ã€ãŸããäœããã®çš®é¡ã®ããŒã¿ããŒã¹ãšã³ãã£ãã£ãè¿œå ããŠæäœæ¹æ³ãåŠç¿ããã¯ã©ã€ã¢ã³ããã圢æã®ããã«ããŒã¿ãåä¿¡ããæ¢ã«äœæããããšã³ãã£ãã£ã«é¢ããåãããŒã¿ãã¯ã©ã€ã¢ã³ãã«éä¿¡ããŸãã
ã³ã³ãããŒã©ããšã³ãã£ãã£ããªããžããªããµãŒãã¹ãutilsããã±ãŒãžãäœæããŸãã
ãšã³ãã£ãã£ããã±ãŒãžã§ãã€ã³ã¿ãŒãã§ã€ã¹ãäœæããŸãã
public interface DomainObject extends Serializable { }
ãããŠæ¬è³ªïŒ
public class Data implements DomainObject { private UUID id; private String description; public Data(UUID id, String description) { this.id = id; this.description = description; } public UUID getId() { return id; } public void setId(UUID id) { this.id = id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
ãã®äŸã§ã¯JPAããã³Hibernateã¢ãããŒã·ã§ã³ã䜿çšããŸããããããã®ãã¯ãããžãŒã«ããäœæ¥ãå€§å¹ ã«é ããªãããïŒèŠæ±ã¯çŽç²ãªjdbcããã10åé ãå®è¡ã§ããïŒãORMãå®éã«å¿ èŠãšãããéåžžã«è€éãªãšã³ãã£ãã£ããªãããããã®åŸãéåžžã®jdbcTemplateã䜿çšããŸãã
ãªããžããªã€ã³ã¿ãŒãã§ã€ã¹ãäœæããŸãã
public interface DataRepository<V extends DomainObject> { void persist(V object); void delete(V object); Set<String> getRandomData(); }
ãããŠãã®å®è£ ïŒ
@org.springframework.stereotype.Repository("dataRespitory") public class DataRepositoryImpl implements DataRepository<Data> { @Autowired protected JdbcOperations jdbcOperations; @Override public void persist(Data object) { Object[] params = new Object[] { object.getId(), object.getDescription() }; int[] types = new int[] { Types.VARCHAR, Types.VARCHAR }; jdbcOperations.update("INSERT INTO yourapp_data(\n" + " data_id, data_description)\n" + " VALUES (cast(? as UUID), ?);", params, types); } @Override public void delete(Data object) { jdbcOperations.update("DELETE FROM yourapp_data\n" + " WHERE data_id = '" + object.getId().toString() + "';"); } @Override public Set<String> getRandomData() { Set<String> result = new HashSet<>(); SqlRowSet rowSet = jdbcOperations.queryForRowSet("SELECT data_description FROM yourapp_data p ORDER BY RANDOM() LIMIT 50;"); while (rowSet.next()) { result.add(rowSet.getString("data_description")); } return result; } }
ãã§ã«èª¬æããjdbcTemplateã®ä»£ããã«ãã芧ã®ãšãããã€ã³ã¿ãŒãã§ã€ã¹ã§ããJdbcOperationsã䜿çšããŸãã ã€ã³ã¿ãŒãã§ã€ã¹ã¯ã©ãããã§ãå®è£ ããåé¢ããå¿ èŠããããŸãã第äžã«ãã¹ã¿ã€ãªãã·ã¥ã§ãã¡ãã·ã§ããã«ã§è¥ã ããã第äºã«ãæ¥ã¯ãªããžã§ã¯ãã«æšæºjdk'shnyãããã·ã䜿çšãããããå®è£ ãçŽæ¥æ³šå ¥ã§ããªãããã§ããæ¬æ Œçãªã¢ã¹ãã¯ããšAspectJã³ã³ãã€ã«æãŠã£ãŒãã³ã°ã玹ä»ãããŸã§ã ç§ãã¡ã®å Žåãã¢ããªã±ãŒã·ã§ã³ããªãŒããŒããŒããããªãããã«ããã¯å¿ èŠãããŸããã
ãã§ã«å°ããããŸãã ãµãŒãã¹ãäœæããŸãïŒåªããéçºè ã§ãããããžãã¹ããžãã¯ãDBMSãšé£æºããããžãã¯ããåé¢ããå¿ èŠããããŸããïŒïŒã
ã€ã³ã¿ãŒãã§ãŒã¹ïŒ
public interface DataService { public boolean persist(String problem); public Set<String> getRandomData(); }
å®è£ ïŒ
@Service("dataService") public class DataServiceImpl implements DataService { private static final Logger LOG = LoggerFactory.getLogger(DataServiceImpl.class); @Autowired @Qualifier("dataRespitory") private DataRepository dataRepository; @Override public boolean persist(String problem) { try { dataRepository.persist(new Data(UUID.randomUUID(), problem)); return true; } catch (Exception e) { LOG.error("ERROR SAVING DATA: " + e.getMessage(), e); return false; } } @Override public Set<String> getRandomData() { return dataRepository.getRandomData(); } }
çŽ æŽãããã 次ã«ãã³ã³ãããŒã©ãŒã®å®è£ ã«å¿ èŠãªãã«ããŒã¯ã©ã¹ãããã€ãäœæããŸãã
public class RestException extends Exception { public RestException() { } public RestException(String message) { super(message); } public RestException(String message, Throwable cause) { super(message, cause); } public RestException(Throwable cause) { super(cause); } public RestException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
ãããäŸå€ã®å®è£ ã§ãã å¿ é ã§ã¯ãããŸããããå°æ¥äŸ¿å©ã«ãªãå¯èœæ§ããããŸããã次ã®ã¯ã©ã¹ãé¢é£ä»ããããŠããŸãã
@Controller public class ExceptionHandlerController { private static final Logger LOG = Logger.getLogger(ExceptionHandlerController.class); @ExceptionHandler(RestException.class) public @ResponseBody String handleException(RestException e) { LOG.error(": " + e.getMessage(), e); return ": " + e.getMessage(); } }
ã³ã³ãããŒã©ãŒã§ãã®ãããªãšã©ãŒããã£ããããå Žåããã®ã¡ãœããã§ããã«åŠçãããŸãã
æåŸã«ãã¯ã©ã€ã¢ã³ãã«éä¿¡ããããã®ããŒã¿æ§é ã圢æããå°ããªã¯ã©ã·ãã¯ãäœæããŸãã
public class Ajax { public static Map<String, Object> successResponse(Object object) { Map<String, Object> response = new HashMap<String, Object>(); response.put("result", "success"); response.put("data", object); return response; } public static Map<String, Object> emptyResponse() { Map<String, Object> response = new HashMap<String, Object>(); response.put("result", "success"); return response; } public static Map<String, Object> errorResponse(String errorMessage) { Map<String, Object> response = new HashMap<String, Object>(); response.put("result", "error"); response.put("message", errorMessage); return response; } }
ãã«ããŒã¯ã©ã¹ã®ãã¹ãŠãå®äºããŸããã ã³ã³ãããŒã©ãŒãæžãããšã¯æ®ã£ãŠããŸãã ã³ã«ã¯ãšåããããç°¡åã§ãïŒ
@Controller public class DataController extends ExceptionHandlerController { private static final Logger LOG = Logger.getLogger(DataController.class); @Autowired @Qualifier("dataService") private DataService dataService; @RequestMapping(value = "/persist", method = RequestMethod.POST) public @ResponseBody Map<String, Object> persist(@RequestParam("data") String data) throws RestException { try { if (data == null || data.equals("")) { return Ajax.emptyResponse(); } dataService.persist(data); return Ajax.emptyResponse(); } catch (Exception e) { throw new RestException(e); } } @RequestMapping(value = "/getRandomData", method = RequestMethod.GET) public @ResponseBody Map<String, Object> getRandomData() throws RestException { try { Set<String> result = dataService.getRandomData(); return Ajax.successResponse(result); } catch (Exception e) { throw new RestException(e); } } }
2ã€ã®æ¹æ³ããããŸã-åä¿¡ããããŒã¿ãä¿åããã©ã³ãã ããŒã¿ã®äžéšãã¯ã©ã€ã¢ã³ãã«æäŸããŸãã ã³ã³ãããŒã©ãŒã¯ãåã«äœæããExceptionHandlerControllerããç¶æ¿ãããŸãã äŸå€åŠçã¯ãã³ãã¬ãŒããšããŠã®ã¿èšè¿°ãããŠãããããã«å¿ããŠèª¿æŽããå¿ èŠããããŸãã
ãã®ããããµãŒããŒã³ãŒãã®äž»èŠéšåãèšè¿°ãããã¯ã©ã€ã¢ã³ãã§ã®åäœã確èªããå¿ èŠããããŸãã ãããè¡ãã«ã¯ãindex.htmlãã¡ã€ã«ãæ¹è¯ãããšåæã«ãjqueryã©ã€ãã©ãªãjsãã£ã¬ã¯ããªã«è¿œå ããå¿ èŠããããŸãã
index.htmlïŒ
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>YourApp</title> <script src="js/jquery-2.1.3.min.js"></script> </head> <body> <h1> HELLO WORLD </h1> <input type="text" id="data"/> <a id="post" href="#">POST</a> <a id="get" href="#">GET</a> <div id="container"></div> </body> <script> $('#get').click(function () { $.ajax({ type: "GET", cache: false, url: '/getRandomData', data: "", success: function (response) { var html = ""; $.each(response.data, function (i) { html = html + response.data[i] + "<br/>"; }); $('#container').html(html); } }); }); $('#post').click(function () { if (!$("#data").val()) { alert("Enter your data!"); } else { $.ajax({ type: "POST", cache: false, url: '/persist', data: { 'data': $("#data").val() }, success: function (response) { $('#get').click(); } }); } }); </script> </html>
ã¯ããUIã¯ããã»ã©çŸããã¯ãããŸããã§ãããããã®å©ããåããŠã¢ããªã±ãŒã·ã§ã³ããã¹ãã§ããŸãã
ãããžã§ã¯ããå®è¡ããŸãã Intellij Ideaã§ã¯ãããã¯ç¹å¥ãªã¹ã¿ãŒãã¢ããæ§æïŒã¹ããªã³ã°ããŒãïŒã§å®è¡ã§ããŸãã
ãã¹ãŠãæ£ããå®äºããããlocalhostïŒ8080ã§Hello WorldããããŒãå ¥åè¡ã2ã€ã®ãã¿ã³ã確èªã§ããŸãã å ¥åè¡ã«äœããå ¥åããŠãPOSTãã¿ã³ãã¯ãªãã¯ããŠã¿ãŠãã ããã ãã®åŸãå ¥åãã£ãŒã«ãã®äžã«åæ§ã®ããã¹ãã衚瀺ãããå Žåããã¹ãŠãæ£åžžã«æ©èœããŠããŸãã ããŒãºã«åãããŠãããžã§ã¯ããå€æŽãããã¡ãã·ã§ããã«ãªUIïŒäŸïŒ materializecss.com ïŒãè¿œå ããåççã§èŠªåãªæ°žé ã®ãã®ãäœæããŸãã
ããããé ããæ©ãããããªãã¯ããªããæãããšãããã§ãããããããŠãåé¡ã¯ããªãã®é è³ã倧è¡ã«ããããæ¹æ³ã§ãã ããã¯èšäºã®ç¬¬2éšã«ãªããŸãã
å°ãããªãããéèŠãªãã®ããå§ããŸãããã
ãããžã§ã¯ããå°ããå Žåã§ããç¬èªã®ãã¡ã€ã³ãå¿ èŠã§ãã ã¢ã€ãã¢ããã èµ°ãåã£ãŠããã ãã§ãåãgodaddyã«ãã¡ã€ã³ãç»é²ããã®ã«ããŸããéããããããªãå Žåã¯ãç¡æã®ä»£æ¿æ段freenom.comã䜿çšã§ããŸãã
ãã®ãµãŒãã¹ã§ã¯ããã¡ã€ã³ã.tkã.mlã.gaã.cfã.gqãŸãŒã³ã«ç¡æã§ç»é²ã§ããŸã
ã¯ããæè¯ã®ãŸãŒã³ã§ã¯ãããŸãããã次ã®ãšããã§ãã
次ã«ãããããã¹ãŠãå転ãããµãŒããŒãåãäžããŸãããã ãããžã§ã¯ãã¯å°ããã®ã§ããµãŒããŒã¯å°ããåãŸããŸãã çæ³çã«ã¯ååãªVPSã www.digitalocean.comãªã©ãããŸããŸãªå Žæã§å ¥æã§ããŸãã
ç»é²ããŠãæãåçŽãªããããã¬ãããäœæãããã®äžã«ubuntuãé 眮ããŸãïŒç§ã®å Žåã¯ubuntu 12.04ã§ãããã®ã·ã¹ãã ã®è©³çŽ°ãªæé ã«ã€ããŠèª¬æããŸãããæ®ãã¯ã»ãŒåãã§ãïŒ
ããŠããµãŒããŒããããŸãããããžã§ã¯ããã¢ããããŒãããŸãã
éå§ããã«ã¯ããããžã§ã¯ãmaven'omãåéããŸãã IDEãä»ããŠãããè¡ãããææªã®å Žåããããžã§ã¯ãã®ã«ãŒããã£ã¬ã¯ããªã«ç§»åããŠmvn clean installã³ãã³ããçºè¡ããŸãïŒWindowsã®ã·ã¹ãã å€æ°ãã¹ã§mavenãžã®ãã¹ãæå®ããå¿ èŠããããŸãïŒã ã³ãã³ããå®äºãããšãã¢ã»ã³ãã«ãããjarã¯ããŒã«ã«ãªããžããªïŒããã©ã«ãã§ã¯.m2ãšããååïŒã«é 眮ããããããããã«ããŠãµãŒããŒã«éä¿¡ã§ããŸãã
Windowsãå®è¡ããŠããå Žåããã¡ã€ã«ããµãŒããŒã«è»¢éããã«ã¯ã WinSCPã䜿çšããŸãã
次ã«ãWindowsã®puttyãŸãã¯Linuxã®sshã䜿çšããŠãµãŒããŒã«ã¢ã¯ã»ã¹ããŸãã
jar-nicknameãã³ããŒããããã£ã¬ã¯ããªã«ç§»åããjava -jar youapp.jarã³ãã³ãã§å®è¡ããããšããŸã
ãããããããŸããããŸããã§ããã ãããŠããªãã§ããïŒ ç§ãã¡ã®ãããžã§ã¯ãã¯Java 8ã§äœæãããŸãããjava-versionã³ãã³ãã䜿çšããŠããµãŒããŒäžã«ããJavaã確èªã§ããŸãã ã»ãšãã©ã®å Žåã6ãŸãã¯7ã§ãã
ãããã倱æããªãããã«ãæ°ããããŒãžã§ã³ãèšå®ããŸãã
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java8-installer
ä»ã§ã¯postgresã®çªã§ãã ãã®åã«ãéçºè ã®ãã·ã³ã§ããŒã«ã«ããŒãžã§ã³ã䜿çšããŸãããä»åºŠã¯ãDBMSããµãŒããŒã«é 眮ããŸãã
ãããè¡ãã«ã¯ããŸãã³ãã³ãã®ããžãã¯ã·ãŒã±ã³ã¹ãå®è¡ããŸãã
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' sudo apt-get install wget ca-certificates wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update sudo apt-get upgrade sudo apt-get install postgresql-9.4 postgresql-contrib-9.4
postgresãå®è¡ããŸãã
sudo service postgresql start
次ã«ãpsql loginã³ãã³ããå®è¡ããŸãã
sudo -u postgres psql
ãã¹ã¯ãŒããèšå®ããŸãã
\password postgres
ãããŠã\ qã³ãã³ãã䜿çšããŠçµäºããŸã
ãã¡ã€ã«/etc/postgresql/9.4/main/postgresql.confãç·šéããè¡#listen_addresses = 'localhost'ãlisten_addresses = '*'ã«å€æŽããŸã
ãããã£ãŠãpgadminã䜿çšããŠå€éšããpostgresqlã«æ¥ç¶ã§ããŸãã ãã¡ãããã»ãã¥ãªãã£äžã®çç±ãããããåé¿ããããšããå§ãããŸãããã¹ãŠãèšå®ããã³ãããã°ããããããã®æ©èœãç¡å¹ã«ããŸãã
次ã«ããã¡ã€ã«/etc/postgresql/9.4/main/pg_hba.confãç·šéããŸã
次ã®ããã«ã2ã€ã®æ°ããè¡ãè¿œå ãã127.0.0.1ã®1è¡ãå€æŽããå¿ èŠããããŸãã
host all all 127.0.0.1/32 trust host all all <ip- >/32 trust host all all <ip- >/32 trust
ç§ã¯å人çã«ãããžã§ã¯ãã®èµ·åã«åé¡ããããããã«ãã£ãŠäžããããã¢ãã¬ã¹ã®ãã¹ã¯ãŒãæ€èšŒãç¡å¹ã«ããŠãããããmd5ãä¿¡é Œããããã«æå³çã«å€æŽããŸããã æã£ãŠããªããããããŸããã
ããã§ãã¹ãŠãèšå®ãããŸããã postgresãç¡éã«èª¿æŽããããšã¯ã§ããŸãããå°ããªãããžã§ã¯ããããªããããä»ã®ãšããã¯ãã®ãŸãŸã«ããŸãã
postgresãåèµ·åããŸãã
ãã®åäœã確èªããŸããsudo service postgresql restart
postgresã®ã»ããã¢ããã§ããã¹ãŠãå®äºããŸãããã¹ã¯ãªããã®æ¬¡ã¯äœã§ããïŒ
åè¿°ã®ããã«ãjava -jar youapp.jarã³ãã³ãã§ãçµã¿ç«ãŠãããjarãå®è¡ã§ããŸãã
ãã ãããã®ãããªèµ·åã§ã¯ãå€éšãããµã€ãã«ã¢ã¯ã»ã¹ããã«ã¯ãããŒãïŒããã©ã«ãã§ã¯8080ïŒãç»é²ããå¿ èŠããããŸãã ãŠãŒã¶ãŒãã¢ãã¬ã¹ãå ¥åããã ãã§ãµã€ãã«ã¢ã¯ã»ã¹ã§ããããã«ããããã·ãµãŒããŒãå¿ èŠã§ãã äºåèšå®ãå¿ èŠãªnginxã䜿çšããããšã¯å¯èœã§ãã
nginxãã€ã³ã¹ããŒã«ããŸãã
sudo apt-get install nginx
ç§ã®å Žåãnginxã®ã«ãŒããã£ã¬ã¯ããªã¯/ etc / nginxã§ããã ããã§ãæåã«/ sites_available / defaultãã¡ã€ã«ã次ã®ããã«å€æŽããå¿ èŠããããŸãã
server { listen 80; server_name youapp.com; location / { proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080/; } }
ãã ããããã ãã§ã¯ãããŸããã ãŸããç§ãã¡ãèšå®ãããããã·ããµããŒãããããã«ãããžã§ã¯ããä¿®æ£ããå¿ èŠããããŸãã 幞ãããããè¡ãã®ã¯é£ãããããŸãããapplication.propertiesã«è¡ãè¿œå ããã ãã§ãïŒå€æŽãæ°ããããŒãžã§ã³ã«å ¥åããããšãå¿ããªãã§ãã ããïŒã
server.tomcat.remote_ip_header=x-forwarded-for server.tomcat.protocol_header=x-forwarded-proto
ããã§ãservice nginx startã³ãã³ãã䜿çšããŠnginxãèµ·åãããããžã§ã¯ããéå§ã§ããŸãã ãµã€ããªã³ã¯ã§å©çšã§ããŸãããã¡ã€ã³ããŸã è³Œå ¥ããŠããªãå Žåã¯ãããŒããæå®ããã«ãã®IPã¢ãã¬ã¹ã§å©çšã§ããŸãã
ãã1ã€å°ããªã¿ããããããŸãã äžèšã®æ¹æ³ã§ãããžã§ã¯ããåžžã«éå§ããã®ã¯å°ãäžäŸ¿ã§ãã ãããžã§ã¯ãã®éå§æã«ããµãŒããŒã®å ¥åã³ã³ãœãŒã«ã解æŸãããsshã»ãã·ã§ã³ãçµäºããåŸã«ã¢ããªã±ãŒã·ã§ã³ãéãããã¢ããªã±ãŒã·ã§ã³ãã°ãã©ããã«ä¿åãããŠãããšäŸ¿å©ã§ãã ããã¯ãnohupã³ãã³ãã䜿çšããŠå®è¡ã§ããŸãã æåã«ãbashã¹ã¯ãªãããäœæããscript.shãåŒã³åºããŸãã
#!/bin/bash java -jar youapp.jar
ããã©ãŒãã³ã¹ã«å¯Ÿããæš©å©ãèŠå®ããŸãã
chmod +x ./script.sh
ãããŠãã³ãã³ããå®è¡ããŸãïŒ
nohup ./start.sh > log.txt 2>&1 &
ãã¹ãŠãã¢ããªã±ãŒã·ã§ã³ãå®è¡ãããŠããŸãã
ã¢ããªã±ãŒã·ã§ã³ãåæ¢ããã«ã¯ãpkill -9 javaã³ãã³ãïŒããããµãŒããŒäžã§å®è¡ãããŠããå¯äžã®Javaã¢ããªã±ãŒã·ã§ã³ã§ããå ŽåïŒã䜿çšããããhtopãŠãŒãã£ãªãã£ã䜿çšããŠãã®ããã»ã¹ã匷調衚瀺ããF9ãã¿ã³ãæŒããŠããªã¹ãã®å·ŠåŽã®SIGKILLãéžæããŠEnterããŒãæŒããŸãã 泚ïŒåããŠåäœããªãå Žåããããæé ãç¹°ãè¿ãå¿ èŠããããŸãã
ããŠããã¹ãŠãæ£ããè¡ãããŠããã°ããããžã§ã¯ãã®ãµã€ãããã©ãŠã¶ã§éããŠçµæã楜ããããšãã§ããŸãã
PSäœãèŠéããŠããªãããšãé¡ã£ãŠããŸãã ééããèŠã€ããå Žåã¯ãå人çãªã¡ãã»ãŒãžã§ããã«ã€ããŠæžããŠãã ããã ããã¹ãã¯ããã«ä¿®æ£ãããŸãã