SpringCloud微服务实战——搭建企业级开发框架(九)(使用Nacos发现、配置和管理微服务)
????Nacos是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台,Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
1、跟之前新建SpringBoot自定义扩展一样,我们在GitEgg_Platform中新建gitegg-platform-cloud子工程,此工程主要用于Spring Cloud相关功能的自定义及扩展。
2、在GitEgg_Platform中的gitegg-platform-bom子工程添加SpringCloud Alibaba的依赖
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
4.0.0 com.gitegg.platform
gitegg-platform-bom
${project.artifactId}
${gitegg.project.version} pom1.8 3.8.1 UTF-8 UTF-8UTF-81.0-SNAPSHOT 8.0.17 9.1-901.jdbc41.1.23 3.4.0 3.0.1 2.2.3.RELEASE
com.gitegg.platform
gitegg-platform-db
${gitegg.project.version}
com.gitegg.platform
gitegg-platform-mybatis
${gitegg.project.version}
com.gitegg.platform
gitegg-platform-swagger
${gitegg.project.version}
com.gitegg.platform
gitegg-platform-boot
${gitegg.project.version}
com.gitegg.platform
gitegg-platform-cloud
${gitegg.project.version}
mysql
mysql-connector-java
${mysql.connector.version}
postgresql
postgresql
${postgresql.connector.version}
com.alibaba
druid-spring-boot-starter
${druid.version}
com.baomidou
mybatis-plus-boot-starter
${mybatis.plus.version}
com.github.xiaoymin
knife4j-dependencies
${knife4j.version}
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
${spring.cloud.alibaba}
pom
import
3、在gitegg-platform-cloud工程中引入spring-cloud-starter-alibaba-nacos-discovery
GitEgg-Platform
com.gitegg.platform
1.0-SNAPSHOT
4.0.0 gitegg-platform-cloud
${project.artifactId}
${project.parent.version} jar
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
【SpringCloud微服务实战——搭建企业级开发框架(九)(使用Nacos发现、配置和管理微服务)】4、GitEgg_Platform工程重新执行install,在GitEgg_Cloud的子工程gitegg-service中引入gitegg-platform-cloud
GitEgg-Cloud
com.gitegg.cloud
1.0-SNAPSHOT
4.0.0 gitegg-servicepom
gitegg-service-base
gitegg-service-bigdata
gitegg-service-system
com.gitegg.platform
gitegg-platform-boot
com.gitegg.platform
gitegg-platform-cloud
com.gitegg.platform
gitegg-platform-db
com.gitegg.platform
gitegg-platform-mybatis
com.gitegg.platform
gitegg-platform-swagger
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
5、修改application.yml文件,添加nacos配置:
server:
port: 8001
spring:
application:
name: gitegg-service-system
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1/gitegg_cloud?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
username: root
password: root
initialSize: 1
minIdle: 3
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 30000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: config,stat,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;
druid.stat.slowSqlMillis=5000;
# 合并多个DruidDataSource的监控数据
useGlobalDataSourceStat: true
mybatis-plus:
mapper-locations: classpath*:/com/gitegg/*/*/mapper/*Mapper.xml
typeAliasesPackage: com.gitegg.*.*.entity
global-config:
#主键类型0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
id-type: 2
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: 2
#驼峰下划线转换
db-column-underline: true
#刷新mapper 调试神器
refresh-mapper: true
#数据库大写下划线转换
#capital-mode: true
#逻辑删除配置
logic-delete-value: 1
logic-not-delete-value: 0
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
6、修改GitEggSystemApplication.java添加注解@EnableDiscoveryClient,然后运行GitEggSystemApplication:
package com.gitegg.service.system;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
/**
* gitegg-system 启动类
*/
@EnableDiscoveryClient
@ComponentScan(basePackages = "com.gitegg")
@MapperScan("com.gitegg.*.*.mapper")
@SpringBootApplication
public class GitEggSystemApplication {public static void main(String[] args) {
SpringApplication.run(GitEggSystemApplication.class,args);
}}
7、在浏览器中打开nacos的地址,点击左侧菜单的服务列表,可以查看到服务已经注册到nacos
文章图片
本文源码在https://gitee.com/wmz1930/GitEgg 的chapter-09分支。
推荐阅读
- 基于微信小程序带后端ssm接口小区物业管理平台设计
- CET4听力微技能一
- 微习惯复盘
- 社保代缴公司服务费包含哪些
- 员工的微信朋友圈是公司的宣传阵地吗()
- 私有化轻量级持续集成部署方案--03-部署web服务(下)
- FBI怎么和恐怖分子谈判
- 探索免费开源服务器tomcat的魅力
- 微雨中的东湖
- 小醉微记|小醉微记|心不安处皆动荡