美文网首页SpringC...
SpringCloud配置中心和注册中心最简单上手

SpringCloud配置中心和注册中心最简单上手

作者: CuminBeef | 来源:发表于2020-05-24 19:46 被阅读0次

1、前言

最简单的配置中心和注册中心使用,主要使用了gitee存放配置文件,其实使用github、gitlab、gogs或其他git仓库管理工具都可以。

本案例里面主要包括3个工程:
1、mycloud:父工程,除了一个pom.xml文件,啥都不需要
2、myconfig:配置中心,主要就是引用一下gitee仓库,也没有其他什么配置,部署时可以最先启动
3、myeureka:注册中心,可以顺便测试一下配置中心得调用,部署时第二个启动

本次版本
SpringBoot 2.2.5.RELEASE
SpringCloud Hoxton.SR4

2、父工程:mycloud

新建一个maven工程,只保留一个pom.xml文件就行了。
注意必须要有这个配置<packaging>pom</packaging>,不然创建不了子工程。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/>
    </parent>
    
    <groupId>com.adonisz</groupId>
    <artifactId>mycloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>mycloud</name>
    <description>My Cloud Project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

    <modules>
        <module>myeureka</module>
        <module>myconfig</module>
        <module>mybase</module>
    </modules>
</project>

3、配置中心:myconfig

在父工程上新一个model名字叫myconfig,配置中心一共就需要三个文件。
首先是pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.adonisz</groupId>
    <artifactId>mycloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>myconfig</artifactId>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
  </dependencies>
  
</project>

然后是配置文件application.yml,其中application.name中只能用横杠,不能使用下划线,据说是spring的规范。(而工程名字只能使用下划线,如在idea中,可以自己试试。)

server:
  port: 12000
spring:
  application:
    name: my-config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/adonisz/mycloud.git
#          username: adonisz # 私有仓库才需要输入用户名和密码,公开仓库就不需要了。
#          password: zzzzzzz

最后就是启动器了,主要就是加了个注解@EnableConfigServer

package com.adonisz.myconfig;

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

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

这三个文件,配置中心就完事了,主要是仓库的设置。
首先登陆https://gitee.com/,创建自己的一个仓库。
如果设置为私有仓库时,上面配置文件里就要用户名和密码了;设置公开仓库时就不需要了,怎么设置我就不赘述了,还是需要自己百度一下的,不能老伸手是吧。

仓库里面先准备我们配置文件:
eureka-test.yml,其中defaultZone中的ip是要部署的服务器地址

server:
 port: 11000
spring:
 application:
   name: my-eureka
eureka:
 client:
   registerWithEureka: true
   fetchRegistry: true
   serviceUrl:
     defaultZone: http://127.0.0.1:${server.port}/eureka/

本地最好安装一个git,过程也简单,全都下一步就完事了。在一个文件夹里,将上面文件挪进来,文件夹内空白的地方,右击 -> git bash here

git init  #初始化为git目录
git add *.yml  # 标记为待添加
git commit -m 'add config files' # 提交到本地仓库,并注释,ps注释是必须的
git remote add origin https://gitee.com/adonisz/mycloud.git # 连接到远程仓库,初次提交需要,以后就可以省略了。
git push -u origin master # 提交到远程仓库,之后就可以在gitee上看到了

然后就可以知己而启动这个配置中心了,其他工程只要连接配置中心,然后配置中心去仓库找配置文件。

4、注册中心:myeureka

在父工程上再新建一个工程myeureka,同样也就三个文件。
pom.yml,主要就是引用eureka服务和config启动依赖。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.adonisz</groupId>
    <artifactId>mycloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>myeureka</artifactId>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  </dependencies>
  
</project>

本次使用的配置文件是bootstrap.yml。bootstrap.yml和application.yml的区别就是bootstrap.yml是在项目启动前生效的,所以在这个时候配置连接配置中心,就可以通过配置中心获取仓库中的配置文件。如果将这个配置放到application.yml中,项目启动时就不会去找配置中心。

spring:
  cloud:
    config:
      name: eureka
      profile: dev
      label: master
      uri: http://127.0.0.1:12000

启动器MyEurekaApplication.java,这也没啥,就是加个注释@EnableEurekaServer

package com.adonisz.eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class MyEurekaApplication {

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

然后就可以启动了,我们在配置中心的文件中设置的是11000端口,所以在浏览器中直接访问就好了:
http://localhost:11000/

5、后语

以上就是微服务中最基本的两个服务,配置中心和注册中心,本次重点就是配置中心的使用,其他项目使用bootstrap.yml在启动前连接配置中心服务,配置中心就会去配置远程仓库中查找配置文件然后返回给调用处。注册中心就是为了方便其他业务服务间的调用。

相关文章

网友评论

    本文标题:SpringCloud配置中心和注册中心最简单上手

    本文链接:https://www.haomeiwen.com/subject/qcijahtx.html