微服务项目的整合与测试
一、微服务项目整合
1.1 微服务项目结构预览
本项目通过一个名为microservice_mallmanagement的maven父项目构建了四个子项目,分别如下:
microservice-eureka-server:用于服务注册发现
microservice-gateway-zuul:用于API网关
microservice-orderservice:用于订单管理服务
microservice-userservice:用于用户管理服务
1.2 微服务项目功能介绍
1.2.1 microservice-eureka-server
该子项目使用了spring boot的组件Eureka,主要用于搭建一个服务注册中心,配置文件application.yml内容:
spring:
application:
name: eureka-server # 指定应用名称
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:${server.port}/eureka/
# 上线测试需要使用以下配置
# defaultZone: http://eureka-server:${server.port}/eureka/
1.2.2 microservice-gateway-zuul(网关服务)
使用zuul组件,作为其他服务项目的API网关,来实现其他微服务接口的动态代理。
配置文件application.yml内容如下:
spring:
application:
name: gateway-zuul # 指定应用名称
cloud:
inetutils:
preferred-networks:
- 10.0 # 设置注册到Eureka中心的优选服务地址
server:
port: 8050
eureka:
instance:
prefer-ip-address: true #优选通过IP地址找到对应的服务名称
client:
#配置eureka注册中心地址
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# 上线测试需要使用以下配置
# defaultZone: http://eureka-server:8761/eureka/
zuul:
ignoredServices: '*'
routes:
user-service:
path: /user-service/**
serviceId: user-service
order-service:
path: /order-service/**
serviceId: order-service
1.2.3 microservice-orderservice(订单管理服务)
该项目主要提供商城订单管理,并提供有关订单的RESTFUL风格的API接口方法
配置文件application.yml内容如下:
除配置了服务的应用名称、端口以及Eureka注册中心外,还额外增加了MySQL数据库的配置。
#DB Configuration
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/microservice_mallmanagement
# 与Docker整合时可使用以下配置(也可以使用具体的ip+端口)
# url: jdbc:mysql://mysql:3306/microservice_mallmanagement
username: root
password: 123456
application:
name: order-service # 指定应用名称
cloud:
inetutils:
preferred-networks:
- 10.0 # 设置注册到Eureka中心的优选服务地址
server:
port: 7900 # 指定该Eureka实例的端口号
eureka:
instance:
prefer-ip-address: true #优选通过IP地址找到对应的服务名称
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #配置eureka注册中心地址
# 上线测试需要使用以下配置
# defaultZone: http://eureka-server:8761/eureka/
该项目对应的控制器类OrderController具体内容如下:
package com.itheima.controller;
import com.itheima.mapper.OrderMapper;
import com.itheima.po.Order;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderMapper orderMapper;
@GetMapping(path="/findOrders/{userid}")
@HystrixCommand(fallbackMethod = "findOrderfallback") //断路器
public List<Order> findOrder(@PathVariable("userid") Integer userid) {
List<Order> orders= this.orderMapper.selectOrder(userid);
return orders;
}
//针对上面断路器发现的问题编写回调方法(参数和返回值要一样)
public List<Order> findOrderfallback(Integer userid) {
List<Order> orders =new ArrayList<>();
return orders;
}
}
1.2.4 microservice-userservice(用户管理服务)
该服务主要用于进行商城用户管理,并提供有关用户管理的微服务项目,主要用于商城用户管理,并提供有关订单的RESTFUL风格的API接口方法
配置文件application.yml内容如下:
#DB Configuration
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://mysql:3306/microservice_mallmanagement
# 与Docker整合时可使用以下配置(也可以使用具体的ip+端口)
url: jdbc:mysql://localhost:3306/microservice_mallmanagement
username: root
password: 123456
application:
name: user-service # 指定应用名称
cloud:
inetutils:
preferred-networks:
- 10.0 # 设置注册到Eureka中心的优选服务地址
server:
port: 8030 # 指定该Eureka实例的端口号
eureka:
instance:
prefer-ip-address: true #优选通过IP地址找到对应的服务名称
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #配置eureka注册中心地址
# 上线测试需要使用以下配置
# defaultZone: http://eureka-server:8761/eureka/
#客户端动态访问常量配置
ORDERSERVICEURL: http://order-service/
该项目对应的控制器类UserController具体内容如下:
package com.itheima.controller;
import com.itheima.mapper.UserMapper;
import com.itheima.po.Order;
import com.itheima.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private UserMapper userMapper;
@Value("${ORDERSERVICEURL}")
private String ORDERSERVICEURL;
@GetMapping(path="/findOrders/{username}")
public List<Order> getOrderByUsername(@PathVariable("username")
String username) {
User user = this.userMapper.selectUser(username);
//使用Ribbon后,可以使用http://order-service/而不用使用ip+端口
ResponseEntity<List<Order>> rateResponse =
restTemplate.exchange(ORDERSERVICEURL
+"/order/findOrders/"+user.getId(),
HttpMethod.GET, null,
new ParameterizedTypeReference<List<Order>>(){});
List<Order> orders = rateResponse.getBody();
return orders;
}
}
1.3 微服务项目的启动和测试
在项目启动前,先在MySQL数据库中创建SQL脚本。创建有个名为microservice_mallmanagement的数据库,同时在该数据库中分别创建tb_user和tb_order表,具体命令如下:
microservice_mallmanagement.sql
CREATE DATABASE microservice_mallmanagement;
USE microservice_mallmanagement;
DROP TABLE IF EXISTS `tb_order`;
CREATE TABLE `tb_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`createtime` datetime DEFAULT NULL,
`number` varchar(255) DEFAULT NULL,
`userid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=UTF8;
INSERT INTO `tb_order` VALUES ('1', '2017-10-09 10:15:44', '201709181459001', '1');
INSERT INTO `tb_order` VALUES ('2', '2017-10-24 18:22:12', '201709181459008', '1');
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`address` varchar(255) DEFAULT NULL,
`username` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=UTF8;
INSERT INTO `tb_user` VALUES ('1', 'beijing', 'shitou');
启动顺序:microservice-eureka-server—microservice-gateway-zuul—microservice-orderservice——microservice-userservice
启动成功后,通过地址http://localhost:8761/,即可访问Eureka的注册中心,如图:
(1)测试接口方法。分别通过http://localhoat:7900/order/findOrders/1和http://localhoat:8030/user/findOrders/shitou来访问microservice-orderservice和microservice-userservice两个微服务项目各自暴露的API接口
如图:
(2)测试API网关服务
分别通过http://localhoat:8050/order-service/findOrders/1和http://localhoat:8050/user-service/findOrders/shitou来访问microservice-orderservice和microservice-userservice两个微服务项目
订单管理服务:
用户管理服务:
二、接口可视化工具–swagger-ui
2.1 Swagger-UI使用方法
1.下载swagger-ui项目
https://github.com/swagger-api/swagger-ui.git
2.引入swagger-ui。
将整个dist目录复制到需要使用Swagger-UI工具项目的resource目录下。如图:
3.加入swagger依赖
在项目的pom文件下加入如下依赖内容:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
4.编写配置类
在项目中创建一个Swagger-UI的配置类SwaggerConfiguration.java,代码如下:
package com.itheima.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StopWatch;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.Date;
import static springfox.documentation.builders.PathSelectors.regex;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
//定义API接口映射路径
public static final String DEFAULT_INCLUDE_PATTERN = "/user/.*";
private final Logger log =
LoggerFactory.getLogger(SwaggerConfiguration.class);
@Bean
public Docket swaggerSpringfoxDocket() {
log.debug("Starting Swagger");
StopWatch watch = new StopWatch();
watch.start();
//用于生成对应API接口文档的描述信息,可省略
ApiInfo apiInfo = new ApiInfo("用户管理API接口测试文档","description",
"termsOfServiceUrl","contact","version","","");
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.genericModelSubstitutes(ResponseEntity.class)
.forCodeGeneration(true)
.genericModelSubstitutes(ResponseEntity.class)
.directModelSubstitute(java.time.LocalDate.class, String.class)
.directModelSubstitute(java.time.ZonedDateTime.class, Date.class)
.directModelSubstitute(java.time.LocalDateTime.class, Date.class)
.select()
.paths(regex(DEFAULT_INCLUDE_PATTERN))//匹配路径生成对应接口文档
.build();
watch.stop();
log.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
return docket;
}
}
2.2 Swagger-UI使用测试
1.整合测试
重新启动所有整合了Swagger-UI的项目,通过“服务地址IP+端口+、swagger-ui.html”进入测试页面
用户管理服务:
订单管理服务:
2.接口测试
可通过某个方法的具体参数进行查询测试,如图:
用户管理服务:
订单管理服务:
三、总结
通过本章的学习,对位服务项目有了进一步的认识,熟悉spring boot和spring Cloud相关组件的整合开发,同时还可以掌握接口测试工具Swagger-UI。