微服务项目的整合与测试
實驗目的
掌握微服務項目的整合使用
掌握Swagger-UI的簡單使用
練習內容
1、微服務項目整合
1.1、項目預覽
1.1.1、在?https://github.com/shi469391tou/microservice-mallmanagement.git?地址下載,并導入Myeclipse中;
1.1.2、查看項目的結構
1.2、微服務項目的功能介紹
1.2.1、microservice-eureka-server(Eureka注冊中心),搭建服務注冊中心,子項目將通過配置注冊到注冊中心。修改配置application文件如下所示:
spring:application:name: eureka-server # 指定應用名稱
server:port: 8761
eureka:client:register-with-eureka: falsefetch-registry: falseservice-url:defaultZone: http://localhost:${server.port}/eureka/
# 上線測試需要使用以下配置
# defaultZone: http://eureka-server:${server.port}/eureka/
1.2.2、microservice-gateway-zuul,作為其他微服務項目的API網關,實現其他微服務接口的動態代理。配置application文件如下所示:
spring:application:name: gateway-zuul # 指定應用名稱cloud:inetutils:preferred-networks:- 10.0 # 設置注冊到Eureka中心的優選服務地址server:port: 8050eureka:instance:prefer-ip-address: true #優選通過IP地址找到對應的服務名稱client:#配置eureka注冊中心地址serviceUrl:defaultZone: http://localhost:8761/eureka/
# 上線測試需要使用以下配置
# defaultZone: http://eureka-server:8761/eureka/#設置Hystrix熔斷器判定超時時間
#hystrix:
# command:
# default:
# execution:
# isolation:
# thread:
# timeoutInMilliseconds: 60000
zuul:ignoredServices: '*'routes:user-service:path: /user-service/**serviceId: user-serviceorder-service:path: /order-service/**serviceId: order-service
1.2.3、microservice-orderservice,主要用于商品訂單管理,并提供有關訂單管理的RESTFUL風格和API接口,配置application文件如下所示:
#DB Configuration
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.159.137:3306/microservice_mallmanagement
# 與Docker整合時可使用以下配置(也可以使用具體的ip+端口)
# url: jdbc:mysql://mysql:3306/microservice_mallmanagementusername: rootpassword: a1s2d3f!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/
1.2.4、在microservice-orderservice中寫對應的控制器類:
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 {@Autowiredprivate 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.5、 microservice-userservice,主要用于商品用戶管理,并提供有關用戶管理的RESTFUL風格和API接口,配置application文件如下所示:
#DB Configuration
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.159.137:3306/microservice_mallmanagement
# 與Docker整合時可使用以下配置(也可以使用具體的ip+端口)
# url: jdbc:mysql://mysql:3306/microservice_mallmanagementusername: rootpassword: a1s2d3f!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/
1.2.6、在microservice-userservice中寫對應的控制器類:
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 {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate 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 微服務項目啟動與測試
1.3.1 在MySQL中創建數據庫,并插入數據
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');
1.3.2、查看表中的訂單信息
1.3.3、查看用戶表中信息
1.3.4、啟動項目并運行成功后,通過地址http://localhost:8761,訪問注冊中心
1.3.5、測試接口方法??http://localhost:7900/order/findOrders/1
1.3.6、測試API網關服務
http://localhost:8050/order-service/order/findOrders/1
2、接口可視化工具(Swagger-UI)的使用
2.1、Swagger-UI使用方法
2.1.1、下載Swagger-UI項目?https://github.com/swagger-api/swagger-ui.git
2.1.2、引入Swagger-UI
a)用戶管理類
b)訂單管理類
2.1.3、加入Swagger依賴
<!-- Use Swagger UI for REST API test --><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>
a)用戶管理類
b)訂單管理類
2.1.4、編寫配置類
a)訂單管理類
b)用戶管理類
?
2.2、Swagger-UI使用測試
2.2.1、整合測試
重新啟動項目,通過對應服務地址IP+端口+swagger-ui.html,列出接口控制類:
?
2.2.2、接口測試
單擊user-controller面板,展示接口所有方法,單擊某個方法,列出詳細信息
在參數信息欄,輸入username的參數值shitou,單擊Try it out按鈕進行測試
從圖中可以看出,該方法查詢出了username為shitou的用戶訂單信息,同時還提供了curl和URL兩種請求方式,如果接口方法出現變更,只需要將對應的服務重啟,并刷新文檔頁面就會自動更新對應的方法.
3、總結
學習掌握了Swagger-UI測試工具,該工具能在項目開發中高開發的效率以及簡化操作
總結
以上是生活随笔為你收集整理的微服务项目的整合与测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spark集群启动时worker节点启不
- 下一篇: 微服务项目的部署