SpringCloud-config(服务端,客户端的环境搭建)

                  SpringCloud-config(服务端,客户端的环境搭建)

注明:此项目为本人学习尚硅谷老师的教学视频然后整理核心的配置文件,所有的项目均在以下地址下载。
https://github.com/xwbGithub/microservicecloud下载

本项目请参考microservicecloud-config-3344(作为config配置中心),microservicecloud-config(作为客户端)

服务端的搭建

新建module模块microservicecloud-config-3344(作为cloud的配置中心模块)

pom核心文件

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

yml文件

server:
  port: 3344
spring:
  application:
    name: microservicecloud-config
  cloud:
    config:
      server:
        git:
          #username:  xxxx
          #password: xxxx
          uri: https://github.com/xwbGithub/microservicecloud-config.git #github上的原始地址

主启动类添加EnableConfigServer注解

@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp {
    public static void main(String[] args) {
        SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
    }
}

修改主机映射

Windows对应的位置是:C:\Windows\System32\drivers\etc\hosts

Linux对应的是:/etc/hosts

127.0.0.1 config-3344.com

测试

启动微服务3344

访问http://config-3344.com:3344/application-dev.yml

本地config搭建

在本地的项目microservicecloud-config下创建microservicecloud-config-client.yml文件

microservicecloud-config-client.yml内容如下:

yml

spring:
  profiles:
    active:
      - dev
---
server:
  port: 8201
spring:
  profile: dev
  application:
      name: microservicecloud-config-client
eureka:
  client:
    service-url:
      defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
  port: 8202
spring:
  profiles: test
  application:
    name:  microservicecloud-config-client
eureka:
  client:
    service-url:
        defaultZone:  http://eureka-test.com:7001/eureka/

然后上传到github上本人地址为https://github.com/xwbGithub/microservicecloud-config

b客户端的搭建

创建客户端modulemicroservicecloud-config-client-3355

pom

<!-- SpringCloud Config客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

Application.ym(是用户级的配置资源项)

Bootstrap.yml(是系统级的,优先级更高)

创建bootstrap.yml文件

spring:
  cloud:
    config:
      name: microservicecloud-config-client # 需要从github上读取资源名称 注意没有yml
      profile: dev # 本次访问的配置项
      label:  master
      uri:  http://config-3344.com:3344 # 本资源微服务启动后先去找3344号服务,通过SpringCloudConfig获取Github的服务地址

 

application.yml文件

spring:
  application:
    name:  microservicecloud-config-client

host下添加映射文件

127.0.0.1 client-config.com

新建controller类ConfigClientRest

package com.atguigu.springcloud.rest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientRest
{
   @Value("${spring.application.name}")
   private String applicationName;
   @Value("${eureka.client.service-url.defaultZone}")
   private String eurekaServers;
   @Value("${server.port}")
   private String port;
   @RequestMapping("/config")
   public String getConfig() {
      String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
      System.out.println("******str: " + str);
      return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
   }
}

启动主动类

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

测试

启动config的服务器3344

然后启动3355作为client准备访问

测试1:

切换bootstrap.yml的访问配置项

profile:dev(切换bootstrap.yml的访问配置项)

              1、dev默认在github上对应的端口就是8201

             2、http://client-config.com:8201/config

Profile: test

           1、test默认在github上对应的端口就是8202

           2、http://client-config.com:8202/config

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值