[TOC]
## 在evm中新建一個微服務
如何在evm新建一個新的微服務?
### 1-新建一個module
右鍵父級module,新建一個maven module:

### 2-編輯pom.xml
```xml
<dependencies>
<!-- evm-common模塊 -->
<dependency>
<groupId>com.keyou.evm</groupId>
<artifactId>evm-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- 關系型數據庫配置 -->
<dependency>
<groupId>com.keyou.evm</groupId>
<artifactId>db-spring-boot-starter</artifactId>
</dependency>
<!-- 非關系型數據庫配置 -->
<dependency>
<groupId>com.keyou.evm</groupId>
<artifactId>redis-spring-boot-starter</artifactId>
</dependency>
<!-- 日志中心 -->
<dependency>
<groupId>com.keyou.evm</groupId>
<artifactId>log-spring-boot-starter</artifactId>
</dependency>
<!-- 公共實體配置 -->
<dependency>
<groupId>com.keyou.evm</groupId>
<artifactId>common-spring-boot-starter</artifactId>
</dependency>
<!-- 資源服務器配置 -->
<dependency>
<groupId>com.keyou.evm</groupId>
<artifactId>uaa-client-spring-boot-starter</artifactId>
</dependency>
<!-- API文檔配置 -->
<dependency>
<groupId>com.keyou.evm</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
</dependency>
<!-- 熔斷限流組件 -->
<dependency>
<groupId>com.keyou.evm</groupId>
<artifactId>sentinel-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 選用nacos時打開-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
</dependencies>
<!-- 形成帶第三方jar包的可執行jar包,jar包目錄結構如下 application.properties lib META-INF mybatis
org -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 首先加入pom ${docker.image.prefix} : 這個是你的dockerhub注冊上面的名字 gitgeek 這個是我注冊的
${project.artifactId} : 項目的名稱 dockerDirectory : dockerfile的文件路徑 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<dockerHost>${docker.host}</dockerHost>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
<resources>
<!-- 打包java代碼資源 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/profiles/${app.env}</directory>
<filtering>false</filtering>
</resource>
</resources>
<finalName>${project.artifactId}</finalName>
</build>
```
### 3-創建啟動類
>[info] 啟動類是Springboot項目啟動的入口。

```java
@Configuration
@EnableLogging
@EnableDiscoveryClient
@SpringBootApplication
@EnableApiIdempotent
@MapperScan("com.keyou.evm.*.mapper,com.keyou.evm.*.dao")
public class ApiFakerApp {
public static void main(String[] args) {
// 固定端口啟動
// SpringApplication.run(UserCenterApp.class, args);
//隨機端口啟動
SpringApplication app = new SpringApplication(ApiFakerApp.class);
app.addListeners(new PortApplicationEnvironmentPreparedEventListener());
app.run(args);
}
}
```
### 4-創建swagger配置類

