SpringCloud (9) ---- mysql achieve Configuration Center

The company management is carried out by the configuration data mysql configuration management, because already be in place, so re-build their own hands again, familiar with the whole process. Late-related projects on github source will complement address

Micro-micro-management services for centralized configuration service, different environments different configurations can also be dynamically adjusted during operation, the demand can be updated automatically after configuration changes, Spring Cloud Config while meeting the above requirements.

 GitHub project code Address : https://github.com/yudiandemingzi/spring-cloud-study

A project to build

The service is mainly used three micro

(1) Eureka-server: 7001 Registration Authority

(2) config-server: 5001 Configuration Center

(3) product-server: 8001 Goods Micro Services

1, Eureka-server registry

Registry is very simple, some will not be repeated here, registry did not change. Before you can see the blog wrote: SpringCloud (3) --- Eureka service registration and discovery

2, Micro Center Configuration Service

    1, pom.xml

Copy the code
<!--服务中心jar包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!--配置中心jar包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

<!--连接msql数据库相关jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
Copy the code

   2, application.yml

Copy the code
#服务名称
 server:
   port: 5001

#连接配置信息
 spring:
   application:
     name: config-server-jdbc
   profiles:
     active: jdbc
   cloud:
     config:
       server:
         default-label: dev
         jdbc:
           sql: SELECT akey , avalue FROM config_server where APPLICATION=? and APROFILE=? and LABEL=?
 #####################################################################################################
 # mysql 属性配置
   datasource:
     driver-class-name: com.mysql.jdbc.Driver
     url: jdbc:mysql://127.0.0.1:3306/test
     username: root
     password: root
 ################################################## ################################################## # 

# specify the center address 
 Eureka: 
   Client: 
     serviceUrl: 
       defaultzone: HTTP: // localhost: 7001 / Eureka /
Copy the code

Under the main speaker connection configuration information here

(1) spring.profiles.active = jdbc, automatically JdbcEnvironmentRepository.

(2) sql statement custom, otherwise it will default to "SELECT KEY, VALUE from PROPERTIES where APPLICATION =? And PROFILE =? And LABEL =?", Specific reference JdbcEnvironmentRepository achieved.

(3) I built a database table config_server, due to the key, value and profile are mysql keyword, so I have added in front of a. Of course, the table name field name can be customized.

(4) {application} corresponding to the client "spring.application.name" attribute;

         {Aprofile} corresponding to the client "spring.profiles.active" attribute (comma-separated list); and

          {Label} corresponding to the server attribute, the version can be designated a set of configuration files.

(5) select long as it is two fields, the frame will be automatically packaged into the environment map <key, value>.

       3, mysql data

   4, springboot startup class

Add comment @EnableConfigServer

Copy the code
@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {

public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
    }
}
Copy the code

 3, product-service micro services

      1, pom.xml 

Copy the code
        <!--服务中心jar-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        
        <!--配置中心客户端jar-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
Copy the code

      2, bootstrap.yml

Copy the code
# Designated registration center address 
Eureka: 
  Client: 
    serviceUrl: 
      defaultzone: HTTP: // localhost: 7001 / Eureka / 

# service name 
the Spring: 
  the Application: 
    name: Product-Service 
  # specify which configuration is read from the center 
  Cloud: 
    config: 
      Discovery: 
        -ID-Service: Server-config-JDBC 
        Enabled: to true 
      Profile: dev 
      label: dev 

Server: 
  Port: 8001
Copy the code

Why bootstrap.yml here instead application.yml, is because if application.yml and bootStrap.yml in the same directory,

The load order bootStrap.yml higher than application.yml, namely bootStrap.yml priority will be loaded.

Why do I need to config server information placed bootstrap.yml in?

When using Spring Cloud, configuration information is generally loaded from config server, in order to obtain configuration information (such as passwords, etc.), or you need some early boot configuration.

Therefore, the information on the config server bootstrap.yml, used to load configuration information you really need.

     3, ConfigController type (test)

Copy the code
@RestController
@RequestMapping("/api/v1/product")
public class ConfigController {

    @Value("${item_url}")
    private String url;

    /**
     * 输出url
     */
    @RequestMapping("url")
    public void list(){

        System.out.println(url);
    }
Copy the code

       4, the test

By visiting: http: // localhost: 8001 / api / v1 / product / url into the break.

 

 

I only occasionally to quiet down, past all the wondering about it. Those who have in the old days there have been naive, even stupid, not worth condemnation. After all, the next day, still very long. Continue to encourage their own,

At daybreak, but also a new starting point, but also the unknown journey (Col. 11)

 

Reprinted to: https://www.cnblogs.com/qdhxhz/p/9624386.html

 

Guess you like

Origin www.cnblogs.com/wllcs/p/12002892.html
Recommended