Config remote configuration file of springCloud series (6)

In microservice projects, there are often a lot of configuration files. Too many configuration files are not conducive to later modification. In order to reduce the processing of configuration files, the config module in springCloud will be used for development and processing.

Config is divided into server and client. We put commonly used configurations on remote tools (git, SVN, code cloud), and then the Config server downloads the required configurations from the remote tools, and then the client obtains these configurations. Last use configuration

Simple to use

1. Write the configuration file config-dev.yml on Code Cloud . It is actually an ordinary configuration file. It is best to add - to the configuration file name, which will facilitate the writing of the configuration file later.

server:
  port: 8085
spring:
  application:
    name: user-provider
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false
    password: root
    username: root
mybatis:
  type-aliases-package: com.lihao
  mapper-locations: classes:mapper/*Mapper.xml
eureka:
  client:
    service-url:
      defaultZone: http://root:123456@localhost:7776/eureka/

2. Write the config-server project to download the corresponding configuration from the code cloud

First, add new dependencies based on the original springCloud project dependencies.

<!-- config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

Add configuration file application.yml

server:
  port: 7000
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri:  https://gitee.com/lihao2/config-server.git
eureka:
  client:
    service-url:
      defaultZone: http://root:123456@localhost:7776/eureka/

Add the @EnableConfigServer annotation to the startup class, and finally run the startup class

After starting, visit http://localhost:7000/config-dev.yml and you can see the content of the configuration file you wrote, indicating success.

3. Write config-client

The client here actually refers to the producer or consumer. We can add dependencies directly on the producer side. First, add maven dependencies under user-provider.

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

Create the bootstrap.yml file in the resources directory. The bootstrap.yml function is similar to application.yml. The difference is that it is executed before application.yml.

注意name的值和profile的值就是在Git中配置文件的名称
我的配置文件名是config-dev,所以下面的name值是config,profile的值是dev

spring:
  application:
    name: config
  cloud:
    config:
      uri: http://localhost:7000/
      profile: dev
      

Manual refresh

At present, the use of config has ended, but in actual programming sometimes it is necessary to directly change the configuration of the remote file. However, it cannot be used directly at this time and the service needs to be restarted. So can it be used directly without restarting the project? The following configuration talks about completing the project without restarting, directly refreshing the configuration manually .

In order to conveniently display the manual refresh effect, first we add the profile attribute on the remote gitee. It does not matter what value it is. It is mainly used to show the effect of refreshing the configuration.

profile: one

Add the jar package to config-client, which is the producer or consumer of the service. In this project, it is user-provider.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Add the following settings in the configuration file bootstrap.yml to cancel springBoot verification

management:
  security:
    enabled: false

Finally, the controller class

@RestController
@RefreshScope
public class UserService {

	// 获取配置中的profile值,并注入
    @Value("${profile}")
    private String profile;

    @RequestMapping("t")
    public String test(){
        System.out.println(profile);
        return "success";
    }
}

then test

First start everything, then modify the value of the profile in the remote, and then access the /refresh path on the producer side. Note that the access method is POST. At this time, the manual refresh is completed. You can see it in the print log on the producer side. What is printed is The latest one is worth it

Guess you like

Origin blog.csdn.net/lihao1107156171/article/details/114296560