java B2B2C 多租户电子商城系统-Spring Cloud配置中心客户端读取配置

微服务连接配置中心来实现外部配置的读取。需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 一零三八七七四六二六

引入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>
</dependencies>
复制代码

spring-cloud-starter-config:配置中心客户端的依赖。

spring-boot-starter-aop,spring-retry:这两个是连接配置中心快速失败和重试需要用到的依赖。

增加启动类

@EnableDiscoveryClient
@SpringBootApplication
public class ServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }

}
复制代码

添加配置 在bootstrap.yml中添加如下配置,必须是bootstrap,application中不行。

spring: 
  application: 
    name: config-client
  cloud:
    config:
      #username: 
      #password: 
      name: ${git.application}
      profile: ${git.profile}
      label: ${git.label}
      fail-fast: true
      retry:
        initial-interval: 2000
        max-attempts: 5
      discovery: 
        enabled: true
        service-id: config-center 

eureka:
  client: 
    serviceUrl:
      defaultZone: ${register-center.urls}
复制代码

可以看出配置比较简单,下面也不再详述。

application.yml配置文件参考如下:

spring: 
  profiles: 
    active: config-client1

eureka:
  instance:
    prefer-ip-address: true  
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    lease-expiration-duration-in-seconds: ${lease-expiration-duration-in-seconds}
    lease-renewal-interval-in-seconds: ${lease-renewal-interval-in-seconds}

---
spring: 
  profiles: config-client1

server: 
  port: ${config-client1.server.port}

---
spring: 
  profiles: config-client2

server: 
  port: ${config-client2.server.port}
复制代码

Maven filter配置

#git
git.application=application
git.profile=dev
git.label=master

复制代码

读取配置

@RestController
public class TestController {

    @Value("${username}")
    private String username;
复制代码

使用Value就能读取配置中心的配置,当然也可以通过其他方式获取SpringCloud中的配置,参考之前SpringBoot系列文章。

启动服务 通过指定Profile启动两台微服务,它们可以读取配置中心的内容。

spring-boot:run -Drun.profiles=config-client1 -P dev
spring-boot:run -Drun.profiles=config-client2 -P dev
复制代码

java B2B2C Springboot多租户电子商城系统

转载于:https://juejin.im/post/5c4a676df265da616c65efb8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值