4、配置中心-服务端
目标
- 建config-server
- 修改pom.xml
- 安装rabbitmq(先装windows版,后续部署改为linux版)
- mysql下建properties表
- 配置文件修改
配置中心默认支持的配置文件类型:git/svn jdbc filesystem redis等
我们选用的是数据库
建config-server
拷贝eureka-service 更名为config-server
修改后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>cloudPlatform</groupId>
<artifactId>configService</artifactId>
<name>configServicer</name>
<version>1.0</version>
<description />
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!-- 后续管理界面开发用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 监控用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- config-server向eureka-server注册
各个service通过eureka-server来发现配置中心
这是一种方案 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 通过bus通知各服务配置有更改,后续各服务通过eureka-server找到 config-server,进而与config-server同步,更新配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- 连接mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- 如果是phpstudy需要指定一下版本(低版本) -->
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>http://49.4.22.210:8081/repository/maven-public</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>http://49.4.22.210:8081/repository/maven-public</url>
</pluginRepository>
</pluginRepositories>
<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>
安装rabbitmq(先装windows版,后续部署改为linux版)
安装文件位置:
E:\工作文件\springcloud-new\rabbitmq
先安装:otp_win64_22.2.exe再安装rabbitmq-server-3.8.2.exe
1)otp_win64_22.2.exe只安装development即可,其余选项默认
image.png
2)安装rabbitmq-server 一路默认即可
双击C:\Program Files\RabbitMQ Server\rabbitmq_server-3.8.2\sbin\rabbitmq-service.bat启动
进入C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.5\sbin下输入以下命令安装插件
rabbitmq-plugins.bat enable rabbitmq_management
服务启动命令:
rabbitmq-service.bat start
服务关闭命令:
rabbitmq-service.bat stop
访问http://localhost:15672/,默认用户名:guest,密码:guest进入控制台!
建用户和赋权限为可选,参考以下网址-略
https://www.cnblogs.com/lykbk/p/erewererewr32434343.html
输入正确的用户名密码,即可进入管理界面
image.png
mysql下建properties表
/*
Navicat MySQL Data Transfer
Source Server : mysql
Source Server Version : 50553
Source Host : localhost:3306
Source Database : java16
Target Server Type : MYSQL
Target Server Version : 50553
File Encoding : 65001
Date: 2020-02-02 17:06:01
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for properties
-- ----------------------------
DROP TABLE IF EXISTS `properties`;
CREATE TABLE `properties` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`keyword` varchar(255) DEFAULT NULL,
`valueword` varchar(255) DEFAULT NULL,
`APPLICATION` varchar(255) DEFAULT NULL,
`PROFILE` varchar(255) DEFAULT NULL,
`LABEL` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS=1;
配置文件修改application.yml
#springboot基本配置
spring:
#mq 此处为新增
rabbitmq:
host: 127.0.0.1
password: guest
port: 5672
username: guest
management:
endpoint:
health:
showDetails: ALWAYS
gateway:
enabled: true
endpoints:
web:
base-path: /actuator
exposure:
include: "*"
bootstrap.yml
#springboot基本配置
spring:
application:
name: config-server
#jdbc对应的查询语句 此处为新增
cloud:
config:
server:
jdbc:
sql: SELECT keyword,valueword from properties where APPLICATION=? and PROFILE=? and LABEL=?
prefix: /config
#config类型 此处为新增
profiles:
active: jdbc
datasource:
hikari:
maximumPoolSize: 20
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/java16?characterEncoding=UTF-8&useUnicode=true
username: root
password: root
server:
port: 9002
eureka:
instance:
instanceId: ${eureka.instance.ipAddress}:${spring.application.name}:${server.port}
ipAddress: 127.0.0.1
#是否优先使用ip地址
preferIpAddress: true
#用上述三行替代localhost,改为用ip地址访问
#hostname: localhost
client:
#是否在eureka服务器上注册自己的信息以供其他服务发现 (server端为单机时false,server端有负载时为true client端为true)
registerWithEureka: true
#是否获取eureka服务器注册表上的注册信息 server端为false,client端如果需要访问其他服务为true
fetchRegistry: true
#此处为eureka-server服务地址,即指定向谁注册
serviceUrl:
defaultZone: http://127.0.0.1:8090/eureka
启动类
一定注意@EnableConfigServer注解,之前因为没加这个注解,启动后其他服务无法找到他,调试了好长时间,踩过的坑啊
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
启动服务
注意一定先启动eureka-server再启动配置中心
目前可以只启动eureka-server和 config-server
简单看一下效果
image.png image.png
bus服务算是搭建成功了,接下来需要连接BUS的那些服务需要做更改
哪些服务需要连接,取决于你的架构设计。
我来个简单的,先只有service1-n 连接