当前位置:网站首页>【云原生】什么是微服务?怎么搭建?手把手教你搭建第一个微服务(框架)
【云原生】什么是微服务?怎么搭建?手把手教你搭建第一个微服务(框架)
2022-06-11 02:50:00 【神州数码云基地】
目录
在学习搭建第一个微服务框架之前,我们先了解下什么是微服务
什么是微服务?
在学习了微服务架构之后,我们会知道,微服务架构就是把项目里的每一个功能元素独立出来,再对这些功能元素进行动态组合。这样的优点在于:节省调用资源,使之成为可独立升级的代码块。
微服务把服务拆分成一个个模块,使得其界限相对明显,从而实现项目的可理解性、可测试性以及可观测性,同时降低项目解耦(高内聚,低耦合)的复杂性。
现在,结合神州数码云基地的微服务相关项目,手把手教大家搭建自己第一个微服务。
如何搭建第一个微服务(框架)
一、环境准备
1.1 软件要求
这里主要讲的是构建微服务框架时的前期准备工作,主要是环境的准备。包括了 JDK 的版本、IEDA 的版本和Maven 的版本等。
- JDK 1.8
- IEDA 2019(推荐)
- Maven 3.5.x(推荐)
- nacos 1.1.3(推荐)
在这里要注意一点,IDEA 版本与 Maven 的版本需要进行适配。如 IDEA2019.1 与 Maven3.5.x 系列的适配度较高,Maven 的版本不宜太高,这样的话在 IDEA的ieda.log 里可能会报" Unable to create injector "的错。直接导致不能拉取框架或者 Maven 的 Plugins 下载报错等问题。
1.2 配置操作
这里主要讲的是一些配置的相关操作,包括了 Maven 的 settings.xml 配置、IDEA 的 settings 配置以及 Nacos的配置等。
- Maven 的 settings.xml 配置:
框架在云基地的 nexus3 私服上面,需要在这上面进行拉取
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>你的本地仓库路径</localRepository>
<pluginGroups></pluginGroups>
<proxies></proxies>
<servers>
<server>
<id>DCone</id>
<username>你的ITCODE</username>
<password>你的密码</password>
</server>
</servers>
<mirrors>
<mirror>
<id>DCone</id>
<mirrorOf>*</mirrorOf>
<url>https://nexus.dev.wh.digitalchina.com/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>DCone</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>DCone</id>
<url>https://nexus.dev.wh.digitalchina.com/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>- Nacos 的配置
1、安装 Nacos
基础框架使用 Nacos 为注册中心,所以第一步就是安装 Nacos 注册中心
下载对应的压缩包
推荐下载1.3.1版本的,1.3.2启动报错
然后解压
linux:
$ unzip nacos-server-$version.zip
#或者
$ tar -xvf nacos-server-$version.tar.gz
$ cd nacos/bin2、Linux/Unix/Mac 运行
启动/关闭命令(standalone 代表着单机模式运行,非集群模式):
$ sh startup.sh -m standalone
$ sh shutdown.sh
# 后台运行
$ nohup startup.sh -m standalone &
# 如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:
$ bash startup.sh -m standalone3、Windows 运行
启动/关闭命令:
$ cmd startup.cmd
$ cmd shutdown.cmd或者双击 startup.cmd/shutdown.cmd 运行文件。
Windows 后台运行的方法
在 nacos/bin 目录创建一个 nacosStartUp.vbs 文件,右键编辑:
CreateObject("WScript.Shell").Run "startup.cmd",0,FALSE关闭也是同理,在 nacos/bin 目录创建 nacosShutDowm.vbs:
CreateObject("WScript.Shell").Run "shutdowm.cmd",0,FALSE双击 nacosStartUp.vbs 文件启动 nacos 注册中心
双击 nacosShutDowm.vbs 关闭 nacos 注册中心
安装启动后,访问地址http://127.0.0.1:8848/nacos 账号密码均默认为nacos
启动成功之后,就说明这个注册中心已经搭建好了
4、1.3.2版本出错解决办法
问题一:db.num is null --数据库有问题
解决办法:
- mysql 新建库:nacos,字符集:utf8 ,排序规则:utf8_general_ci
- nacos/conf/nacos-mysql.sql 文件里的 sql 脚本执行到本机数据库的 nacos 库中
- nacos/conf/application.properties 里修改配置
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=123456问题二:unable to start web server --无法启动内置的 tomcat
解决办法:将 nacos/target/nacos-server.jar 拷贝到自己的 tomcat 目录下启动
- IDEA 的 settings 配置:

二、拉取框架
框架的拉取主要是通过引入在云基地的 Nacos 注册中心。其地址在 Maven 文件夹下的 conf 文件夹中,文件名为 settings.xml,推荐使用 IED 打开(如 VsCode )。具体的本地文件路径和镜像地址、仓库地址如下图2-1、2-2和2-3所示:

图2-1 settings.xml本地文件路径

图2-2 镜像地址

图2-3 仓库地址
三、模块搭建
3.1、sdk模块
sdk 模块的内容不需要继承父模块的内容,所以不需要暴露框架的 parent 标签。但是每个模块的 pom.xml 都必须要有自己的 groupId 坐标,因为删除了 parent 标签,所以需要自己手动加入 groupId 和 artifactId。同时在sdk 模块中引入 lombok 依赖和 swagger 注解,方便写请求体和返回体的类。具体可参考图3-1与3-2所示:

图3-1 手动加入坐标

