springboot整合eureka、config搭建注册中心和配置中心

这篇文章详细介绍怎么通过eureka和config分别搭建一个注册中心和配置中心的服务。


目录

一 、整合eureka搭建注册中心

1、创建项目

2、添加依赖

3、修改配置

4、添加注解

5、启动项目

6、访问项目

二、整合config搭建配置中心

1、创建项目

2、添加依赖

3、修改配置

4、添加注解

5、启动项目

三、创建客户端项目

1、创建项目

2、添加依赖

3、修改配置

4、添加注解

5、创建仓库

6、启动项目


一 、整合eureka搭建注册中心

1、创建项目

在IntelliJ IDEA中创建一个springboot项目eureka-server

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>cn.edu.sgu.www</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Spring Boot整合Eureka搭建注册中心</description>

    <properties>
        <java.version>1.8</java.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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3、修改配置

将配置文件application.properties重命名为application.yml,修改配置文件的内容。

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、添加注解

重命名启动类,删除启动类的Application后缀,在启动类上添加@EnableEurekaServer注解。

package cn.edu.sgu.www.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;

/**
 * @author heyunlin
 * @version 1.0
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServer {

    static final Logger logger = LoggerFactory.getLogger(EurekaServer.class);

    public static void main(String[] args) {
        if (logger.isDebugEnabled()) {
            logger.debug("启动eureka注册中心......");
        }
        
        SpringApplication.run(EurekaServer.class, args);
    }

}

5、启动项目

运行启动类EurekaServer的main方法启动当前项目。

6、访问项目

浏览器访问localhost:8761,如果能看到下面的界面,说明eureka配置完成了

二、整合config搭建配置中心

1、创建项目

在IntelliJ IDEA中创建一个springboot项目config-center

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-center</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-center</name>
    <description>Spring Boot整合config搭建配置中心</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3、修改配置

将配置文件application.properties重命名为application.yml。

修改配置文件的内容,添加配置中心和注册到euraka的配置。

server:
  port: 8888

# 注册到eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        # 远程git仓库
        git:
          search-paths: config  # 配置文件所在根目录
          uri: https://gitee.com/muyu-chengfeng/repository.git # git仓库地址

4、添加注解

在启动类上添加@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;

/**
 * @author heyunlin
 * @version 1.0
 */
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigCenter {

    static final Logger logger = LoggerFactory.getLogger(ConfigCenter.class);

    public static void main(String[] args) {
        if (logger.isDebugEnabled()) {
            logger.debug("启动config-center配置中心...");
        }
        
        SpringApplication.run(ConfigCenter.class, args);
    }

}

5、启动项目

运行启动类EurekaServer的main方法启动当前项目。

浏览器访问localhost:8761,查看服务config-server是否注册到了eureka

三、创建客户端项目

经过前面的两步,已经搭建好了配置中心,并且能够正确的注册到注册中心,接下来创建一个服务,从配置中心拉取配置。


1、创建项目

在IntelliJ IDEA中创建一个springboot项目eureka-client

2、添加依赖

<?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>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client</name>
    <description>Spring Boot整合eureka-client客户端项目</description>

    <properties>
        <java.version>1.8</java.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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3、修改配置

将配置文件application.properties重命名为application.yml。

修改配置文件的内容,添加配置中心和注册到euraka的配置。

spring:
  application:
    name: eureka-client
  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  #配置中心的服务名
      uri: http://localhost:8888   #配置中心的地址

# 注册到eureka注册中心
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

server:
  port: 9001

4、添加注解

在启动类上添加@EnableEurekaClient注解。

package cn.edu.sgu.www.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.EnableEurekaClient;

/**
 * @author heyunlin
 * @version 1.0
 */
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

    static final Logger logger = LoggerFactory.getLogger(EurekaClientApplication.class);

    public static void main(String[] args) {
        if (logger.isDebugEnabled()) {
            logger.debug("启动eureka客户端项目...");
        }

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

}

5、创建仓库

创建一个git仓库,取名为repository,新建一个config包,在config包下新建一个eureka-client-dev.yml,添加一些配置。

server:
  port: 8085

user:
  name: 沐雨橙风ιε
  email: he*****@163.com

6、启动项目

如果最后项目启动的端口号不是9001,而是8085,说明成功拉取到了配置。


好了,文章就分享到这里了,看完不要忘了点赞+收藏哦,文章涉及的代码已开源,可按需获取~

Spring Boot整合Eureka搭建注册中心案例项目icon-default.png?t=O83Ahttps://gitee.com/muyu-chengfeng/eureka-server.gitSpring Boot整合config搭建配置中心案例项目icon-default.png?t=O83Ahttps://gitee.com/muyu-chengfeng/config-center.gitSpring Boot整合eureka-client客户端项目icon-default.png?t=O83Ahttps://gitee.com/muyu-chengfeng/eureka-client.git


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐雨橙风ιε

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值