consul-系统学习3-springBoot整合Consul
一、springBoot+Consul注册中心1.1、代码pom.xml<?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:schemaLoc
·
一、springBoot+Consul注册中心
1.1、代码

pom.xml
<?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 https://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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>fei.zhou</groupId>
<artifactId>consul-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consul-01</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--consul-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</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>
</project>
Consul01Application
package fei.zhou.consul01;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class Consul01Application {
public static void main(String[] args) {
SpringApplication.run(Consul01Application.class, args);
}
}
application.properties
server.port=8010
spring.application.name=consul-01
# consul 配置
spring.cloud.consul.host=60.205.188.229
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.health-check-path=/actuator/health
# 注册到 Consul 的服务名称,后期客户端会根据这个名称来进行服务调用
spring.cloud.consul.discovery.service-name=consul-01
spring.cloud.consul.discovery.heartbeat.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
1.2、核心代码



1.3、效果

二、springBoot+Consul注册中心+配置中心
对原有的代码进行改造
2.1、代码

pom.xml
<?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 https://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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>fei.zhou</groupId>
<artifactId>consul-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consul-01</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 注册中心依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- 配置中心依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--consul-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</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>
</project>
bootstrap.properties
server.port=8010
spring.application.name=consul-01
# consul ip
spring.cloud.consul.host=60.205.188.229
# consul 端口
spring.cloud.consul.port=8500
# 健康检测API
spring.cloud.consul.discovery.health-check-path=/actuator/health
# 注册到 Consul 的服务名称,后期客户端会根据这个名称来进行服务调用
spring.cloud.consul.discovery.service-name=consul-01
# 使用心跳检查push机制定期向consul server汇报自身存活情况,逾期没有汇报则server认为异常。
spring.cloud.consul.discovery.heartbeat.enabled=true
# 列出公开端点的ID
management.endpoints.web.exposure.include=*
# 详细信息显示给所有用户
management.endpoint.health.show-details=always
#让服务以ip的方式注册
spring.cloud.consul.discovery.prefer-ip-address=true
# 开启配置中心
spring.cloud.consul.config.enabled=true
# consul创建key值与环境的分隔符
spring.cloud.consul.config.profile-separator=-
# 配置文件的文件格式
spring.cloud.consul.config.format=properties
# 配置文件的存储的根路径,默认为config
spring.cloud.consul.config.prefix = config
# 配置文件存储key的值,或者理解为文件的名称,默认为data
spring.cloud.consul.config.data-key = data
# 启用配置自动刷新
spring.cloud.consul.config.watch.enabled=true
# 刷新延迟时间,单位:秒
spring.cloud.consul.config.watch.wait-time=1
# 刷新频率,单位:毫秒
spring.cloud.consul.config.watch.delay=10000
DemoController
@RestController
public class DemoController {
@Autowired
private OrderProperties orderProperties;
@RequestMapping("/search")
public String searchDiscount() {
return orderProperties.toString();
}
}
OrderProperties
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@ConfigurationProperties("order")
//动态刷新配置
@RefreshScope
@Data
@Component
public class OrderProperties {
private Integer discount = 100;
}
2.2、代码变化的地方




注意点:
- 配置文件要用bootstrap.properties文件
- 配置中心默认加载bootstrap.(yml|yaml|properties)配置文件
2.3、启动项目


2.4、修改consul的配置

order.discount=60
config/consul-01/data
01、文件夹解释
-
config
配置文件的存储的根路径,默认为config spring.cloud.consul.config.prefix = config -
consul-01
spring.application.name=consul-01 -
data
# 存储key的文件名称,默认为data spring.cloud.consul.config.data-key = data
02、效果
修改consul的配置后,日志输出拉取最新的配置

重新请求后,发现内容改变

代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/mysql_mybaties_DB/consul-learn
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)