springboot实现gitee配置中心搭建以及访问

版权声明:原创博文,转载请注明出处~ https://blog.csdn.net/She_lock/article/details/84341973

需求

场景:由于现在大多都是微服务架构,项目中,我们会配置一些信息,有的是敏感的信息,如果都写在代码里,所有的开发人员都是透明的,这样在一定层度上还是存在风险的。另外,随着业务的不断拓展,这种微服务不断增加,配置信息分散在每个微服务项目中,维护极度不方便,所以,拥有一个独立于项目的微服务配置中心变得越发重要。

需求:配置文件即各种properties或者yml文件,全部放置在gitee上,然后通过一个配置中心连接gitee统一来读取所有配置文件信息到内存当中。然后,所有的其他项目的配置文件,都从这个配置中心获取。

创建gitee

如图,创建两个配置文件,分别表示pj-test 的开发环境dev和生成环境prd。里面可以写上相应的配置信息,例如 ,pj-test-dev.yml文件中:

server:
  port: 1112
someboy:
  say: world

有了这个,接下来就是搭建配置中心服务了。

配置中心搭建

  1. 添加依赖,配置中心依赖属于spring cloud范畴,所以要多继承相关依赖:
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.12.RELEASE</version>
	</parent>

	<properties>
		<spring-cloud.version>Edgware.SR4</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<!--配置中心服务-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
	</dependencies>

	<!--多继承-->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>io.spring.platform</groupId>
				<artifactId>platform-bom</artifactId>
				<version>Brussels-SR11</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
  1. 新建一个bootstrap.yml,配置如下:
spring:
  application:
    name: pj-demo
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/olysa/your-project.git		#gitee仓库地址,也可以是其他如gitlab
          username: yourusername			# 用户名
          password: yourpassword			# 密码
server:
  port: 9001
  1. 启动程序如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class DemoApplication {

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

  1. 启动程序,可以看见端口为配置的环境9001

输入http://localhost:9001/pj-test-dev.yml,可以看到在gitee上创建的文件内容:

这样做好后,接下的工作就是,新建一个微服务项目pj-test来使用这个配置中心中已经从gitee上读取的配置信息。

新建pj-test微服务项目

  1. 添加依赖:
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.12.RELEASE</version>
    </parent>

    <properties>
        <spring-cloud.version>Edgware.SR4</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--配置中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!--web ,包含tomcat-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--多继承-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Brussels-SR11</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  1. 新建bootstrap.yml,配置信息如下:
spring:
  application:
    name: pj-test 	#这个很关键,这个将和profile字段组合成配置文件名称pj-test-dev,对应gitee上面创建的pj-test-dev.yml文件
  cloud:
    config:
      profile: dev		#读取哪个配置文件
      uri: http://localhost:9001/		# 配置中心的地址
  1. 启动程序,和平常的程序一样
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApplication {

	public static void main(String[] args) {
		SpringApplication.run(TestApplication.class, args);
	}
}
  1. 启动程序,由日志信息可以看到,首先去配置中心http://localhost:9001/拉取相关的配置内容,然后根据内容,启动了容器的1112端口,正是我们在gitee上创建的那个文件中的配置信息。

到这,应该知道配置如何搭建配置中心已经如何使用配置中心了吧~~~

猜你喜欢

转载自blog.csdn.net/She_lock/article/details/84341973