微服务改造过程记录

jfinal项目改造为微服务过程记录,主要包括:

  • Eclipse SpringBoot项目创建流程

  • SpringBoot

    • 热部署配置方法
    • 如何使用ActiveRecordPlugin连接数据库(Db.find…)
    • 请求参数获取与Json数据返回
    • 正式/测试环境区分
    • maven导入本地包
    • 拦截器
    • 接口中文到前端乱码的问题
    • 事件监听器
    • 如何格式化输出,包括统一的异常处理
    • 安全处理
  • 微服务:

    • 服务注册与发现:Eureka Server/Eureka Discovery
    • 自动化部署:maven/jekins //TODO
    • 文档生成:Swagger //TODO
    • 日志与链路追踪 sleuth + zipkin

Eclipse SpringBoot项目创建流程

  • 安装SpringBoot插件:菜单栏->Help ->Eclipse Market 搜索 “Spring”,选择Spring Tools 4(截止2019年2月21日09:56:34,最新),Install,安装完毕重启Eclipse
  • 创建SpringBoot项目:File -> New -> Other -> SpringBoot -> Spring Starter Project, Next
  • 基本配置:根据需要修改Name(项目名称),选择项目存储位置,Packing选项(jar/war)待定, 先选择war ,Next
  • 选择依赖包:根据需要选择依赖包,例如注册中心选择Eureka Server等等,Finish
  • 项目配置:根据需要编辑src/main/resources/application.properties
  • 业务逻辑:根据需要,编写接口
  • 启动:以java application 方式运行主包下的 XXXApplication.java文件,完成

Springboot推荐项目目录

SpringBoot 学习

热部署配置方法

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

...

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

...

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

...

如何使用ActiveRecordPlugin连接数据库(Db.find…)

使用ActiveRecordPlugin原因,减少代码改动量

module生成
1
2
_Generator.java
_Mapkit.java
springboot中的配置启动
1
ActiveRecordPluginConfig.java

请求参数获取与Json数据返回

  • 请求参数以参数名写在Controller方法的形参中,不需要带
  • Controller方法返回参数统一写为Object(响应数据主要分为Record/List/Map/String)
1
//一个典型的接口

###接口请求参数返回到前端为乱码??

  • 定位为拦截器配置类的问题
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
44
//配置类需增加以下三个方法:responseBodyConverter,configureMessageConverters,configureContentNegotiation
package yourpackage;

import java.nio.charset.Charset;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport{

@Override
protected void addInterceptors(InterceptorRegistry registry) {
//registry.addInterceptor(new TestInterceptor());
super.addInterceptors(registry);
}

@Bean
public HttpMessageConverter<String> responseBodyConverter() {
StringHttpMessageConverter converter = new StringHttpMessageConverter(
Charset.forName("UTF-8"));
return converter;
}

@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(responseBodyConverter());
}

@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}

格式化输出,包括统一的异常处理

安全问题

SpringCloud的服务注册与发现——Eureka

Eureka Server 与 Eureka Client(Eureka Discovery)

Eureka Server

  • 创建项目过程中,选择依赖包 步骤勾选 Eureka Server
  • 配置文件编写:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    # server name
    spring.application.name=eureka-server
    # server port
    server.port=1001
    eureka.instance.hostname=localhost
    # not register self
    eureka.client.register-with-eureka=false
    # not fetch from eureka-server
    eureka.client.fetch-registry=false
  • 运行
  • 浏览器输入网址:例如上述配置文件,可打开地址 http://localhost:1001/ 查看效果,如图,注意红框中,目前没有任何已注册的服务实例。
    一个未注册任何服务的Eureka展示页面

Eureka Client(Eureka Discovery)

  • 创建项目过程中,选择依赖包 步骤勾选 Eureka Discovery

  • 配置文件编写:

    1
    2
    3
    4
    # producer name
    spring.application.name=eureka-client
    server.port=2001
    eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
  • 编写一个简单的接口

  • 运行

  • 刷新查看Eureka页面是否发生变化,结果展示:
    Eureka注册结果展示

  • 注册中心相关教程链接
    [1] SpringBoot搭建的注册中心 https://blog.csdn.net/HBL6016/article/details/80638106

SpringBoot 连接数据库

  • jfinal 所使用的ActiveRecordPlugin
  • spring Data JPA
  • JDBCTemplate

jfinal 所使用的ActiveRecordPlugin

使用spring Data JPA连接数据库

//TODO

JDBCTemplate

//TODO

[1] spring boot 访问mysql数据库的几种方式 https://www.jianshu.com/p/7fa6dc5c35fe

springboot 接受参数并返回json数据

return 数据

优势:原生,默认

HttpServletResponse 返回数据

优势:减少代码改动量,不需修改renderAppJson

springboot 如何省略 requestmapping 注解

springboot AOP 拦截器

正式环境与测试环境区分开,

  • 最好分为 开发环境,测试环境,正式环境