-
sb-cloud-config 配置中心
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huarui</groupId> <artifactId>sb_cloud_config</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sb_cloud_config</name> <packaging>jar</packaging> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR3</spring-cloud.version> </properties> <dependencies> <!-- 消息总线支持 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <!-- eureka client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- config依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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>
server: port: 9002 spring: application: name: sb-cloud-config cloud: config: server: git: uri: https://github.com/youxiu326/sb_cloud_config_repo username: youxiu326 password: 123456 basedir: D:sortwareIDEApackagesb_springcloud_packageasedir #searchPaths: spring-cloud/helloworldConfig #RebbitMq rabbitmq: host: 39.108.85.204 port: 5672 username: guest password: guest eureka: client: serviceUrl: defaultZone: http://39.108.85.204:8761/eureka/ instance: #注册时使用ip而不是主机名 prefer-ip-address: true #指定此实例的ip ip-address: 39.108.85.204 # 博客: https://www.cnblogs.com/moonandstar08/p/7571610.html # 关闭安全验证 management: security: enabled: false
注: 避免通过服务名无法发现服务
eureka: client: serviceUrl: defaultZone: http://39.108.85.204:8761/eureka/ instance: #注册时使用ip而不是主机名 prefer-ip-address: true #指定此实例的ip ip-address: 39.108.85.204 多网卡环境下Eureka服务注册IP 不正确,部署服务时手动指定ip,避免该服务无法被发现
@SpringBootApplication @EnableEurekaClient //@EnableEurekaClient将它注册到服务中心 @EnableConfigServer //@EnableConfigServer注解说明了一个Config Server public class SbCloudConfigApplication { //http://localhost:9002/application-dev.yml public static void main(String[] args) { SpringApplication.run(SbCloudConfigApplication.class, args); } }
-
sb_cloud_config_client
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huarui</groupId> <artifactId>sb_cloud_config_client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sb_cloud_config_client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR3</spring-cloud.version> </properties> <dependencies> <!-- 消息总线支持 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <!-- eureka client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- config依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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>
spring: application: name: sb_cloud_config_client cloud: config: discovery: enabled: true service-id: SB-CLOUD-CONFIG profile: dev #uri: http://localhost:9002/ # #RebbitMq rabbitmq: host: 39.108.85.204 port: 5672 username: guest password: guest #management: # security: # enabled: false #刷新时,关闭安全验证 eureka: client: serviceUrl: defaultZone: http://39.108.85.204:8761/eureka/ instance: #注册时使用ip而不是主机名 prefer-ip-address: true #指定此实例的ip #ip-address: ip server: port: 9003
bootstrap.yml 先于 application.yml 读取
@SpringBootApplication @EnableEurekaClient public class SbCloudConfigClientApplication { public static void main(String[] args) { SpringApplication.run(SbCloudConfigClientApplication.class, args); } // post http://localhost:9003//bus/refresh }
@RestController @RefreshScope public class HelloController { @Value("${lihui}") private String lihui; @GetMapping("/hello") public String hello(){ return lihui; } }
github:结构
- 依次启动 eureka server, config, config-client
- 访问config-client http://localhost:9003/hello
- 可以发现config-client中的配置文件信息于config服务中获得
- 修改github上的配置文件,刷新页面,并无改变
- post 方式访问 http://39.108.85.204:9002/bus/refresh
刷新页面,值已改变
- 每次修改github配置文件后,每次自己访问也是非常不方便的,可在github中配置webHook 每次提交手动发送一个请求
- 这样每次提交文件时,就会向rabbitMQ队列中发消息,所有收到消息的就会更新 带有 @RefreshScope 注解的类