首页 /  技术专区  /  SpringCloud 宽屏模式 >

Config:客户端链接服务端访问远程

1.新建config-client.yml

spring:
    profiles:
        active: dev
        
---
server:
    port: 8201
     
#spring的配置
spring:
    profiles: dev
    application:
        name: springcloud-provider-dept

#Eureka的配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
        
---
server:
    port: 8202
    
#spring的配置
spring:
    profiles: test
    application:
        name: springcloud-provider-dept

#Eureka的配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/

2.上传git

image.png

3.新建springcloud-config-client-3355项目。

image.png

4.引入Maven依赖。

image.png

<?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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.allen</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-config-client-3355</artifactId>

    <dependencies>

        <!-- config -->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- actuator 完善监控信息-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
</project>

5.新建bootstrap.yml和application.yml,这里简单讲一下bootstrap.yml和application.yml的区别,bootstrap.yml表示系统级配置,application.yml表示用户级配置,所以bootstrap.yml要优先于application.yml。

image.png

image.png


#用户级别配置
spring:
  application:
    name: springcloud-config-client-3355
#系统级别配置
spring:
  cloud:
    config:
      name: config-client #需要从git上读取的资源名称,不要后缀
      profile: dev
      label: master
      uri: http://localhost:3344

6.新建一个ConfigClientController用来测试。

image.png

package com.allen.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer;

    @Value("${server.port}")
    private String port;

    @RequestMapping("/config")
    public String getConfig(){
        return "applicationName:" + applicationName +
                "eurekaServer:" + eurekaServer +
                "port:" + port;
    }

}

7.新建启动类

image.png

package com.allen.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

8.启动3344服务端和3355客户端。

image.png

接口测试

image.png

没有问题,关闭服务,修改配置文件,再次重启测试。

image.png

接口测试:

image.png



头像
0/200
图片验证码