SpringCloud 入门搭建配置中心

1、创建maven项目,添加依赖包

<!-- eureka 注册中心客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- config 配置中心服务端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 服务健康状态监控 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 消息总线 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2、修改或添加application.yml(或者bootstrap.yml)文件:

server:
  port: 8900

spring:
  application:
    name: moli-config
  cloud:
    config:
      server:
        git:
          uri: 您的git仓库地址
          default-label: 您的git仓库分支
          search-paths: /
          clone-on-start: true
          force-pull: true
          username: 访问git仓库的用户名或邮箱
          password: 访问git仓库的密码
  # 消息队列
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest

eureka:
  instance:
    prefer-ip-address: true
    ip-address: 127.0.0.1
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
  client:
    service-url:
      defaultZone: http://${eureka.instance.ip-address}:8800/eureka/

# 配置中心加解密配置的签名证书
# 证书生成方式参考另一篇博文https://blog.molicloud.com/85.html
encrypt:
  key-store:
    location: classpath:/xxxxx.jks
    alias: xxxxx
    password: xxxxx
    secret: xxxxx

management:
  endpoints:
    web:
      exposure:
        include: '*'

3、编写配置中心启动类:

package com.molicloud.moli.config;

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 feitao <yyimba@qq.com> 2019/6/18 17:49
 */
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class MoliConfigApplication {

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

项目工程截图(请忽略我的config目录):

茉莉配置中心-模块截图

未经允许不得转载:小茉莉 » SpringCloud 入门搭建配置中心

赞 (1) 打赏

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下小茉莉

支付宝扫一扫打赏

微信扫一扫打赏