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

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
注册配置 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)

目录
相关文章
|
22天前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
1天前
|
Java 微服务 Spring
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
文章介绍了如何利用Spring Cloud Alibaba快速构建大型电商系统的分布式微服务,包括服务限流降级等主要功能的实现,并通过注解和配置简化了Spring Cloud应用的接入和搭建过程。
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
|
4天前
|
运维 Java Nacos
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
|
17天前
|
消息中间件 监控 Java
解锁Spring Cloud微服务架构的奥秘:深度剖析拆分原则,打造高内聚低耦合的业务创新引擎!
【8月更文挑战第3天】踏入微服务领域,Spring Cloud以丰富组件助力高效系统构建。微服务拆分需遵循原则确保系统高内聚低耦合且能适应变化。首要原则为单一职责,每个服务专注一个业务功能,降低复杂度并提高可维护性。其次,追求高内聚低耦合以减少服务间影响。围绕业务域拆分有助于保持逻辑清晰及团队协作。处理数据一致性问题时,考虑采用最终一致性模型。Spring Cloud提供Eureka、Zuul/Gateway、Sleuth和Config等工具支持服务发现、路由、跟踪及配置管理,共同构建灵活健壮的微服务架构。
33 2
|
17天前
|
NoSQL Java Redis
Spring Boot集成Redis全攻略:高效数据存取,打造性能飞跃的Java微服务应用!
【8月更文挑战第3天】Spring Boot是备受欢迎的微服务框架,以其快速开发与轻量特性著称。结合高性能键值数据库Redis,可显著增强应用性能。集成步骤包括:添加`spring-boot-starter-data-redis`依赖,配置Redis服务器参数,注入`RedisTemplate`或`StringRedisTemplate`进行数据操作。这种集成方案适用于缓存、高并发等场景,有效提升数据处理效率。
72 2
|
22天前
|
监控 NoSQL Java
Spring Boot Actuator 使用和常用配置
Spring Boot Actuator 使用和常用配置
36 5
|
22天前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
41 3
|
8天前
|
Java Spring
Spring Boot Admin 授权配置
Spring Boot Admin 授权配置
14 0
|
8天前
|
监控 Java Spring
Spring Boot Admin 配置应用
Spring Boot Admin 配置应用
18 0

热门文章

最新文章