图3-2 引入注解
3.2、sdk-cloud 模块
sdk-cloud 模块是内部模块,所以保留 parent 标签,它的作用主要是暴露给内部的其它服务去调用。且由于模块的引用方式是层层引用的,所以 sdk-cloud 模块需要引用 sdk 模块。同时,无论子模块中是否继承了父模块的依赖,都需要在父模块的 dependencies 中 对子模块的版本进行声明,表明子模块的版本跟随的是项目版本,使得两者版本保持一致,便于管理。具体可参考图3-3所示:

图3-3 保留 parent 标签
同时,在每个新建的子模块中都需要声明该模块引用上一层的模块。比如 sdk-cloud 模块引用的就是 sdk 模块,写在 dependencies 中即可。具体可参考图3-4所示:

图3-4 引用 sdk 模块
在 sdk-cloud 模块中,需要对 Feign 做一些配置,Feign 是微服务之间进行远程调用的重要组件。一般而言,哪个服务需要用到 Feign 哪个服务就会去写 Feign。具体操作是在 sdk-cloud 的 resources 下注入 spring factories 文件,来声明 Feign 的配置在其它模块也能够生效。具体可参考图3-5所示:

图3-5 Feign的配置
3.3、common模块
common 模块是整个服务的核心,主要对数据进行处理,与业务的逻辑无关。作为内部的核心模块,依然保留parent 标签,且需要引用上一层模块,在这里就不赘述了。
下面展示一下它的目录结构,具体如图3-6所示:

图3-6 common目录结构
其中 PersonMapper 是作为接口,直接继承封装好的框架内容(BaseMapper),泛型是 Person 类。将主要的作用是对数据库进行持久化操作,有直接针对数据库的一些代码。具体如图3-7所示:

图3-7 PersonMapper 接口
而 Person 作为数据库表的实体类,主要的作用是定义字段,自动生成 getter 和 setter,再加入 swagger 注解进行声明以及生成 setter 时返回类。具体如图3-8所示:

图3-8 实体类
3.4、api 模块
程序的入口(启动类)写在 api 模块中,java 程序的入口是 main 函数,spring boot 的配置文件也写在该模块中。由于项目采用的是多模块的方式,在启动类中声明 Service 注解或者 Component 注解,表明该类为 spring 容器中的一个组件。注意在多模块的项目中,包扫描( basePackages)的作用是在指定的路径下,扫描到 Service 注解(或 Component 注解)所修饰的类,注入到 spring 容器中。具体如图3-9所示:

图 3-9 人员接口
在 api 模块中还需要声明与 mybatis 有关的注解,在包扫描中指定 mapper 的路径(在 common 模块中)。具体如图3-10所示:

图 3-10 相关注解
项目直接使用 main 函数作为程序的入口,但是由于框架对启动类做了一定程度上的封装,所以选择 extends 直接继承框架。具体如图3-11所示:

图 3-11 程序入口
由于框架中使用 discovery 模块后,在启动类中会存在一些问题,所以选择在 Nacos 配置中心使用 spring cloud的一个组件。该组件的配置文件名为 bootstrap.yml,其被执行的优先级稍高于启动类,该文件对 Nacos 的一些配置做了封装。
一般的 springboot 项目启动后只需要通过端口去访问,在本项目中还需加入变量前缀,该 yml 文件中还包含了mysq l数据库一些性能调优的信息。

图3-12 相关端口信息
3.5、facade模块
暂未总结。
至此,服务的框架基本搭建完毕。
边栏推荐
- CocosCreator原生二次开发的正确姿势
- 文件合成器
- TCP/IP
- 反射三种方式
- Harris corner detection opencv
- Prophet
- B_ QuRT_ User_ Guide(20)
- Looking at the ups and downs of the mobile phone accessories market from the green Union's sprint for IPO
- Cmake common commands
- Detailed explanation of unity project optimization (continuous supplement)
猜你喜欢

The new colleague asked me what "where 1=1" means???

HQChart实战教程55-欧易网K线面积图

postgresql源码学习(22)—— 故障恢复③-事务日志的注册

【大咖秀】博睿数据眼中的AIOps,选择正确的赛道正确的人

B_QuRT_User_Guide(18)

AOSP ~ modify WebView default implementation

msg=SolrCore ‘collection1‘ is not available due to init failure: Could not l

Hough transform of image

Question bank and answers for 2022 melting welding and thermal cutting operation certificate examination

新来的同事问我 where 1=1 是什么意思???
随机推荐
com. mchange. v2.c3p0. Combopooleddatasource red
出栈序列是否是入栈序列
AOSP - Developer mode is enabled by default
How can Delma's own brand "take off" when Philips is listed on the market?
TimeHelper
Error excluded identifier before '(' token, grpc enumeration class compilation error
Pyqt5:slider slider control
Niuke: two numbers that only appear once in the array
第七章 常用的协议简介(1)
Whether the outbound sequence is the inbound sequence
Android P SoftAP start process
MySQL学习笔记:JSON嵌套数组查询
求MySQL先按大于等于当前时间升序排序,再按小于当前时间降序排序
Shell reads files by line
OpenJudge NOI 1.13 17:文字排版
Why did those who left Beijing, Shanghai and Guangzhou with a smile cry in the end?
Go quick start of go language (I): the first go program
近期学习和更新计划
Application of the remote acquisition IOT gateway of the Bashir trough flowmeter in open channel flow monitoring
CocosCreator原生二次开发的正确姿势