Spring Cloud Config 分布式配置中心
Config服务端
依赖 Spring Web, Config Server
package com.xu.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer
@SpringBootApplication
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
application.properties
server.port=8881
spring.application.name=config-server
# configserver git配置
# 仓库的地址,如果是私有那就哦欸之账号和密码
spring.cloud.config.server.git.uri=https://gitee.com/你的账号/SpringCloudConfig.git
#私有仓库,git账号,密码
spring.cloud.config.server.git.username=你的账号
spring.cloud.config.server.git.password=你的密码
#存放配置文件的目录
spring.cloud.config.server.git.search-paths=resp
#使用的git分支
spring.cloud.config.label=master
Config 客户端
package com.xu.configclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class ConfigclientApplication {
@Value("${name}")
private String name;
@GetMapping("/test")
public String test(){
return name;
}
public static void main(String[] args) {
SpringApplication.run(ConfigclientApplication.class, args);
}
}
配置文件
bootstrap.properties文件,注意改名bootstrap.properties
依赖 Spring Web, Config Client
#配置文件名称 必须是 bootstrap.properties/yml
server.port=8882
spring.application.name=config-client
#引用配置中心得内容
spring.cloud.config.label=master
#指定配置中心application.properties的访问地址
spring.cloud.config.uri=http://localhost:8881/
spring.cloud.config.profile=dev
得到配置信息
那么问题来了,修改配置gitee配置信息需要重新Config配置中心
高可用的分布式配置中心(Spring Cloud Config)
服务端
导入注册中心依赖
<!--注册中心需要的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
启动注册中心,我的注册中心端口是8761
application.properties
server.port=8881
spring.application.name=config-server
# configserver git配置
# 仓库的地址,如果是私有那就哦欸之账号和密码
spring.cloud.config.server.git.uri=https://gitee.com/账号/SpringCloudConfig.git
#私有仓库,git账号,密码
spring.cloud.config.server.git.username=账号
spring.cloud.config.server.git.password=你的密码
#存放配置文件的目录
spring.cloud.config.server.git.search-paths=resp
#使用的git分支
spring.cloud.config.label=master
# 指定注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
添加注解
@EnableEurekaClient
启动配置中心服务端两个不同端口8881,8882
客户端
导入注册中心依赖
<!--注册中心需要的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
bootstrap.properties文件,注释了原来配置中心地址,增加了其他配置
server.port=8883
spring.application.name=config-client
#引用配置中心得内容
spring.cloud.config.label=master
#多个configserver 注册到注册中心后就不单独指定了
#指定配置中心的访问地址(已经不需要了,为了高可用已经相当于配置集群了)
#spring.cloud.config.uri=http://localhost:8881/
spring.cloud.config.profile=dev
#配置文件名称 必须是 bootstrap.properties/yml
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
#表示从注册中心获取内容
spring.cloud.config.discovery.enabled=true
#表示注册中心配置中心得服务名称(就是刚才启动得两个8881和8882 CONFIG-SERVER)
spring.cloud.config.discovery.service-id=config-server
测试
消息总线(Spring Cloud Bus)
客户端
添加依赖
//刷新作用域
@RefreshScope
<!--消息总线依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.properties文件,这要打开rabbitmq消息队列
server.port=8883
spring.application.name=config-client
#引用配置中心得内容
spring.cloud.config.label=master
#多个configserver 注册到注册中心后就不单独指定了
#指定配置中心的访问地址
#spring.cloud.config.uri=http://localhost:8881/
spring.cloud.config.profile=dev
#配置文件名称 必须是 bootstrap.properties/yml
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
#表示从注册中心获取内容
spring.cloud.config.discovery.enabled=true
#表示注册中心配置中心得服务名称(就是刚才启动得两个8881和8882 CONFIG-SERVER)
spring.cloud.config.discovery.service-id=config-server
#消息队列端口
spring.rabbitmq.host=47.99.191.178
# 放行bus/refresh 接口
management.endpoints.web.exposure.include=bus-refresh
启动两个config-client 端口分别是8883,8884
此时分别访问两个端口,都能成功访问
修改gitee配置文件
分别访问端口,配置内容没有改变…
然后POST访问任意一个config-client端口.
我访问http://localhost:8883/actuator/bus-refresh
稍等会返回空内容
在线我们再次访问端口8883,8884
服务链路追踪(Spring Cloud Sleuth)
安装zipkin
ZIPKIN下载
他的端口是9411,localhost:9411访问
feigin加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
Eureka-client配置 添加了spring.zipkin.base-url:http://localhost:9411,然后启动
server:
port: 8772
spring:
application:
name: eureka-client
zipkin:
base-url: http://localhost:9411
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka
配置feign配置文件添加 spring.zipkin.base-url:http://localhost:9411 并启动feign
server:
port: 8781
spring:
application:
# Ӧ������
name: fegin
zipkin:
base-url: http://localhost:9411
eureka:
client:
service-url:
# ע�����ĵ�ַ
defaultZone: http://localhost:8761/eureka
#开启断路器
feign:
hystrix:
enabled: true
EurekaServer开启
访问localhost:8781/hello
查看zipkin,都进来了
点击依赖