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依赖
  
    
      org.springframework.cloud
      spring-cloud-config-server
    
    
      org.springframework.cloud
      spring-cloud-starter-eureka
    
  
三 新建项目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
  
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.cloud
      spring-cloud-starter-config
    
    
      org.springframework.cloud
      spring-cloud-starter-eureka
    
  
四 测试
1 启动Eureka
2 启动项目microservice-config-server-eureka
3 启动项目microservice-config-client-eureka
4 输入 http://localhost:8761/
Spring Cloud Config与Eureka配合使用_第1张图片

你可能感兴趣的:(微服务)