92.【SpringCloud NetFilx】(十一)

简介: 92.【SpringCloud NetFilx】

手动创建application.yaml

在application.yaml中编写如下的配置

spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
  application:
    name: springcloud-config-dev
---
spring:
  profiles: test
  application:
    name: springcloud-config-test

按照步骤进行提交到码云上

cd spring-cloud   # 进入响应的文件夹
git add .  # 把所有新改动的文件全部添加上去
git status  # 查看添加的状态
git commit -m "first commit"   #进行提交以及备注
git push origin master #进行推送到码云

3.服务端链接Git配置

我们需要在远程建立一个仓库springcloud,来存放我们的配置,先在本地写好,然后push到远程仓库。

(1).链接操作

1.新建springcloud-config-server-3344模块导入pom.xml依赖

<dependencies>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!--       监控信息 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>>

2.resource下创建application.yml配置文件,Spring Cloud Config服务器从git存储库(必须提供)为远程客户端提供配置:

server:
  port: 3344
#告诉微服务我是谁
spring:
  application:
    name: SpringCloud-config-server
  #链接远程的仓库
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/lwt121788/spring-cloud.git  # 这个链接是HTTPS的,不是SSH的
# 不加这个配置会报Cannot execute request on any known server 这个错:连接Eureka服务端地址不对
# 或者直接注释掉eureka依赖 这里暂时用不到eureka
#eureka:
#  client:
#    register-with-eureka: false  # 表示不想注册中心中注册自己
#    fetch-registry: false   #表示是注册中心

3.编写启动类

package com.jsxs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class configServer3344 {
    public static void main(String[] args) {
        SpringApplication.run(configServer3344.class,args);
    }
}

4.如果启动后出现下面的信息,我们需要让仓库开源

4.开源之后,成功访问到3344,但是因为我们在application.yaml中设置文档块(---)的形式,所以我们只有访问它的applicati.name才有效

(2)三种读取方式

1.http://localhost:3344/application-test.yaml


2.http://localhost:3344/application/test/master

3.http://localhost:3344/master/application-test.yaml

4.客户端链接服务端访问远程

在建立客户端之前我们还需要新建一个config-client.yml,push到远程仓库

config-client.yml文件:

spring:
  profiles:
    active: dev
---
server:
  port: 8201
#开发环境
spring:
  profiles: dev
  application:
    name: SpringCloud-provider-dept
  # Eureka 服务注册到哪里?
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
---
server:
  port: 8202
# 测试环境
spring:
  profiles: test
  application:
    name: SpringCloud-provider-dept
  # Eureka 服务注册到哪里?
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

新建一个springcloud-config-client-3355模块,并导入依赖

<dependencies>
        <!--config-->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-start -->
        <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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

resources下创建application.yml和bootstrap.yml配置文件

bootstrap.yml 是系统级别的配置

# 系统级别的配置
spring:
  cloud:
    config:
      name: config-client  #想要读取Git远程资源的名称 config-client.yaml
      uri: http://localhost:3344  #客户端链接服务端,服务端链接Git
      profile: dev  #到config-client.yaml哪个生参环境? dev
      label: master
      # 实际上是对访问链接的拆分  http://localhost:3344/application/test/master

application.yml 是用户级别的配置

# 用户级别的配置
spring:
  application:
    name: SpringCloud-config-client

创建controller包下的ConfigClientController.java 用于测试

package com.jsxs.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 this.applicationName+"\t"+this.eurekaServer+"\t"+this.port;
    }
}

主启动类

package com.jsxs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class configClient3355 {
    public static void main(String[] args) {
        SpringApplication.run(configClient3355.class,args);
    }
}

测试:

启动服务端Config_server_3344 再启动客户端ConfigClient

http://localhost:3344/config-client-dev.yaml

http://localhost:8201/config

相关文章
|
6月前
|
消息中间件 NoSQL Java
Spring Cloud项目实战Spring Cloud视频教程 含源码
Spring Cloud项目实战Spring Cloud视频教程 含源码
99 1
|
25天前
|
负载均衡 Java API
【Spring Cloud生态】Spring Cloud Gateway基本配置
【Spring Cloud生态】Spring Cloud Gateway基本配置
31 0
|
3月前
|
Java Spring
【Azure Spring Cloud】Spring Cloud Azure 4.0 调用Key Vault遇见认证错误 AADSTS90002: Tenant not found.
【Azure Spring Cloud】Spring Cloud Azure 4.0 调用Key Vault遇见认证错误 AADSTS90002: Tenant not found.
|
3月前
|
Java Spring 容器
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM
|
3月前
|
存储 Java Spring
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
|
3月前
|
SQL Java 数据库连接
【Azure Spring Cloud】Azure Spring Cloud connect to SQL using MSI
【Azure Spring Cloud】Azure Spring Cloud connect to SQL using MSI
|
3月前
|
Java 开发工具 Spring
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
|
3月前
|
NoSQL Java Redis
【Azure Spring Cloud】Java Spring Cloud 应用部署到Azure上后,发现大量的 java.lang.NullPointerException: null at io.lettuce.core.protocol.CommandHandler.writeSingleCommand(CommandHandler.java:426) at ... 异常
【Azure Spring Cloud】Java Spring Cloud 应用部署到Azure上后,发现大量的 java.lang.NullPointerException: null at io.lettuce.core.protocol.CommandHandler.writeSingleCommand(CommandHandler.java:426) at ... 异常
|
3月前
|
Java Spring
【Azure 应用服务】记一次Azure Spring Cloud 的部署错误 (az spring-cloud app deploy -g dev -s testdemo -n demo -p ./hellospring-0.0.1-SNAPSHOT.jar --->>> Failed to wait for deployment instances to be ready)
【Azure 应用服务】记一次Azure Spring Cloud 的部署错误 (az spring-cloud app deploy -g dev -s testdemo -n demo -p ./hellospring-0.0.1-SNAPSHOT.jar --->>> Failed to wait for deployment instances to be ready)
|
3月前
|
Java Maven Python
【Azure Spring Cloud】部署Azure spring cloud 失败
【Azure Spring Cloud】部署Azure spring cloud 失败