增加 spring cloud config 整合用例

This commit is contained in:
罗祥 2019-01-17 15:28:11 +08:00
parent 312f219451
commit 7de0cf7d9d
24 changed files with 254 additions and 54 deletions

BIN
pictures/bus-refresh.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

View File

@ -0,0 +1,32 @@
配置中心拉取git配置文件:
```$xslt
[on(6)-127.0.0.1] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/D:/git-config/spring-cloud/spring-cloud-test-config/application.yml
[nio-8010-exec-2] pClientConfigurableHttpConnectionFactoryx : No custom http config found for URL: https://github.com/heibaiying/spring-samples-for-all/info/refs?service=git-upload-pack
[nio-8010-exec-2] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
[nio-8010-exec-2] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/D:/git-config/spring-cloud/spring-cloud-test-config/application-dev.yml
[nio-8010-exec-2] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/D:/git-config/spring-cloud/spring-cloud-test-config/application.yml
```
每次启动时候都会去注册中心拉取最新的代码,但是已经启动的项目是不会热更新的
```$xslt
2019-01-17 13:35:14.986 INFO 4448 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8020/
2019-01-17 13:35:20.910 INFO 4448 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=master, version=50dcfb85cd751e4f28761cd6bad84c1f73034002, state=null
```
消息总线
```$xslt
2019-01-17 13:37:50.998 INFO 4448 --- [e0bL-TWAMhWg-19] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#496c6d94:22/SimpleConnection@185d85d2 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 63713]
2019-01-17 13:37:51.015 INFO 4448 --- [e0bL-TWAMhWg-19] o.s.amqp.rabbit.core.RabbitAdmin : Auto-declaring a non-durable, auto-delete, or exclusive Queue (springCloudBus.anonymous.iY4TIIi9TSe0bL-TWAMhWg) durable:false, auto-delete:true, exclusive:true. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.
```
刷新应用上下文,热更新
```$xslt
Attempting to connect to: [127.0.0.1:5672]
Created new connection: rabbitConnectionFactory.publisher#b00f2d6:0/SimpleConnection@403c0406 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 62748]
Fetching config from server at : http://DESKTOP-8JGSFLJ:8020/
Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@62e12f66
```
http://localhost:8030/actuator/bus-refresh

View File

@ -1,14 +0,0 @@
package com.heibaiying.config.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.heibaiying.config</groupId>
<artifactId>spring-cloud-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>config-client</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--config client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--bus-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--因为要用到端点功能(主要是刷新端点),所以需要导入actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,18 @@
package com.heibaiying.configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}

View File

@ -1,8 +1,9 @@
package com.heibaiying.config.server.config;
package com.heibaiying.configclient.config;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import java.util.Date;
@ -13,7 +14,8 @@ import java.util.Map;
@ConfigurationProperties(prefix = "programmer")
@Data
@ToString
public class Programmer {
@RefreshScope // 定义下面配置热刷新范围
public class Programmer{
private String name;
private int age;

View File

@ -1,6 +1,7 @@
package com.heibaiying.config.server.controller;
package com.heibaiying.configclient.controller;
import com.heibaiying.config.server.config.Programmer;
import com.heibaiying.configclient.config.Programmer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -16,7 +17,7 @@ public class ConfigController {
private Programmer programmer;
@RequestMapping("programmer")
public Programmer getProgrammer(){
return programmer;
public String getProgrammer() {
return programmer.toString();
}
}

View File

@ -0,0 +1,47 @@
server:
port: 8030
spring:
application:
name: config-client
cloud:
config:
discovery:
enabled: true
# 这里我们指定的是服务名 如果配置中心有多个,且用同一个服务名,我们的客户端拉取配置的时候是负载均衡的,配置中心也就是高可用
serviceId: config-server
# 指定分支
label: master
# 指定环境
profile: dev
bus:
#开启总线
enabled: true
# 打开ack跟踪的标志默认关闭
trace:
enabled: true
# 使用bus实现热更新
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
# 注意指定注册中心的配置不要从公共配置中拉取,要在本地的配置文件中指定
# 因为我们必须要先从注册中心去获取可用的配置中心, 从配置中心去拉取配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/
# 暴露热刷新的端点
#management:
# endpoint:
# bus-refresh:
# enabled: true
management:
endpoints:
web:
exposure:
include: bus-refresh

View File

@ -1,4 +1,4 @@
package com.heibaiying.config.client;
package com.heibaiying.configclient;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ClientApplicationTests {
public class ConfigClientApplicationTests {
@Test
public void contextLoads() {

View File

@ -9,9 +9,7 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>server</artifactId>
<artifactId>config-server</artifactId>
<dependencies>
<dependency>
@ -22,9 +20,12 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>

View File

@ -1,15 +1,17 @@
package com.heibaiying.config.server;
package com.heibaiying.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ServerApplication {
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
SpringApplication.run(ConfigServerApplication.class, args);
}
}

View File

@ -0,0 +1,26 @@
server:
port: 8020
# 指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/
# 指定服务命名
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/heibaiying/spring-samples-for-all/
search-paths: spring-cloud/spring-cloud-test-config/
# 如果代码仓库是公开的 则 不需要设置用户名和密码
username:
password:
# 指定拉取的配置文件的存放的位置,配置文件最后存储的目录为 basedir + search-paths
# 这个地方还需要注意的是,配置文件的仓库最好只放配置文件
# 因为配置中心不仅会拉取search-paths下的文件还会把uri指定仓库中的全部文件拉取到basedir下
basedir: D:\git-config
# 指定分支
label: master

View File

@ -0,0 +1,17 @@
package com.heibaiying.configserver;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConfigServerApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@ -9,16 +9,13 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>client</artifactId>
<artifactId>eureka</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

View File

@ -0,0 +1,16 @@
package com.heibaiying.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}

View File

@ -0,0 +1,12 @@
server:
port: 8010
eureka:
instance:
hostname: localhost
client:
# 设置为false,代表不向注册中心注册自己
register-with-eureka: false
# 注册中心主要用于维护服务并不需要检索服务所以设置为false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

View File

@ -1,4 +1,4 @@
package com.heibaiying.config.server;
package com.heibaiying.eureka;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ServerApplicationTests {
public class EurekaApplicationTests {
@Test
public void contextLoads() {

View File

@ -7,10 +7,16 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<version>2.0.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
<module>config-client</module>
<module>config-server</module>
<module>eureka</module>
</modules>
<groupId>com.heibaiying.config</groupId>
<artifactId>spring-cloud-config</artifactId>
<version>0.0.1-SNAPSHOT</version>

View File

@ -1,15 +0,0 @@
server:
port: 8010
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/heibaiying/spring-samples-for-all/
search-paths: spring-cloud/spring-cloud-test-config/
# 如果代码仓库是公开的 则 不需要设置用户名和密码
username:
password:
label: master