Spring Cloud Config与Eureka配合使用

一 介绍
在微服务中指定Config Server地址,这种方式无法利用服务发现组件的优势。
本篇将讨论Config Server注册到Eureka Server上时,如何使用Spring Cloud config。
二 新建项目microservice-config-server-eureka
1 启动类代码
package com.itmuch.cloud.study;

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;

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}
2 application.yml
server:
  port: 8080
spring:
  application:
    name: microservice-config-server-eureka
  cloud:
    config:
      server:
        git:
          uri:https://git.oschina.net/itmuch/spring-cloud-config-repo# 配置Git仓库的地址
          username:                                                         # Git仓库的账号
          password:                                                         # Git仓库的密码
eureka:
  client:
    serviceUrl:
      defaultZone:http://localhost:8761/eureka/
3 pom依赖
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
  </dependencies>
三 新建项目microservice-config-client-eureka
1 启动类
package com.itmuch.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigClientApplication.class, args);
  }
}
2 application.yml
server:
  port: 8081
3 bootstrap.yml
spring:
  application:
    name: microservice-foo    # 对应config server所获取的配置文件的{application}
  cloud:
    config:
      profile: dev
      label: master
      discovery:
        enabled: true                                  # 表示使用服务发现组件中的Config Server,而不自己指定Config Server的uri,默认false
        service-id: microservice-config-server-eureka  # 指定Config Server在服务发现中的serviceId,默认是configserver
eureka:
  client:
    serviceUrl:
      defaultZone:http://localhost:8761/eureka/
4 pom
  <dependencies>
    <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>
  </dependencies>
四 测试
1 启动Eureka
2 启动项目microservice-config-server-eureka
3 启动项目microservice-config-client-eureka
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值