springcloud:config+bus-amqp+actu

2019-10-10  本文已影响0人  CXY_XZL

1.框架版本
spring boot - version: 2.1.9.RELEASE
spring cloud - version : Greenwich.SR3
git版本控制工具 : 码云
2.代码

前提条件:
需要已安装erlang和rabbitmq,并启动它们。
如果您是windows系统,可以参照 Windows安装rabbitmq 这篇文章

2.1 eureka服务端

<?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.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>xzl.xiongzelin</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

package xzl.xiongzelin.eurekaserver;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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


 #服务名称
spring.application.name=eureka.server

 #端口
server.port=8761

 # eureka访问路径
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

#这行属性最好不加,当配置eureka服务端负载均衡时有这行代码会出问题(其他实例不可用)
#eureka.instance.prefer-ip-address=true

#服务端示例id
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

#单eureka服务器必加属性(以下两个)
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

启动eureka服务器,访问http://localhost:8761,正常界面如下:

eureka服务器正常界面.png

2.2 config-server 服务端

<?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.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>xzl.xiongzelin</groupId>
    <artifactId>spring-cloud-config-monitor-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-config-monitor-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


package xzl.xiongzelin.springcloudconfigmonitorserver;

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;

@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudConfigMonitorServerApplication {

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



server.port=9999

#本应用名称
spring.application.name=config-server

#此处用的是gitee,也就是码云存储配置文件
spring.cloud.config.server.git.uri=https://gitee.com/xiongzelin/springcloudconfigserver.git

#配置文件的文件夹路径
spring.cloud.config.server.git.search-paths=config-repo

#登录gitee所需账号及密码
spring.cloud.config.server.git.username=xiongzelin
spring.cloud.config.server.git.password=xzl.123

eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

#actuator配置
  ##开启actuator
management.endpoints.enabled-by-default=true
  ##打印日志
management.endpoint.health.show-details=always
  ##向外暴露的接口,这里*则表示所有的actuator接口都可以被访问
management.endpoints.web.exposure.include=*

#rabbitmq配置信息,这里的信息和默认的rabbitmq信息一样,其实是可以省略掉的
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

#encrypt.key=4444

启动config-server,正常情况下,该服务会注册到eureka,页面如下:


config-server注册成功.png

2.3 config-client 客户端

<?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.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>xzl.xiongzelin</groupId>
    <artifactId>spring-cloud-config-monitor-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-config-monitor-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>


package xzl.xiongzelin.eurekaserver;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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


package xzl.xiongzelin.springcloudconfigmonitorclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RefreshScope
@RestController
public class TestController {
  
    /**配置文件中的属性*/
    @Value("${from}")
    private String from;

    @RequestMapping("/from")
    public String from (){
        return this.from;
    }
}



spring.application.name=didispace
server.port=8866

#开启配置发现
spring.cloud.config.discovery.enabled=true

#对应配置服务名称
spring.cloud.config.discovery.service-id=config-server

#git分支名称
spring.cloud.config.label=feature-xiongzelin

#profile 对应开发环境
spring.cloud.config.profile=dev

##这行代码很重要,根据官方定义的标准来的
spring.cloud.bus.id=${spring.application.name}:${spring.cloud.config.profile}:${random.value}

eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

#下面的配置可省
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

启动config-client,同样的,在eureka中它也会被注册,访问http://localhost:8866/from获取from的值,发现页面展示的是git-dev-28.0,页面就不展示了。

2.4配置文件

上图只展示了master分支,还有一个feature-xiongzelin分支,它们的文件是一样的,现在测试的是feature-xiongzelin分支,这些文件中都只有一个类似于from=git-dev-27.0的键值对

到了这里,大家就可以尝试看看是否在不配置webhook的情况下,服务端和客户端访问的配置文件是否有动态刷新以及提交变更代码时rabbitmq的queue变化



3.配置webhooks

注意:这里的webhooks所属于存放配置文件的项目,具体位置如下:


webhooks.png

点击“添加”,进入如下页面


添加webhook.png

添加页面的url要求 必须公网ip 或者 域名,我这里使用 natapp

做内网渗透,生成一个临时可用域名,具体怎么做大家百度一下就知道了,有一点要注意的是,做内网渗透的是配置服务端的ip+port,而不是配置客户端的

在生成域名后,在域名后添加/monitor就可以了,类似于http://6ku6fu.natappfree.cc/monitor ,密码可以先不设置,“选择事件”还是“push”,好了,可以直接点“添加“了



4.测试



另外:如果大家要给webhook加密码的话,急需要把config-server服务端的配置的最后一行代码注释给解开


  1. 代码地址
    eureka-server:https://gitee.com/xiongzelin/eureka-seever.git
    config-server:https://gitee.com/xiongzelin/spring-cloud-config-monitor-server.git
    config-client: https://gitee.com/xiongzelin/spring-cloud-config-monitor-client.git
    配置文件:https://gitee.com/xiongzelin/springcloudconfigserver.git
上一篇 下一篇

猜你喜欢

热点阅读