2019-06-12 eureka client 端以及 con
1/pom 文件
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zbiti.zz</groupId>
<artifactId>member</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>member</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!------actuator用于动态刷新配置文件的--->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2、从git 配置中心获取配置内容,必须将config client 端的配置文件命名为 bootstrap.yml
server:
port: 1006
eureka:
client:
service-url:
#eureka 客户端配置
defaultZone: http://localhost:1005/eureka/
register-with-eureka: true
fetch-registry: true
spring:
application:
#应用名称,需要和git 上存的配置文件前缀一致
name: member
cloud:
#配置中心客户端配置
config:
#环境名称,需要和git上存的配置文件命名中的环境一致 eg:member-dev.yml
profile: dev
discovery:
#配置中心server 在eureka注册 的服务名称
service-id: config-server
enabled: true
#开启所有端点手动根据配置中心配置 刷新
management:
endpoints:
web:
exposure:
#include: "*"
include: /refresh #这是配置刷新路径
3、系统主类,开启eureka客户端
@SpringBootApplication
@EnableEurekaClient
public class MemberApplication {
public static void main(String[] args) {
SpringApplication.run(MemberApplication.class, args);
}
}
4、需要使用配置中心的文件地方,开启@Configuration(有人说不用这个注解),开启手动刷新注解 @RefreshScope, 当git 中配置文件内容变更,只需要 post 方式 访问当前这个节点 eg: http://127.0.0.1:1006/actuator/refresh 刷新即可
@RestController
@Configuration
@RefreshScope //开启手动刷新配置文件的注解
public class IndexController {
#git配置文件中一个属性的读取
@Value("${userAge}")
private StringuserAge;
@RequestMapping("/get_user_age")
public String getUserAge(){
return userAge;
}
}