网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Springcloudconfig集成的示例分析-创新互联

这篇文章主要介绍了Spring cloud config集成的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

成都创新互联公司主营拜城网站建设的网络公司,主营网站建设方案,app软件开发公司,拜城h5成都微信小程序搭建,拜城网站营销推广欢迎拜城等地区企业咨询

Spring Cloud Config 分为

  • Config Server:

    • 分布式配置中心,是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息

  • Config Client:

    • 通过指定配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息

Spring boot版本2.1.8.RELEASE

服务中心使用Consu,启动Consu

1.配置中心(服务端)

easy-config

(1)添加依赖


  org.springframework.boot
  spring-boot-starter-web



  org.springframework.cloud
  spring-cloud-config-server


  org.springframework.cloud
  spring-cloud-starter-consul-discovery

(2)配置

在resources下

A. 添加 bootstrap.properties

spring.profiles.active=native本地存储配置方式

也可以使用git方式

server.port=8091
spring.application.name=easy-config
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port}
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/config/

B. 添加config/easy-api-dev.properties

hello-string=我是来自配置中心的
(3)修改启动类

添加注解@EnableConfigServer开启配置服务支持

package com.tydt.easy.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class EasyConfigApplication {

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

}

启动easy-config

浏览器访问 http://localhost:8091/easy-api/dev

返回结果

{
 "name": "easy-api",
 "profiles": [
  "dev"
 ],
 "label": null,
 "version": null,
 "state": null,
 "propertySources": [
  {
   "name": "classpath:/config/easy-api-dev.properties",
   "source": {
    "hello-string": "我是来自配置中心的"
   }
  }
 ]
}

说明:

配置中心的配置文件会被转化成相应的web接口

  • /{application}/{profile}[/{label}]

  • /{application}/{profile}.yml

  • /{label}/{application}-{profile}.yml

  • /{application}/{profile}.properties

  • /{label}/{application}-{profile}.properties

2.客户端

easy-api

(1)添加依赖


 org.springframework.boot
 spring-boot-starter-web


 org.springframework.cloud
 spring-cloud-starter-consul-discovery


 org.springframework.cloud
 spring-cloud-starter-config

(2)配置

添加配置bootstrap.properties

通过注册中心的发现服务,去配置中心查找配置

server.port=8083
spring.application.name=easy-api
spring.profiles.active=dev
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500

spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.heartbeat.enabled=true

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=easy-config
#设为true,如果无法连接config server,启动时会抛异常,并停止服务
spring.cloud.config.fail-fast=true

(3)测试方法

package com.tydt.engine.api.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 HelloController {
 @Value("${hello-string}")
 private String helloString;
 @RequestMapping("/")
 public String Hello(){
  return "hello,easy-api,"+helloString;
 }
}

3.测试

启动easy-api

测试地址

http://localhost:8083/

返回结果

hello,easy-api,我是来自配置中心的

4.更新

修改了配置中心的配置后,如何读取到新的配置呢

(1)修改测试方法

添加注解 @RefreshScope

package com.tydt.easy.api.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class HelloController {
 @Value("${hello-string}")
 private String helloString;
 @RequestMapping("/")
 public String Hello(){
  return "hello,easy-api,"+helloString;
 }
}

启动easy-config

启动easy-api

测试地址

http://localhost:8083/

返回结果

hello,easy-api,我是来自配置中心的

(2)修改配置

easy-config的resources/config/easy-api-dev.properties

hello-string=我是来自配置中心的111

重启easy-config

执行http://localhost:8083/actuator/refresh

输出

[
 "hello-string"
]
http://localhost:8083/

输出

hello,esay-api,我是来自配置中心的111

说明:

  • 如果出现Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available

  • 无论在 Config Server 中配置什么端口,Config Client 启动时,会去访问都默认的 8888 端口

  • 出现这种情况可以删掉以前的配置文件

  • 在resources文件夹下,新建 bootstrap.properties 文件( bootstrap.yml)

感谢你能够认真阅读完这篇文章,希望小编分享的“Spring cloud config集成的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持创新互联建站,关注创新互联网站建设公司行业资讯频道,更多相关知识等着你来学习!

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:Springcloudconfig集成的示例分析-创新互联
网页网址:http://bjjierui.cn/article/ppjcs.html

其他资讯