Spring Boot配置

多环境配置

在resources目录下,新建不同环境的配置文件

  • application-dev.properties
  • application-prod.properties
  • application-test.properties

在application.properties里面设置当前的配置文件

1
spring.profiles.active=dev

Database

MySQL

数据库名称为spring

1
2
3
4
spring.datasource.url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Druid

H2

1
2
3
4
5
6
7
8
9
10
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=root
spring.datasource.password=!Tester2468
spring.datasource.driver-class-name=org.h2.Driver

# visit db url: http://localhost:8080/h2

Servlet

Tomcat

修改服务器端口

1
server.port=8011

Jetty

替换Tomcat

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Build

War

找到节点

1
<packaging>jar</packaging>

  • 修改jar为war
  • 创建初始化类
    1
    2
    3
    4
    5
    class ServletInitializer : SpringBootServletInitializer() {
    override fun configure(builder: SpringApplicationBuilder?): SpringApplicationBuilder {
    return builder!!.sources(FiatgwApplication::class.java)
    }
    }

RPC

Thrift

pom.xml

1
2
3
4
5
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>

定义Thrift接口

1
2
3
4
5
namespace java com.fintend.fiatgw

service FiatGatewaySystemService{
bool exists(1:bool checked)
}

生成Java代码

1
thrift --gen java FiatGatewaySystemService.thrift

### gRPC

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
</dependency>

<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>

<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>

<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
<version>${grpc.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>com.google.protobuf.nano</groupId>
<artifactId>protobuf-javanano</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>io.grpc</groupId>
<artifactId>protoc-gen-grpc-java</artifactId>
<version>1.9.0</version>
<type>pom</type>
</dependency>