微服务学习笔记十 Spring Cloud Config远程配置

本文涉及的产品
任务调度 XXL-JOB 版免费试用,400 元额度,开发版规格
注册配置 MSE Nacos/ZooKeeper,118元/月
云原生网关 MSE Higress,422元/月
简介: 微服务学习笔记十 Spring Cloud Config远程配置

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200717143242835.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

创建配置文件,上传至Github


```yaml

server:

 port: 8070

eureka:

 client:

   serviceUrl:

     defaultZone: http://localhost:8761/eureka/

spring:

 applicatiom:

   name: configclient

```


**创建ConfigServer,新建maven工程**,pom.xml


```yaml

<dependencies>

   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-config-server</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>

</dependencies>

```


创建配置文件 application.yml


```yaml

server:

 port: 8888

spring:

 application:

   name: configserver

 cloud:

   config:

     server:

       git:

         uri: https://github.com/hnust-xijing/springcloud1.git

         search-paths: config

         username: root

         password: root

     label: master

eureka:

 client:

   serviceUrl:

     defaultZone: http://localhost:8761/eureka/

```


创建启动类


```java

package com.shuang;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;


@SpringBootApplication

@EnableConfigServer

public class ConfigServerApplication {

   public static void main(String[] args) {

       SpringApplication.run(ConfigServerApplication.class,args);

   }

}

```


**创建Config Client**

创建maven工程,pom.xml


```yaml

<dependencies>

   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-config</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>


   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>

</dependencies>

```



创建bootstrap.yml


```yaml

spring:

 cloud:

   config:

     name: configclient

     label: master

     discovery:

       enabled: true

       service-id: configserver

eureka:

 client:

   service-url:

     defaultZone: http://localhost:8761/eureka/

```


注解声明:

spring.cloud.config.name:当前服务注册在Eureka server上的名称,与远程仓库配置文件名对应。

spring.cloud.config.labal:Git Repository的分支。

spring.cloud.config.discovery.enable:是否开启Config服务发现支持。

spring.cloud.config.discovery.service-id:配置中心在Eureka Server上注册的名称。


创建启动类


```java

package com.shuang;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication

public class ConfigClientApplication {

   public static void main(String[] args) {

       SpringApplication.run(ConfigClientApplication.class,args);

   }

}

```


创建Handler


```java

package com.shuang.controller;


import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


@RestController

@RequestMapping("/hello")

public class HelloHandler {

   @Value("${server.port}")

    private String port;


   @GetMapping("/index")

   public String index(){

       return this.port;

   }

}

```


依次启动:

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200717143454752.png)

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200717143511238.png)

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200718063924489.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200718063940723.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

目录
相关文章
|
2月前
|
存储 网络协议 Nacos
高效搭建Nacos:实现微服务的服务注册与配置中心
Nacos(Dynamic Naming and Configuration Service)是阿里巴巴开源的一款动态服务发现、配置管理和服务管理平台。它旨在帮助开发者更轻松地构建、部署和管理分布式系统,特别是在微服务架构中。
560 81
高效搭建Nacos:实现微服务的服务注册与配置中心
|
2月前
|
Java 开发者 微服务
从单体到微服务:如何借助 Spring Cloud 实现架构转型
**Spring Cloud** 是一套基于 Spring 框架的**微服务架构解决方案**,它提供了一系列的工具和组件,帮助开发者快速构建分布式系统,尤其是微服务架构。
322 69
从单体到微服务:如何借助 Spring Cloud 实现架构转型
|
1月前
|
搜索推荐 NoSQL Java
微服务架构设计与实践:用Spring Cloud实现抖音的推荐系统
本文基于Spring Cloud实现了一个简化的抖音推荐系统,涵盖用户行为管理、视频资源管理、个性化推荐和实时数据处理四大核心功能。通过Eureka进行服务注册与发现,使用Feign实现服务间调用,并借助Redis缓存用户画像,Kafka传递用户行为数据。文章详细介绍了项目搭建、服务创建及配置过程,包括用户服务、视频服务、推荐服务和数据处理服务的开发步骤。最后,通过业务测试验证了系统的功能,并引入Resilience4j实现服务降级,确保系统在部分服务故障时仍能正常运行。此示例旨在帮助读者理解微服务架构的设计思路与实践方法。
112 17
|
19天前
|
传感器 监控 安全
智慧工地云平台的技术架构解析:微服务+Spring Cloud如何支撑海量数据?
慧工地解决方案依托AI、物联网和BIM技术,实现对施工现场的全方位、立体化管理。通过规范施工、减少安全隐患、节省人力、降低运营成本,提升工地管理的安全性、效率和精益度。该方案适用于大型建筑、基础设施、房地产开发等场景,具备微服务架构、大数据与AI分析、物联网设备联网、多端协同等创新点,推动建筑行业向数字化、智能化转型。未来将融合5G、区块链等技术,助力智慧城市建设。
|
1月前
|
监控 JavaScript 数据可视化
建筑施工一体化信息管理平台源码,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
智慧工地云平台是专为建筑施工领域打造的一体化信息管理平台,利用大数据、云计算、物联网等技术,实现施工区域各系统数据汇总与可视化管理。平台涵盖人员、设备、物料、环境等关键因素的实时监控与数据分析,提供远程指挥、决策支持等功能,提升工作效率,促进产业信息化发展。系统由PC端、APP移动端及项目、监管、数据屏三大平台组成,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
|
2月前
|
运维 监控 Java
为何内存不够用?微服务改造启动多个Spring Boot的陷阱与解决方案
本文记录并复盘了生产环境中Spring Boot应用内存占用过高的问题及解决过程。系统上线初期运行正常,但随着业务量上升,多个Spring Boot应用共占用了64G内存中的大部分,导致应用假死。通过jps和jmap工具排查发现,原因是运维人员未设置JVM参数,导致默认配置下每个应用占用近12G内存。最终通过调整JVM参数、优化堆内存大小等措施解决了问题。建议在生产环境中合理设置JVM参数,避免资源浪费和性能问题。
140 3
|
2月前
|
消息中间件 监控 Java
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
51 6
|
2月前
|
Java 关系型数据库 MySQL
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
84 5
|
2月前
|
缓存 监控 Java
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
65 5
|
3月前
|
Java 网络安全 Nacos
Nacos作为流行的微服务注册与配置中心,其稳定性与易用性广受好评
Nacos作为流行的微服务注册与配置中心,其稳定性与易用性广受好评。然而,“客户端不发送心跳检测”是使用中常见的问题之一。本文详细探讨了该问题的原因及解决方法,包括检查客户端配置、网络连接、日志、版本兼容性、心跳检测策略、服务实例注册状态、重启应用及环境变量等步骤,旨在帮助开发者快速定位并解决问题,确保服务正常运行。
75 5