SpringBoot版本:1.5.16.RELEASE
之前查看了很多地方都没有成功,不是依赖少了就是配置错了,要不是版本对应不上,使用时要更注意,一下是最全面的,代码已经经过验证
一.服务端
1.依赖
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--config-auto-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
2.配置
#basic
spring.application.name=config-server
server.port=8100
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
#config
spring.cloud.config.server.git.uri=https://github.com/mianjuhou/SpringCloudDemo2
spring.cloud.config.server.git.search-paths=config-repo
spring.cloud.config.server.git.username=mianjuhou@163.com
spring.cloud.config.server.git.password=fdaMJH0318
spring.cloud.config.server.git.skip-ssl-validation=true
#rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#config-atuo
management.endpoints.web.exposure.include=bus-refresh
management.security.enabled=false
spring.cloud.bus.refresh.enabled=true
3.应用注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
二.客户端
1.依赖
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--config-auto-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
2.配置
bootstrap.properties
#basic
server.port=9000
spring.application.name=config-client
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
#config
spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.server.git.skip-ssl-validation=true
#config-atuo
management.endpoints.web.exposure.include=bus-refresh
management.security.enabled=false
application.properties
#rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3.应用注解
//无Config相关
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
4.代码
@RestController
@RefreshScope
public class ClientController {
@Value("${neo.hello}")
private String domain;
@RequestMapping("/domain/name")
public String getDomain() {
return this.domain;
}
}
三.github的webhok
使用的ngrok做内外网穿透
http://9b785d6c.ngrok.io/bus/refresh
content-type使用application/json