最近按照网上教程学习搭建spring cloud消息总线例子,但是网上的步骤大都基于spring boot 1.x的版本,导致踩了很多坑,今天在这里更新一下基于spring boot 2.0 搭建spring cloud消息总线的教程,以及需要注意的事项。
本文采用的IDE为IntelliJ; 构建的项目的方式通过Maven构建。
eurekaServer:创建服务注册中心
1. 添加依赖服务:spring-cloud-starter-netflix-eureka-server,spring-boot-starter-web,具体配置如下:
4.0.0
com.springcloud
configeurekaserver
0.0.1-SNAPSHOT
jar
configeurekaserver
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
Finchley.M8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
2. 添加配置文件application.yml:
server:
port: 8889
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3. 主程序添加@EnableEurekaServer注解
config-server服务
1.添加依赖服务:spring-cloud-starter-netflix-eureka-server, spring-cloud-config-server, spring-cloud-starter-eureka,具体配置如下:
4.0.0
com.springcloud
cloudconfigserver
0.0.1-SNAPSHOT
jar
cloudconfigserver
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
Finchley.M8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-eureka
1.4.3.RELEASE
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
2. 修改配置文件application.properties:
spring.application.name=config-server #服务名称
server.port=8888 #服务端口
#https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.uri=https://github.com/fyjfengyujie/SpringRespo/ #远程git仓库
spring.cloud.config.server.git.search-paths= #远程仓库的目录,本例子配置文件在根目录中
spring.cloud.config.label=master #git分支名称
spring.cloud.config.server.git.username= #git仓库账号
spring.cloud.config.server.git.password= #git仓库密码
eureka.client.service-url.defaultZone=http://localhost:8889/eureka/ #eureka服务注册中心
3. 主程序入口添加@EnableConfigServer注解
config-client:
1. 添加项目依赖包:spring-cloud-starter-config, spring-cloud-starter-eureka, spring-retry, spring-cloud-starter-bus-amqp, spring-boot-starter-actautor, 想依赖pom.xml文件内容如下:
4.0.0
com.springcloud
config-client
0.0.1-SNAPSHOT
jar
config-client
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
Finchley.M8
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
1.5.9.RELEASE
org.springframework.cloud
spring-cloud-starter-eureka
1.4.3.RELEASE
org.springframework.retry
a
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
2. 编写配置文件application.properties:
spring.application.name=config-client #这里名称需要与子服务名称以及读取的配置文件库中文件名称一致
spring.cloud.config.label=master #git仓库分支名称
spring.cloud.config.profile=dev #开发文件,本例子中读取的配置文件为 config-client-dev.properties
spring.cloud.bus.refresh.enabled=true #开启总线消息更新功能
eureka.client.service-url.defaultZone=http://localhost:8889/eureka/ #eureka服务注册中心IP
eureka.cloud.config.discovery.enabled=true
eureka.cloud.config.discovery.serviceId=config-server #分布式服务注册服务ID名称
server.port=8881 #服务端口
#rabbitmq相关配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#重点:在spring boot 2.0版本中一定要修改此配置,可以选择【"health","mappings","bus-refresh"】三种选项暴露那些端点,
management.endpoints.web.exposure.include=*
3. 在application入口处添加@RefreshScope注解,注意无论spring boot 2.0 还是1.x都需要添加该注解,否则无法生效
@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${foo}") //读取的配置文件中的foo参数
String foo;
@RequestMapping(value = "/hi")
public String hi() {
return foo;
}
}