Consul不仅可以作为Spring Cloud中服务的注册中心,也可以作为其配置中心,这样一个系统就可以实现服务发现和统一配置,减少系统维护的麻烦,其中在使用Consul作为配置中心使用的过程中可以说是血泪史,参考其他博客大部分含糊其辞,Spring Cloud中文网站,方便大家参考。
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- consul discovery-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- consul config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
<!--configuration-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
依赖说明,创建的maven聚合项目,父pom中引入的Spring Cloud版本为Hoxton.SR3
,Spring Boot版本为2.2.5.RELEASE
@ConfigurationProperties
注解时,IDEA会提示错误Spring Boot Configuration Annotation Processor not Configured,引入当前依赖就好。@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigurationProperties(ConfigBean.class)
public class ConsulApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulApplication.class,args);
}
}
@Data
@ConfigurationProperties(prefix = "db-config")
@Component
@RefreshScope
public class ConfigBean {
private String driverClassName;
private String url;
private String userName;
private String password;
}
application.yml是项目启动的配置文件,这里只配置Consul作为服务的配置中心,服务注册中心可以参考spring cloud consul
spring:
application:
name: consul-client
profiles:
active: dev
cloud:
consul:
discovery:
hostname: localhost
port: 8500
health-check-path: /actuator
health-check-interval: 10s
health-check-timeout: 30s
enabled: true
server:
port: 8080
注意名称一定是bootstrap.yml,和application.yml同样在resources目录下
这也是博主踩坑的地方,网上很多资料将Consul的spring cloud consul config配置放在服务发现spring cloud consul discovery的配置一块,项目启动请求远程配置信息不会报错,但是注入为null,最终找了很久才找到原因。
spring:
cloud:
consul:
config:
enabled: true
prefix: config
default-context: consul-client
profile-separator: ','
data-key: data
format: yaml
host: localhost
port: 8500
,
启动consul服务端后,访问http://localhost:8500,在这里创建键值对
key:
config/consul-client,dev/data
value:
desc: Consul Confisuration Test
dbconfig:
driverClassName: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@localhost:1521:orcl
userName: system
password: orcl
注意:
@RestController
public class TestController {
@Resource
private ConfigBean configBean;
@Value("${desc}")
private String desc;
@RequestMapping("/desc")
public String desc(){
return desc;
}
@RequestMapping("/config")
public ConfigBean getUserInfo(){
return configBean;
}
}
访问http://localhost:8080/desc,查看属性desc的注入情况
Consul Configuration Test
访问http://localhost:8080/config,查看ConfigBean的注入情况
{"driverClassName":"oracle.jdbc.driver.OracleDriver","url":"jdbc:oracle:thin:@localhost:1521:orcl","userName":"system","password":"orcl"}
最后,Consul的官方文档都有详细的解释,查阅资料时一定要选择合适的资料,建议首先考虑官网。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章