1、创建Eureka注册中心Discovery-Eureka-8000
2、创建分布式配置中心服务端native-config
项目结构如下:
(1)导入pom文件
<!--springcloud相关的版本在父类继承-->
<dependencies>
<!--配置服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--注册服务客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
(2)添加yml文件
server:
port: 8010
spring:
application:
name: config-center
profiles:
# 配置的本地环境,不然会到git中查找文件(You need to configure a uri for the git repository)
active: native
cloud:
config:
server:
native:
search-locations: classpath:/config01/ #需要读取的本地文件配置路径
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8000/eureka/
(3)创建主配置文件config01/native-config-native.yml
config:
info: native-config-native.yml version= 4
(4)启动类
package com.xiaolun;
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;
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer # 开启配置中心注解
public class NativeConfigMain9010 {
public static void main(String[] args) {
SpringApplication.run(NativeConfigMain9010.class, args);
}
}
3、创建分布式配置中心客户端HttpTest7000
(1)导入pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<exclusions> <exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
(2)创建配置文件applcation.yml
server:
port: 7000
eureka:
client:
register-with-eureka: true #将自己入驻进eureka
service-url:
defaultZone: http://localhost:8000/eureka
(3)创建配置文件bootstrap.yml
spring:
application:
name: native-config
cloud:
config:
enabled: true # 关闭spring cloud config
discovery:
enabled: false # 是否开启配置信息发现,此时配置不开启,表示从本地中读取文件
service-id: config-center # 配置中心微服务名称
uri: http://localhost:8010 # uri 配置中心地址
fail-fast: true
profiles:
active: native
通过上面的配置,我们可以找到分布式配置中心的文件
native-config-native.yml
({spring.application.name}-{spring.profiles.active}.yml)
注意:当我们在applcation.yml和bootstrap.yml文件中都配置spring.application.name属性时,由于bootstrap.yml文件先加载,因此,applcation.yml文件中spring.application.name属性会覆盖bootstrap.yml文件。
(4)业务类-controller
package com.xiaolun.zuulTest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ZuulHello {
@Value("${config.info}") //读取本地的配置信息
private String configInfo;
@RequestMapping("configTest")
@ResponseBody
public String configTest() {
return configInfo;
}
}
(5)主启动类
package com.xiaolun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class HttpTestMain7000 {
public static void main(String[] args) {
SpringApplication.run(HttpTestMain7000.class, args);
}
}
4、测试
依次启动Discovery-Eureka-8000、native-config和HttpTest7000,浏览器输入下面的网址:
http://localhost:7000/configTest
能够读取配置中心中的配置文件native-config-native.yml,如下图所示,表明配置成功。