Spring Cloud微服务架构(十一)分布式配置中心(spring cloud config)客户端 config client远程读取数据库配置文件
1、构建Config client
创建完pom文件修改如下
<?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.vesus</groupId> <artifactId>springcloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-config</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.vesus</groupId> <artifactId>springcloud-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- eureka:微服务注册 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!--config client :--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--spring jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
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<User> 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