博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微服务java_b2b商城系统_java商城源码100%开源适合2次开发-(七)高可用的分布式配置中心(Spring Cloud Config)...
阅读量:7064 次
发布时间:2019-06-28

本文共 4973 字,大约阅读时间需要 16 分钟。

讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下:

一、准备工作

继续使用上一篇文章的工程,创建一个eureka-server工程,用作服务注册中心。

在其pom.xml文件引入Eureka的起步依赖spring-cloud-starter-eureka-server,代码如下:

4.0.0
com.forezp
eureka-server
0.0.1-SNAPSHOT
jar
eureka-server
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
复制代码
在配置文件application.yml上,指定服务端口为8889,加上作为服务注册中心的基本配置,代码如下:复制代码
server:  port: 8889eureka:  instance:    hostname: localhost  client:    registerWithEureka: false    fetchRegistry: false    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/复制代码

入口类:

@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {    public static void main(String[] args) {        SpringApplication.run(EurekaServerApplication.class, args);    }}复制代码

二、改造config-server

在其pom.xml文件加上EurekaClient的起步依赖spring-cloud-starter-eureka,代码如下:

org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-eureka
复制代码

配置文件application.yml,指定服务注册地址为,其他配置同上一篇文章,完整的配置如下:

spring.application.name=config-serverserver.port=8888spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/spring.cloud.config.server.git.searchPaths=respospring.cloud.config.label=masterspring.cloud.config.server.git.username= your usernamespring.cloud.config.server.git.password= your passwordeureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/复制代码

最后需要在程序的启动类Application加上@EnableEureka的注解。

三、改造config-client

将其注册微到服务注册中心,作为Eureka客户端,需要pom文件加上起步依赖spring-cloud-starter-eureka,代码如下:

org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
复制代码
配置文件bootstrap.properties,注意是bootstrap。加上服务注册地址为http://localhost:8889/eureka/复制代码
1
2
3
4
5
6
7
8
9
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http:
//localhost:8888/
eureka.client.serviceUrl.defaultZone=http:
//localhost:8889/eureka/
spring.cloud.config.discovery.enabled=
true
spring.cloud.config.discovery.serviceId=config-server
server.port=
8881

  架构代码如下:

技术支持2147775633

转载地址:http://tkill.baihongyu.com/

你可能感兴趣的文章
Kubernetes首爆严重安全漏洞,请升级你的Kubernetes
查看>>
Scrum丰田之道
查看>>
渔村小厂,如何成长为5G霸王
查看>>
GitHub推出更多课程
查看>>
InfoQ播客:Tal Weiss谈JVM的可观测性、插桩、以及字节码操作
查看>>
独家!支付宝小程序技术架构全解析
查看>>
1100名达摩院“扫地僧”加持,阿里云的下一个十年
查看>>
python学习笔记-类对象的信息
查看>>
Java多线程(4):使用线程池执行定时任务
查看>>
苹果推出开源医学研究框架ResearchKit
查看>>
Java 9的日期时间格式化趋近Unicode区域设置标准
查看>>
.NET Core中的去虚
查看>>
CLion 2016.1新增Python、Swift支持,并改进了C++支持
查看>>
2016 “Better Software East/DevOps East/Agile Dev East”三个会议上的发言
查看>>
为什么要选择Apache Pulsar(一)
查看>>
基于Gitflow分支模型自动化Java项目工作流
查看>>
Azure Stack 云安全概览
查看>>
2018年测试状况调查
查看>>
Visual Studio 2015 for Linux更好地支持Linux下的开发
查看>>
解决C# 7.2中的结构体性能问题
查看>>