最近一直在研究公司架构的精髓,公司项目一般分为三个模块:****-api、****-data-access、****-cms。
****-api采用spring boot微服务理念,为其他项目提供接口服务。
api服务所有接口的返回值都以如下结构返回:
{
status: 200,
message: "ok",
entity: {}
}
status为http状态码,message为状态说明,entity为具体返回数据,可以是对象也可能是数组。
****-data-access为数据访问层,通过spirng data封装的方法操作es和mysql。
****-cms为本项目业务逻辑的封装。
在****-api为其他项目写完接口后,本地运行main函数,便可把接口在本地成功发布。
package com.letv.autostore;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication(exclude={ElasticsearchAutoConfiguration.class})
@ImportResource("${spring.config.path}")
public class AutoApiApplication {
public static void main(String[] args) {
SpringApplication.run(AutoApiApplication.class, args);
}
}
本地测试通过后,通过maven的package命令,把****-api项目打包成jar。
下面我们将讲解如何在linux服务器上部署spring boot微服务:
1.需要我们再/usr/local目录下创建****-api文件夹;
2.文件夹文件如下:application.yml ****-api-0.0.1.jar ehcache.xml start.sh;
3.application.yml中为tomcat、spring、log、es的基本配置:
server:
port: 8080
tomcat:
max-threads: 500
accesslog:
enabled: true
directory: ./logs
pattern: '%h %t %m "%U" %s %b %D'
prefix: localhost_access
suffix: .log
spring:
config:
path: classpath:applicationContext.xml
datasource:
username: ****
password: ****
url: jdbc:mysql://**.**.**.**:3306/****?useUnicode=true&characterEncoding=UTF-8
initialSize: 3
maxActive: 50
minEvictableIdleTimeMillis: 1800000
timeBetweenEvictionRunsMillis: 60000
maxWait: 10000
ehcache:
name: ehcache
config:
path: /usr/local/****-api/ehcache.xml
logging:
path: ./logs
level:
org.springframework: info
es:
nodes: **.**.**.**:9300
cluster: ****-****-****
4.ehcache.xml为ehcache缓存的相关配置:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
properties="connect=TCP(bind_addr=**.**.**.**;bind_port=7800):
TCPPING(initial_hosts=**.**.**.**.**[7801];port_range=10):
MERGE2(min_interval=3000;max_interval=5000):
FD_ALL(interval=5000;timeout=20000):FD(timeout=5000;max_tries=48;):
VERIFY_SUSPECT(timeout=1500):
pbcast.NAKACK(use_mcast_xmit=false;retransmit_timeout=100,200,300,600,1200,2400,4800;discard_delivered_msgs=true):
pbcast.STABLE(stability_delay=1000;desired_avg_gossip=20000;max_bytes=0):
pbcast.GMS(join_timeout=5000;print_local_addr=false;)"
propertySeparator="::" />
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="1000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<cache
name="ehcache"
maxElementsInMemory="100000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
overflowToDisk="false"
maxElementsOnDisk="1000000"
diskPersistent="false"
copyOnRead="true"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true"/>
</cache>
</ehcache>
5.start.sh为运行spring boot写的shell脚本:
java -server -Xms2g -Xmx2g -jar auto-api-0.0.1.jar &
我们进入/usr/local/****-api文件夹后,运行./start.sh便可在linux服务器启动spring boot应用。
接口部署成功后,我们可以通过google的Postman插件,测试接口是否可以成功被调用。