这篇文章详细介绍怎么通过eureka和config分别搭建一个注册中心和配置中心的服务。
目录
一 、springboot整合eureka搭建注册中心
二、springboot整合config搭建配置中心
三、从配置中心拉取配置
一 、springboot整合eureka搭建注册中心
1、在IntelliJ IDEA中创建一个springboot项目,并命名为eureka
2、修改pom.xml,添加eureka-server的依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<lombok.version>1.18.22</lombok.version>
<eureka.version>1.4.4.RELEASE</eureka.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>${eureka.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、修改配置文件
server:
port: 8761
eureka:
instance:
hostname: localhost # eureka所在的服务器名
client:
fetch-registry: false
register-with-eureka: false
# eureka提供服务的地址
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
4、启动类上添加注解@EnableEurekaServer
package com.example.eureka;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Eureka {
static final Logger logger = LoggerFactory.getLogger(Eureka.class);
public static void main(String[] args) {
if (logger.isDebugEnabled()) {
logger.debug("启动eureka注册中心......");
}
SpringApplication.run(EurekaApplication.class, args);
}
}
5、启动项目
6、浏览器访问localhost:8761,如果能看到下面的界面,说明eureka配置完成了
二、springboot整合config搭建配置中心
1、在IntelliJ IDEA中创建一个springboot项目,并命名为config
2、修改pom.xml,添加config-server的依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>cn.edu.sgu.www</groupId>
<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<eureka.version>1.4.4.RELEASE</eureka.version>
<config.version>2.2.8.RELEASE</config.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>${eureka.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>${config.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4、修改配置文件,注册到euraka并添加配置中心的配置
这里的配置文件可以存到当前项目下,也可以存到git仓库,还能保存到远程的服务器。
server:
port: 8888
# 注册到eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: config-server
cloud:
config:
server:
# 本地
# native:
# searchLocations: classpath:/config #本地配置的路径 读取/resources/config下的配置
# 远程git仓库
git:
search-paths: config # 配置文件所在根目录
uri: https://gitee.com/he-yunlin/repository.git # git仓库地址
5、启动类上添加注解@EnableConfigServer和@EnableEurekaClient
package cn.edu.sgu.www.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class Config {
static final Logger logger = LoggerFactory.getLogger(Config.class);
public static void main(String[] args) {
if (logger.isDebugEnabled()) {
logger.debug("启动config配置中心...");
}
SpringApplication.run(ConfigApplication.class, args);
}
}
5、启动项目
6、浏览器访问localhost:8761,查看服务config是否注册到了eureka上
三、从配置中心拉取配置
经过前面的两步,已经搭建好了配置中心,并且能够正确的注册到注册中心,接下来创建一个服务,从配置中心拉取配置。
1、在IntelliJ IDEA中创建一个springboot项目eureka-client
2、添加maven依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>euraka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<eureka.version>1.4.4.RELEASE</eureka.version>
<config.version>2.2.8.RELEASE</config.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>${eureka.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>${config.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、修改配置文件
server:
port: 9001
spring:
application:
name: eureka-client
profiles:
active: dev
cloud:
config:
profile: dev # 不加此属性直接获取eureka-client.yml,加了后符合config的名字规则eureka-client-dev.yml
enabled: true
name: eureka-client # 配置中心Git仓库config文件夹里的文件名字
label: master # 默认分支master
fail-fast: true # 是否启动快速失败功能,功能开启则优先判断config server是否正常
discovery:
enabled: true
service-id: config-server #spring cloud 配置中心服务名
eureka:
instance:
hostname: eureka
client:
service-url:
defaultZone: http://localhost:8761/eureka
4、创建一个git仓库,取名为repository,新建一个config包,在config包下新建一个eureka-client-dev.yml,配置项目的启动端口号
server:
port: 9002
5、启动项目
如果最后项目启动的端口号不是9001,而是9002,说明成功拉取到了配置。
好了,文章就分享到这里了,看完不要忘了点赞+收藏哦,文章涉及的代码已开源,可按需获取~文章来源:https://www.toymoban.com/news/detail-572480.html
spring cloud netflix eureka微服务注册中心https://gitee.com/he-yunlin/eureka.gitspring cloud config微服务配置中心https://gitee.com/he-yunlin/config.giteureka客户端项目,从配置中心拉取配置https://gitee.com/he-yunlin/eureka-client.git文章来源地址https://www.toymoban.com/news/detail-572480.html
到了这里,关于springboot整合eureka、config搭建注册中心和配置中心的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!