增加 spring cloud config 整合用例
This commit is contained in:
		
							
								
								
									
										52
									
								
								spring-cloud/spring-cloud-config/config-client/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								spring-cloud/spring-cloud-config/config-client/pom.xml
									
									
									
									
									
										Normal 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>
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,30 @@
 | 
			
		||||
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;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
@Component
 | 
			
		||||
@ConfigurationProperties(prefix = "programmer")
 | 
			
		||||
@Data
 | 
			
		||||
@ToString
 | 
			
		||||
@RefreshScope // 定义下面配置热刷新范围
 | 
			
		||||
public class Programmer{
 | 
			
		||||
 | 
			
		||||
    private String name;
 | 
			
		||||
    private int age;
 | 
			
		||||
    private boolean married;
 | 
			
		||||
    private Date hireDate;
 | 
			
		||||
    private float salary;
 | 
			
		||||
    private int random;
 | 
			
		||||
    private Map<String, String> skill;
 | 
			
		||||
    private List company;
 | 
			
		||||
    private School school;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,12 @@
 | 
			
		||||
package com.heibaiying.configclient.config;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author : heibaiying
 | 
			
		||||
 */
 | 
			
		||||
@Data
 | 
			
		||||
public class School {
 | 
			
		||||
    private String name;
 | 
			
		||||
    private String location;
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,23 @@
 | 
			
		||||
package com.heibaiying.configclient.controller;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author : heibaiying
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
@RestController
 | 
			
		||||
public class ConfigController {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private Programmer programmer;
 | 
			
		||||
 | 
			
		||||
    @RequestMapping("programmer")
 | 
			
		||||
    public String getProgrammer() {
 | 
			
		||||
        return programmer.toString();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,17 @@
 | 
			
		||||
package com.heibaiying.configclient;
 | 
			
		||||
 | 
			
		||||
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 ConfigClientApplicationTests {
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void contextLoads() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user