點擊“藍字”關注我們更多!
作者 | 付高揚
本篇文章主要講述如何快速搭建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小助手
咱們一起做點有意思的事情~
掃碼進羣
您點的每個贊,我都認真當成了喜歡