由于原有SpringCloud体系版本比较老,最初的注册中心使用的Eureka后期官方无升级方案,配置中心无法在线管理配置,还有实时上下线的问题,因此需要将原有系统的Eureka服务升级Nacos注册心服务。
原有版本SpringBoot1.5.15、SpringCloud E、注册中心Eureka
升级后版本SpringBoot2.1.6、SpringCloud G、注册中心Nacos1.4.2
注释掉根目录下的parent,和starter依赖。

注释掉dependencyManagement下的platform-bom

添加dependencyManagement的springboot依赖

添加打包资源


并添加alibaba.cloud版本

使用Undertow 替换内置 Tomcat;
添加Nacos配置:
添加监控相关引用
升级Feign引用
SpringbootApplication启动项中更新为
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
publicclass JoinDerepeatApplication {
}
根据自身服务更改下面内容,${nacos-namespace:}这种写法是为了运行时可以由外部环境变量传入,而不用更改配置重新打包发布。
server:
port: 8123
undertow:
worker-threads: 1000
io-threads: 32
accesslog:
enabled: true
dir: /logs/${spring.application.name}
pattern: '%t,${spring.application.name},%a,%A:%p,"%r",%s,%T,%b,%{i,Referer},"%{i,User-Agent}"'
spring:
application:
name: join-derepeat
profiles:
active: ${active:dev}
cloud:
#手动配置Bus id,
bus:
id: ${spring.application.name}:${server.port}
main:
allow-bean-definition-overriding: true
#解决restful 404错误 spring.mvc.throw-exception-if-no-handler-found=true spring.resources.add-mappings=false
mvc:
throw-exception-if-no-handler-found: true
resources:
add-mappings: false
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
#解决读取配置文件中文乱码
file:
encoding: utf-8
---
spring:
profiles: dev
cloud:
inetutils:
preferred-networks: 10.2
nacos:
config:
namespace:
server-addr: 10.3.87.30:3105
shared-configs[0]:
data-id: common.properties
## refresh: true
shared-configs[1]:
data-id: rabbitmq.properties
discovery:
namespace:
server-addr: 10.3.87.30:3105
metadata:
version: ${project.version}
description: ${project.description}
---
spring:
profiles: prd
cloud:
nacos:
config:
namespace: ${nacos-namespace:}
server-addr: ${config-server-addr}
shared-configs[0]:
data-id: common.properties
## refresh: true
shared-configs[1]:
data-id: rabbitmq.properties
discovery:
namespace: ${nacos-namespace:}
server-addr: ${discovery-server-addr}
metadata:
version: ${project.version}
description: ${project.description}
端口自定义,测试与正式环境配置shared-configs[0] common.properties 本项目需要加载的配置文件内容保存在Nacos中心。
refresh: true 设置为可以实时动态更新的配置文件

配置内容示例

可以添加自已的配置文件 如添加join-derepeat.yml。
只需要继续添加自己的配置文件shared-configs[2]配置即可。
最后启动本服务
访问接口页面http://localhost:8123/doc.html 调试即可。
在config文件夹内添加SwaggerConfig文件
将如下高亮更新为自己的程序内容
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
publicclass SwaggerConfig {
//swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等 @Bean
public Docket createRestApi() {
returnnew Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("数据去重复")
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.join.derepeat.controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个private ApiInfo apiInfo() {
returnnew ApiInfoBuilder()
//页面标题
.title("数据去重复 API")
//创建人
.contact(new Contact("james", "", ""))
//版本号
.version("1.0")
//描述
.description("重复数据拦截")
.build();
}
}
由于是采用Docker部署,因此需要打成docker镜像,使用的是docker-maven-plugin插件。
添加容器仓库地址
添加
小技巧:这里将jar运行时设置的参数变量化,方便在不重新编译的情况下快速通过设置环境变量的方式添加一些想要的值。
以上就是在实战中总结出来的配置经验。