一、爱购网仓库搭建
1.创建Github仓库:https://github.com/zhengqingya/aigou_parent.git
2.克隆到本地,并完成初始化
3.导入项目到idea中完成版本控制
二、配置中心搭建
1.github - 配置库 --> https://github.com/zhengqingya/aigou-config
2.配置服务端
导入依赖:
<dependencies><!--springboot支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!--eureka客户端--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!--配置中心支持--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency>
</dependencies>
<!--打包插件依赖-->
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.zhengqing.aigou.ConfigServerApplication_8848</mainClass><layout>ZIP</layout></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins>
</build>
配置:
server:port: 8848
eureka:client:service-url:defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪儿 #单机环境instance:
# instance-id: gateway-9527.com #Eureka状态那一列显示的名字prefer-ip-address: true #显示客户端真实ip
spring:application:name: aigou-config-servercloud:config:server:git:uri: https://github.com/zhengqingya/aigou-config.git # TODO 填写自己的username: zhengqingya # TODO 填写自己的password: xxx # TODO 填写自己的
入口类:
@SpringBootApplication
@EnableEurekaClient //加入注册中心
@EnableConfigServer //启用配置服务端
public class ConfigServerApplication_8848 {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication_8848.class);}
}
测试:启动
http://127.0.0.1:8848/application-plat-dev.yml 或 http://127.0.0.1:8848/application-plat-test.yml ...
3.配置客户端
注:除 不需要,其他的都是客户端
1>配置8001
加入配置中心依赖
<!--配置中心支持-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>
以及打包插件
<!--打包插件依赖-->
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.zhengqing.aigou.PlatApplication_8001</mainClass><layout>ZIP</layout></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins>
</build>
配置:
server:port: 8001
spring:application:name: aigou-plat
spring:profiles:active: devcloud:config:name: application-plat #github上面名称profile: ${spring.profiles.active} #环境 java -jar -D xxx jarlabel: master #分支discovery:enabled: true #从eureka上面找配置服务service-id: aigou-config-server #指定服务名#uri: http://127.0.0.1:1299 #配置服务器 单机配置
eureka: #eureka不能放到远程配置中client:service-url:defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪儿 #单机环境instance:prefer-ip-address: true #显示客户端真实ip
启动测试:看eureka里面显示名称
2>配置网关:
加入配置中心依赖:
<!--配置中心支持-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>
以及打包插件依赖:
<!-- 打包插件依赖 -->
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.zhengqing.aigou.ZuulGatewayApplication_9527</mainClass><layout>ZIP</layout></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins>
</build>
配置文件:
server:port: 9527
spring:application:name: aigou-zuul-gateway
spring:profiles:active: devcloud:config:name: application-zuul #github上面名称profile: ${spring.profiles.active} #环境 java -jar -D xxx jarlabel: master #分支discovery:enabled: true #从eureka上面找配置服务service-id: aigou-config-server #指定服务名#uri: http://127.0.0.1:1299 #配置服务器 单机配置
eureka: #eureka不能放到远程配置中client:service-url:defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪儿 #单机环境instance:prefer-ip-address: true #显示客户端真实ip
3>7001中配置插件:
<!--打包插件依赖-->
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.zhengqing.aigou.EurekaServerApplication_7001</mainClass><layout>ZIP</layout></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins>
</build>
打包:
整个打包 :
实际上需要打包这3个 -->
打包成功之后如下:
idea启动7001
cmd执行命令打包运行:java -jar -Dspring.profiles.active=test plat_service_8001-1.0-SNAPSHOT.jar
刷新7001 就会发现8001以及注册进去了...
温馨小提示:
项目中我们可以通过Run Dashboard窗口管理启动配置,如果不能显示这个窗口,看参考:https://blog.csdn.net/qq_38225558/article/details/86294401
源码和文档:https://pan.baidu.com/s/1I813NZka7Znyi-gIRP3Sxw