spring cloud config搭建说明例子(二)-添加eureka

本文介绍如何在Spring Cloud项目中集成Eureka服务发现和Config Server配置中心,包括必要的Maven依赖、配置文件设置及注解使用,并区分了不同组件的功能差异。

添加注册eureka

服务端 ConfigServer

pom.xml

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
                <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

spring-cloud-starter-eureka与spring-cloud-starter-eureka-server的区别?

添加EnableDiscoveryClient

@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

@EnableDiscoveryClient与@EnableEurekaClient的区别?

application.yml

server:
   port: ${PORT:8888}                                #配置工程端口号

spring:
   application:
      name: cloud-config-server                     #设置该服务应用名称
   profiles:
      active: native                                 #设置读取为本地工程文件
   config:
      server:
         native:
            searchLocations: classpath:/config       #配置文件根目录,也就是XXX-dev.properties等的目录

#注册到eureka服务中心进行监控
eureka:
   client:
      serviceUrl:
          defaultZone: http://eureka:eureka@localhost:8761/eureka # 可以逗号分隔,配置多个

官网也用发发发发这个端口。

配置文件

新建配置文件:
XXX-dev.properties
XXX-test.properties

客户端AppClient

pom.xml也需要增加eureka

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
        <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

bootstrap.properties配置

spring.cloud.config.name=XXX
spring.cloud.config.profile=dev
#spring.cloud.config.profile=test
spring.cloud.config.uri=http\://localhost\:8888/ # 只能配置一个,不能逗号分隔配置多个config

application.yml配置,和config一样增加eureka

server:
   port: 8080                                  #设置当前服务端口
   context-path: /abc                          #设置服务上下文路径

spring:
   application:
      name: app-client                  #service name 设置当前服务名称

eureka:
   client:
      serviceUrl:
          defaultZone: http://eureka:eureka@localhost:8761/eureka   # 可以逗号分隔,配置多个

application类增加@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ClientApplication.class, args);
    }
}

参考资料:
spring cloud config搭建说明例子(一)-简单示例

转载于:https://www.cnblogs.com/ouyida3/p/9124680.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值