概述
参考网址:https://blog.csdn.net/forezp/article/details/81041028
基于数据库实现配置中心。
高可用的分布式配置中心,如果配置服务部署多份,通过负载均衡,从而高可用。
一、准备工作
安装MySql
启动前面的项目:erurekaserver
二、创建Maven项目 config-server
1、修改pom.xml
<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>wg</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config-server</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2、新建application.properties
spring.application.name=config-server
server.port=30000
spring.profiles.active=jdbc
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/wg?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.cloud.config.label=master
spring.cloud.config.server.jdbc=true
spring.cloud.config.server.jdbc.sql=SELECT P_KEY, P_VALUE from CONFIG_PROPERTIES where APPLICATION=? and P_PROFILE=? and LABEL=?
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka/
3、MySql创建数据库 wg ,创建表 CONFIG_PROPERTIES,初始化表数据
CREATE DATABASE IF NOT EXISTS wg DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
USE wg;
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for properties
-- ----------------------------
DROP TABLE IF EXISTS CONFIG_PROPERTIES;
CREATE TABLE CONFIG_PROPERTIES(
ID int(11) NOT NULL,
P_KEY varchar(50) NOT NULL,
p_VALUE varchar(500) NOT NULL,
APPLICATION varchar(50) NOT NULL,
P_PROFILE varchar(50) NOT NULL,
LABEL varchar(50) NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of properties
-- ----------------------------
INSERT INTO CONFIG_PROPERTIES VALUES ('1', 'title', 'i am config by jdbc', 'eurekaclient', 'dev', 'master');
INSERT INTO CONFIG_PROPERTIES VALUES ('2', 'name', 'wg', 'eurekaclient', 'dev', 'master');
INSERT INTO CONFIG_PROPERTIES VALUES ('3', 'sex', 'boy', 'eurekaclient', 'dev', 'master');
4、新建ConfigServerApplication.java
package wg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
5、启动项目,访问http://localhost:8761/
三、修改 eurekaclient1
1、新建bootstrap.yml,原来的配置文件全部删除。
server:
port: 8762
spring:
application:
name: eurekaclient
cloud:
config:
label: master
profile: dev
discovery:
enabled: true
service-id: config-server
eureka:
instance:
instance-id: eurekaclient1
appname: ${spring.application.name}
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2、修改pom.xml
<?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>wg</groupId>
<artifactId>eurekaclient1</artifactId>
<version>1.0-SNAPSHOT</version>
<name>eurekaclient1</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、修改EurekaClientApplication.java
package wg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wg.config.ServerConfig;
@SpringBootApplication
@RestController
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@Autowired
private ServerConfig serverConfig;
@Value("${server.port}")
String port;
@RequestMapping("/")
public String home() {
return "hello world from port " + port+",title:"+serverConfig.getTitle()+" name:"+serverConfig.getName()+" sex:"+serverConfig.getSex();
}
}
4、新建ServerConfig.java,放在EurekaClientApplication.java 下级包下。
package wg.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties//(prefix = "demo")
public class ServerConfig {
private String title;
private String sex;
private String name;
public String getSex() {
return sex;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5、启动项目,访问:http://localhost:8762/
上一篇:Spring Cloud 入门教程(五): 路由网关(zuul) (Greenwich.RELEASE)
下一篇:Spring Cloud 入门教程(七): 消息总线(Spring Cloud Bus)(Greenwich.RELEASE)
转载于:https://my.oschina.net/pipi1919/blog/3064833
最后
以上就是温暖服饰为你收集整理的Spring Cloud 入门教程(六): 分布式配置中心(Greenwich.RELEASE)的全部内容,希望文章能够帮你解决Spring Cloud 入门教程(六): 分布式配置中心(Greenwich.RELEASE)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复