目录
一 、springboot整合eureka实现注册中心
二、springboot整合config实现配置中心
三、从配置中心拉取配置
这篇文章详细介绍怎么通过eureka和config分别搭建一个注册中心和配置中心的服务。
一 、springboot整合eureka实现注册中心
1、创建一个springboot项目,并命名为eureka
2、修改pom.xml,添加eureka-server的依赖
因为博主之前整合springcloud的springboot版本用的都是2.3.2.RELEASE,而springboot对应的springcloud版本为Hoxton.SR
<?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.2.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: 9000eureka:instance:hostname: localhost # eureka所在的服务器名client:fetch-registry: falseregister-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 EurekaApplication {private final static Logger logger = LoggerFactory.getLogger(EurekaApplication.class);public static void main(String[] args) {if (logger.isDebugEnabled()) {logger.debug("启动eureka注册中心......");}SpringApplication.run(EurekaApplication.class, args);}}
5、启动项目
6、浏览器访问localhost:9000,如果能看到下面的界面,说明eureka配置完成了
二、springboot整合config实现配置中心
1、创建一个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.2.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-server</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-server</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:9000/eurekaspring:application:name: config-servercloud: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 ConfigApplication {private final static Logger logger = LoggerFactory.getLogger(ConfigApplication.class);public static void main(String[] args) {if (logger.isDebugEnabled()) {logger.debug("启动config配置中心......");}SpringApplication.run(ConfigApplication.class, args);}}
5、启动项目
6、浏览器访问localhost:9000,查看服务config是否注册到了eureka上
三、从配置中心拉取配置
经过前面的两步,已经搭建好了配置中心,并且能够正确的注册到注册中心,接下来创建一个服务,从配置中心拉取配置。
1、创建一个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.2.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: 9001spring:application:name: eureka-clientprofiles:active: devcloud:config:profile: dev # 不加此属性直接获取eureka-client.yml,加了后符合config的名字规则eureka-client-dev.ymlenabled: truename: eureka-client # 配置中心Git仓库config文件夹里的文件名字label: master # 默认分支masterfail-fast: true # 是否启动快速失败功能,功能开启则优先判断config server是否正常discovery:enabled: trueservice-id: config-server #spring cloud 配置中心服务名eureka:instance:hostname: eurekaclient:service-url:defaultZone: http://localhost:9000/eureka
4、创建一个git仓库,取名为repository,新建一个config包,在config包下新建一个eureka-client-dev.yml,配置项目的启动端口号
server:port: 9002
5、启动项目
如果最后项目启动的端口号不是9001,而是9002,说明成功拉取到了配置。
总结:通过手动配置了一下eureka和config,发现一个问题,spring cloud config的uri不能通过配置文件更改,具体原因不详,可能是版本的bug
好了,文章就分享到这里了,接下来将要学习consul