java spring cloud 版 b2b2c 社交电商-config配置自动刷新

ITyi · · 86 次点击 · 开始浏览   
电子商务平台源码请加企鹅求求:三伍三六二四柒二五玖。本项目采用版本选择: springboot的版本是2.1.2.RELEASE springcloud的版本是Greenwich.SR1 springcloud仓库使用的是Gitee 1、下载并安装RabbitMq 下载地址:http:/ /www.rabbitmq.com/download.html 2、SpringCloud Config Server端配置 config-server pom.xml的配置如下 ``` <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <!--<version>2.1.4.BUILD-SNAPSHOT</version>--> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lee.self</groupId> <artifactId>config</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project> ``` config-server的bootstrap.yml的配置如下: ``` server: port: 9010 spring: application: name: config cloud: config: server: git: #码云的用户名和密码及地址 username: **** password: **** uri: https://gitee.com/lijiaxi118/blog-repo eureka: client: service-url: defaultZone: http://localhost:9001/eureka/ management: endpoints: web: exposure: include: "bus-refresh" ``` 3、SpringCloud Config Client端配置 config-client pom.xml配置如下: ``` <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lee.self</groupId> <artifactId>user</artifactId> <version>0.0.1-SNAPSHOT</version> <name>user</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 增加swagger ui展示 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project> ``` config-client的bootstrap.yml配置: ``` spring: application: name: user cloud: config: discovery: service-id: config enabled: true profile: pro eureka: client: service-url: defaultZone: http://localhost:9001/eureka/ server: port: 9005 ribbon: eureka: enabled: true ``` 4、测试 启动注册中心eureka 启动config server 访问localhost:9010/user-dev.yml监测是否启动成功 启动config client 在client项目中写一个测试类如下 测试类如下 ``` @Component @RefreshScope @Data @ConfigurationProperties(prefix = "spring.redis") public class RedisConfig { private String host; private String port; } @RestController @RequestMapping("/test") public class ConfigController { @Autowired private RedisConfig redisConfig; @RequestMapping("/print") public String print() { return "host:" +"\t" + redisConfig.getHost()+"====Port:"+redisConfig.getPort(); } } ``` 页面访问http://localhost:9005/test/print,查看结果 修改gitee中的user-dev.yml文件 使用postman的post请求访问http://localhost:9010/actuator/bus-refresh 继续浏览器访问http://localhost:9005/test/print查看结果是否更新 自此config的刷新基本完成,最后一步在gitee中配置webhooks,每一次push修改之后都会自动的通知到各个客户端 4、配置仓库中的webhooks 点击gitee中的管理——》webhooks,填入外网访问的路径即可。
86 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet