栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Spring Boot + Spring Cloud 社交电子商务:服务端实现

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot + Spring Cloud 社交电子商务:服务端实现

新建工程

新建 kitty-conifg 工程,作为配置中心的服务端,负责把GIT仓库的配置文件发布为RESTFul接口

添加依赖

除了Spring Cloud依赖之外,添加配置中心依赖包。了解springcloud架构可以加求求:三五三六二四七二五九

pom.xml


    
        org.springframework.cloud
        spring-cloud-config-server
    
启动类

启动类添加注解 @EnableConfigServer,开启配置服务支持。

KittyConfigApplication.java

package com.louis.kitty.config;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplicationpublic class KittyConfigApplication {    public static void main(String[] args) {
        SpringApplication.run(KittyConfigApplication.class, args);
    }
}
配置文件

修改配置文件,添加如下内容。如果是私有仓库需要填写用户名密码,如果是公开仓库,可以不配置密码。

application.yml

server:
  port: 8020spring:
  application:
    name: kitty-config
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        serviceName: ${spring.application.name}    # 注册到consul的服务名称
    config:
     label: master  # git仓库分支
      server:
        git:
          uri: https://gitee.com/liuge1988/kitty.git  # 配置git仓库的地址
          search-paths: config-repo  # git仓库地址下的相对地址,可以配置多个,用,分割。
          username: username  # git仓库的账号
          password: password  # git仓库的密码

Spring Cloud Config也提供本地存储配置的方式,只需设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。另外也可以通过spring.cloud.config.server.native.searchLocations=file:D:/properties/属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了更好的支持内容管理和版本控制,还是比较推荐使用GIT的方式。

测试效果

启动注册中心,配置中心,访问 http://localhost:8020/kitty-consumer/dev,返回结果如下。

{    "name": "kitty-consumer",    "profiles": ["dev"],    "label": null,    "version": "1320259308dfdf438f5963f95cbce9e0a76997b7",    "state": null,    "propertySources": [{        "name": "https://gitee.com/liuge1988/kitty.git/config-repo/kitty-consumer-dev.properties",        "source": {            "consumer.hello": "hello, this is dev configurations. "
        }
    }]
}

访问 http://localhost:8020/kitty-consumer/pro,返回结果如下。

{    "name": "kitty-consumer",    "profiles": ["pro"],    "label": null,    "version": "1320259308dfdf438f5963f95cbce9e0a76997b7",    "state": null,    "propertySources": [{        "name": "https://gitee.com/liuge1988/kitty.git/config-repo/kitty-consumer-pro.properties",        "source": {            "consumer.hello": "hello, this is pro configurations. "
        }
    }]
}

上述的返回的信息包含了配置文件的位置、版本、配置文件的名称以及配置文件中的具体内容,说明server端已经成功获取了git仓库的配置信息。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/234950.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号