点击“蓝字”关注我们更多!
作者 | 付高扬
本篇文章主要讲述如何快速搭建ServiceComb工程,并且结合MyBatis框架实现操作数据库。
完整Demo参考:https://github.com/servicestage-demo/cse-java-demo-list/tree/master/demo-cse-mybatis
创建工程
1
首先在本地搭建ServiceComb工程
为了能够使开发者可以快速构建ServiceComb应用程序,官方为我们提供了一套脚手架,这样能够方便学习者及应用开发者快速入门,同时极大的提高了效率。
快速开发引导页:http://start.servicecomb.io/
填写好工程group、artifact等信息(ServiceComb参数可以使用默认),就可以生成一个ServiceComb工程了,将工程下载到本地,导入到Eclipse或者IDEA等开发工具中。
2
配置microservice.yaml
servicecomb.service.registry.address:CSE(微服务引擎)服务注册发现地址
servicecomb.rest.address:本地应用访问地址
AK、SK获取请参考链接:https://support.huaweicloud.com/devg-apisign/api-sign-provide.html#p3
APPLICATION_ID: demo-face-recognition
service_description:
# name of the declaring microservice
name: demo-face-recognition
version: 0.0.1
environment: development
servicecomb:
service:
registry:
address: https://cse.xxx.myhuaweicloud.com:port
rest:
address: 0.0.0.0:8081
credentials:
accessKey: Your AK
secretKey: Your SK
project: cn-north-1
akskCustomerCipher: default
handler:
chain:
Provider:
default: tracing-provider
也可以使用本地注册中心,到官网http://servicecomb.apache.org/cn/release/下载ServiceComb Service-Center(选最新版本)
本地microservice.yaml配置参考:
APPLICATION_ID: demo-face-recognition
service_description:
name: demo-face-recognition
version: 1.0.0
service_description:
name: demo-provider
version: 1.0.0
servicecomb:
rest:
address: 0.0.0.0:9000
service:
registry:
address: http://127.0.0.1:30100
3
配置jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root
4
配置mybatis
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 引用db.properties配置文件 -->
<properties resource="jdbc.properties" />
<typeAliases>
<typeAlias
type="org.apache.servicecomb.samples.mybatis.util.DruidDataSourceFactory"
alias="DRUID" />
</typeAliases>
<!-- development : 开发模式 work : 工作模式 -->
<environments default="work">
<environment id="work">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="DRUID">
<!-- value属性值引用db.properties配置文件中配置的值 -->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- mybatis的mapper文件,每个xml配置文件对应一个接口 -->
<mappers>
<mapper resource="mapper/userMapper.xml" />
</mappers>
</configuration>
5
配置mapper文件(接口可以自行定义)
userMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper
namespace="org.apache.servicecomb.samples.mybatis.dao.userDao">
<select id="findAllUsers"
resultType="org.apache.servicecomb.samples.mybatis.entity.User">
select * from user;
</select>
<insert id="addUser"
parameterType="org.apache.servicecomb.samples.mybatis.entity.User">
insert into user(sid, name, gender, department)
values(null, #{name}, #{gender}, #{department})
</insert>
</mapper>
6
目录结构说明
目录说明:
src 为示例代码目录
resources为配置文件目录
mapper为sql语句配置目录
+---lib
\---src
\---org
\---apache
\---servicecomb
\---samples
\---mybatis
\---resources
\---mapper
+ pom.xml
+ ReadMe.md
+ VERSION
7
调用示例
调用URL:http://127.0.0.1:8081/user
请求方式:GET
Response示例:
[{
"sid": 1,
"name": "小明",
"gender": "男",
"department": "华为云服务"
}]
8
pom.xml参考
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.servicecomb.samples</groupId>
<artifactId>demo-mybatis</artifactId>
<name>Java Chassis::Samples::Demo-Mybatis</name>
<version>1.2.0</version>
<packaging>jar</packaging>
<description>Quick Start Demo for Using ServiceComb Java Chassis</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<java-chassis.version>${project.version}</java-chassis.version>
<spring-boot-1.version>1.5.14.RELEASE</spring-boot-1.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-dependencies</artifactId>
<version>${java-chassis.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-1.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-boot-starter-provider</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-flowcontrol-qps</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-bizkeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-tracing-zipkin</artifactId>
</dependency>
<dependency>
<groupId>com.huawei.paas.cse</groupId>
<artifactId>cse-solution-service-engine</artifactId>
<version>2.3.56</version>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>tracing-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/dtm-lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>./dtm-lib/</classpathPrefix>
<mainClass>org.apache.servicecomb.samples.gc.GCApplication</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
如您对开源开发、微服务感兴趣
欢迎扫描下方二维码添加
ServiceComb小助手
咱们一起做点有意思的事情~
扫码进群
您点的每个赞,我都认真当成了喜欢