Spring Cloud微服务架构(十一)分布式配置中心(spring cloud config)客户端 config client远程读取数据库配置文件

1、构建Config client

Spring Cloud微服务架构(十一)分布式配置中心(spring cloud config)客户端 config client远程读取数据库配置文件_第1张图片

创建完pom文件修改如下



         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    com.vesus
    springcloud-config
    0.0.1-SNAPSHOT
    jar

    springcloud-config
    Demo project for Spring Boot

    
        com.vesus
        springcloud-demo
        0.0.1-SNAPSHOT
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
        
            mysql
            mysql-connector-java
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2、创建bootstrap.yml配置文件

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ # 注册中心地址
spring:
  application:
    name: springcloud-config-client
  cloud:
    config:
      name: springcloud-config-server #配置文件
      profile: dev
      label: master #配置文件所在分支
      discovery:
        enabled: true #开启配置服务发现
        service-id: springcloud-config-server #配置服务实例名称
      uri: http://localhost:8770/ #配置服务中心
server:
  port: 8771

4、入口方法。

package com.vesus.springcloudconfig;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class SpringcloudConfigApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringcloudConfigApplication.class, args);
    }

}

5、修改远程仓库数据源文件springcloud-config-server-dev.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

6、配置数据源,读取远程数据源配置文件

package com.vesus.springcloudconfig.config;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource(){
        return DataSourceBuilder.create().build() ;
    }
}

7、增加UserController

package com.vesus.springcloudconfig.controller;

import com.vesus.springcloudconfig.model.User;
import com.vesus.springcloudconfig.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserService userService ;

    @Value("${jdbc.username}")
    String username;

    @RequestMapping(value = "/name")
    public String username(){
        return username;
    }

    @RequestMapping(value = "/userlist")
    public List getUserList(){
        return userService.findAll() ;
    }
}

8、访问 http://localhost:8771/userlist

[{"id":1,"name":"admin","age":21,"address":"111","password":"123456"}]

源码:https://gitee.com/vesus198/springcloud-demo/tree/master/springcloud-config

你可能感兴趣的:(spring,cloud)