添加配置信息:
```java
@Component
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("Authorization").description("令牌").
modelRef(new ModelRef("string")).
parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// .apis(RequestHandlerSelectors.basePackage("com.keyou.evm"))
.apis(RequestHandlerSelectors.any())
// .paths(input -> PathSelectors.regex("/user.*").apply(input) || PathSelectors.regex("/permissions.*").apply(input)
// || PathSelectors.regex("/roles.*").apply(input) || PathSelectors.regex("/menus.*").apply(input) || PathSelectors.regex("/test.*").apply(input)
// )
.paths(PathSelectors.any())
.build().globalOperationParameters(pars);
}
private ApiInfo apiInfo() {
===>修改正確的title和description
return new ApiInfoBuilder().title("商城訪客系統api").description("商城訪客系統api").version("1.0").build();
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/");
resolver.setSuffix(".html");
return resolver;
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// super.addResourceHandlers(registry);
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
```
### 5-添加配置信息
#### application.yml
```xml
#端口配置
server:
port: 6001 #固定端口
# port: ${randomServerPort.value[7000,7005]} #隨機端口
spring:
datasource:
# JDBC 配置(驅動類自動從url的mysql識別,數據源類型自動識別)
url: jdbc:mysql://192.168.30.22:3306/zbj?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
#連接池配置(通常來說,只需要修改initialSize、minIdle、maxActive
initial-size: 1
max-active: 20
min-idle: 1
# 配置獲取連接等待超時的時間
max-wait: 60000
#打開PSCache,并且指定每個連接上PSCache的大小
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
validation-query: SELECT 'x'
test-on-borrow: false
test-on-return: false
test-while-idle: true
#配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
time-between-eviction-runs-millis: 60000
#配置一個連接在池中最小生存的時間,單位是毫秒
min-evictable-idle-time-millis: 300000
filters: stat,wall
# WebStatFilter配置,說明請參考Druid Wiki,配置_配置WebStatFilter
#是否啟用StatFilter默認值true
web-stat-filter.enabled: true
web-stat-filter.url-pattern: /*
web-stat-filter.exclusions: "*.js , *.gif ,*.jpg ,*.png ,*.css ,*.ico , /druid/*"
web-stat-filter.session-stat-max-count: 1000
web-stat-filter.profile-enable: true
# StatViewServlet配置
#展示Druid的統計信息,StatViewServlet的用途包括:1.提供監控信息展示的html頁面2.提供監控信息的JSON API
#是否啟用StatViewServlet默認值true
stat-view-servlet.enabled: true
#根據配置中的url-pattern來訪問內置監控頁面,如果是上面的配置,內置監控頁面的首頁是/druid/index.html例如:
#http://110.76.43.235:9000/druid/index.html
#http://110.76.43.235:8080/mini-web/druid/index.html
stat-view-servlet.url-pattern: /druid/*
#允許清空統計數據
stat-view-servlet.reset-enable: true
stat-view-servlet.login-username: admin
stat-view-servlet.login-password: admin
#StatViewSerlvet展示出來的監控信息比較敏感,是系統運行的內部情況,如果你需要做訪問控制,可以配置allow和deny這兩個參數
#deny優先于allow,如果在deny列表中,就算在allow列表中,也會被拒絕。如果allow沒有配置或者為空,則允許所有訪問
#配置的格式
#<IP>
#或者<IP>/<SUB_NET_MASK_size>其中128.242.127.1/24
#24表示,前面24位是子網掩碼,比對的時候,前面24位相同就匹配,不支持IPV6。
#stat-view-servlet.allow=
#stat-view-servlet.deny=128.242.127.1/24,128.242.128.1
# Spring監控配置,說明請參考Druid Github Wiki,配置_Druid和Spring關聯監控配置
#aop-patterns= # Spring監控AOP切入點,如x.y.z.service.*,配置多個英文逗號分隔
################### mysq end ##########################
# zipkin:
# base-url: http://127.0.0.1:11008
redis:
################### redis 單機版 start ##########################
host: 192.168.30.22
port: 6379
timeout: 6000
database: 8
lettuce:
pool:
max-active: 10 # 連接池最大連接數(使用負值表示沒有限制),如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)
max-idle: 8 # 連接池中的最大空閑連接 ,默認值也是8
max-wait: 100 # # 等待可用連接的最大時間,單位毫秒,默認值為-1,表示永不超時。如果超過等待時間,則直接拋出JedisConnectionException
min-idle: 2 # 連接池中的最小空閑連接 ,默認值也是0
shutdown-timeout: 100ms
################### redis 單機版 end ##########################
# cluster:
# nodes: 130.75.131.237:7000,130.75.131.238:7000,130.75.131.239:7000,130.75.131.237:7001,130.75.131.238:7001,130.75.131.239:7001
# #130.75.131.237:7000,130.75.131.238:7000,130.75.131.239:7000,130.75.131.237:7001,130.75.131.238:7001,130.75.131.239:7001
# #192.168.3.157:7000,192.168.3.158:7000,192.168.3.159:7000,192.168.3.157:7001,192.168.3.158:7001,192.168.3.159:7001
# timeout: 1000 # 連接超時時間(毫秒)
# lettuce:
# pool:
# max-active: 10 # 連接池最大連接數(使用負值表示沒有限制),如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)
# max-idle: 8 # 連接池中的最大空閑連接 ,默認值也是8
# max-wait: 100 # # 等待可用連接的最大時間,單位毫秒,默認值為-1,表示永不超時。如果超過等待時間,則直接拋出JedisConnectionException
# min-idle: 2 # 連接池中的最小空閑連接 ,默認值也是0
# shutdown-timeout: 100ms
mybatis-plus:
global-config:
banner: false
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:com/keyou/*/*/mapper/*Mapper.xml
security:
oauth2:
ignored: /users-anon/** , /doc.html ,/document.html ,/users/save
token:
store:
type: redis
#設置最大超時時間
ribbon:
ServerListRefreshInterval: 10 #刷新服務列表源的間隔時間
OkToRetryOnAllOperations: true
MaxAutoRetries: 1
MaxAutoRetriesNextServer: 1
ReadTimeout: 16000
ConnectTimeout: 16000
#設置最大容錯超時時間
hystrix:
command:
default:
execution:
timeout:
enabled: true
isolation:
thread:
timeoutInMilliseconds: 16000
logging:
level:
com.keyou.evm: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
# com.neusoft: DEBUG
# com.netflix: DEBUG #用于心跳檢測輸出的日志
```
#### bootstrap.yml
`bootstrap.yml`的加載優先級是高于`application.yml`的,用于連接nacos獲取配置信息,以及完成服務的注冊。
```yaml
spring:
cloud:
nacos:
config:
# 共享配置的DataId,多個使用,分隔
# 越靠后,優先級越高;
# .yaml后綴不能少,只支持yaml/properties
shared-dataids: common.yaml ### 共享配置
refreshable-dataids: common.yaml ### 可刷新共享配置
server-addr: 192.168.30.22:8848 ### nacos server地址
file-extension: yaml ### dataId擴展名
namespace: f4db1f33-61e5-496e-a2bd-8b390918c14e #命名空間 代指某個環境
sentinel:
transport:
# 指定sentinel 控制臺的地址
dashboard: 127.0.0.1:8080
eager: true
application:
name: api-faker
#metrics
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
chaosmonkey:
enabled: true
health:
show-details: always
```
>[warning] 恭喜你,到此結束,一個新的應用就創建成功了。
- 簡介
- 更新說明
- 其他作品
- 第一部分 Java框架基礎
- 第一章 Java基礎
- 多線程實戰
- 嘗試一下Guava帶返回值的多線程處理類ListenableFuture
- LocalDate和Date有什么區別
- JAVA8接口增強實踐
- 第二章 Spring框架基礎
- MVC究竟是個啥?
- @ApiImplicitParam
- 七種方式,教你在SpringBoot初始化時搞點事情!
- Spring事務狀態
- maven
- Mybatis小總結
- mybatis-plus的使用
- 第三章 SpringSecurity實戰
- 基于SpringSecurity+jwt的用戶認證
- spring-security-oauth2
- 第四章 數據庫
- mysql
- mysql授權
- mysql數據庫三個關鍵性能指標--TPS\QPS\IOPS
- 梳理一下那些年Mysql的弱語法可能會踩的坑
- 關于Mysql的“字符串”數值的轉換和使用
- 憑這一文咱把事務講透
- Mysql性能優化
- 查詢性能優化
- 不常用的一些語法
- elasticsearch
- elasticsearch文檔操作
- 索引的基本操作
- java操作ElaticSearch
- elasticsearch中的各種查詢
- DB與ES混合應用可能存在的問題及解決方案探索
- 使用es必須要知道的一些知識點:索引篇
- Es中的日期操作
- MongoDB
- 入門篇(了解非關系型數據庫 NoSQL - MongoDB)
- 集群分片 (高級篇)
- 互聯網大廠的建表規范
- 第五章 中間件
- nginx
- nginx動靜分離配置,這個雷你踩過嗎?
- Canal
- Sharding-jdbc
- 水平分庫實踐
- kafka
- 第六章 版本管理
- git
- Not currently on any branch 情況提交版本
- 第七章 IO編程
- 第八章 JVM實戰調優
- jvisualvm
- jstat
- 第二部分 高級項目實戰篇
- 第一章 微信開發實戰
- 第二章 文件處理
- 使用EasyExcel處理導入導出
- 第三章 踩坑指南
- 郵件發送功能
- 第三部分 架構實戰篇
- 第一章 架構實戰原則
- 接口防止重復調用的一種方案
- 第二章 高并發緩存一致性管理辦法
- 第三章 異地多活場景下的數據同步之道
- 第四章 用戶體系
- 集成登錄
- auth-sso的管理
- 第五章 分庫分表場景
- 第六章 秒殺與高并發
- 秒殺場景
- 第七章 業務中臺
- 中臺的使用效果是怎樣的?
- 通用黑白名單方案
- 第八章 領域驅動設計
- 第十一章 微服務實戰
- Nacos多環境管理之道
- logback日志雙寫問題及Springboot項目正確的啟動方式
- 第四部分 優雅的代碼
- java中的鏈式編程
- 面向對象
- 開發原則
- Stream操作案例分享
- 注重性能的代碼
- 第五部分 談談成長
- 新手入門指北
- 不可不知的調試技巧
- 構建自己的知識體系
- 我是如何做筆記的
- 有效的提問
- 謹防思維定勢
- 學會與上級溝通
- 想清楚再去做
- 碎片化學習
- 第六部分 思維導圖(付費)
- 技術基礎篇
- 技術框架篇
- 數據存儲篇
- 項目實戰篇
- 第七部分 吾愛開源
- 7-1 麻雀聊天
- 項目啟動
- 前端登錄無請求問題解決
- websocket測試
- 7-2 ocp微服務框架
- evm框架集成
- 項目構建與集成
- zentao-center
- 二次開發:初始框架的搭建
- 二次開發:增加細分菜單、權限到應用
- 7-3 書棧網
- 項目啟動
- 源碼分析
- 我的書架
- 文章發布機制
- IM
- 第八章 團隊管理篇
- 大廠是怎么運作的
- 第九章 碼山有道
- 簡歷內推
- 聯系我內推
- 第十章 學點前端
- Vue