发表于: 2018-09-10 21:44:25

1 431


今天完成的事情:

今天继续学习springcloud的config配置:

config配置中心需要有一个server,

所以我们首先引入相应的依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>

接着对其进行配置:

spring:
 cloud:
   config:
     server:
       git:
         uri: https://github.com/akiakii/SpringConfigTest
default-label: master
application:
   name: config-server

server:
 port: 8769
management:
 endpoints:
   web:
     exposure:
       include: bus-refresh

其代表的是从github中项目主枝拉取配置,而且配置了bus-refresh。

接着启动类添加注解:

@SpringBootApplication
@EnableConfigServer
public class SpringcloudconfigApplication {

public static void main(String[] args) {
SpringApplication.run(SpringcloudconfigApplication.class, args);
  }
}

接着 是client:

<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>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
@RestController
@SpringBootApplication
@RefreshScope
public class ConfClinetApplication {

public static void main(String[] args) {
SpringApplication.run(ConfClinetApplication.class, args);
  }
@Value("${foo}")
String foo;
  @RequestMapping(value = "/")
public String hi(){
return foo;
  }
}

配置@RefreshScope表示可以根据bus-refresh在不重启项目下刷新获取配置。

这边使用了springcloud bus总线,消息队列使用的rabbitmq。

配置:

spring:
 application:
   name: config-client
cloud:
   config:
     uri: http://localhost:8769
fail-fast: true
profiles:
   active: dev
rabbitmq:
   host: localhost
port: 5672
password: guest
username: guest
server:
 port: 8762
management:
 endpoints:
   web:
     exposure:
       include: bus-refresh

启动之后eureka-server之后,再启动config-server和config-client,然后通过更改github上的配置,通过post请求到http://localhost:8762/actuator/bus-refresh就可以实时更新获取配置文件了。

一般在微服务中,我们会把config-server单独注册成一个服务,然后其他服务作为conf-client获取配置,其实gitee或者github也提供了webhook的方法可以获取配置,从而不用注册中心来配置文件。

明天计划的事情:

继续学习

遇到的问题:

springcloud2.x版本更改了很多东西,与1.5.x相比需要修改不少东西。

收获:

进度:




返回列表 返回列表
评论

    分享到