码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)

时间:2019-04-26 22:51:07      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:apach   plugins   clu   cat   param   rabbitmq   framework   file   led   

一、安装rabbitmq

二、pom父文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>1.0.0</groupId>
  <artifactId>springcloud-config-bus</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>springcloud-config-bus</name>
  <url>http://maven.apache.org</url>

  
 
  <modules>
    <module>springcloud-config-bus-eureka</module>
    <module>springcloud-config-bus-client2</module>
    <module>springcloud-config-bus-server</module>
  </modules>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/>
    </parent>
     
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
       <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
     
      <dependencies>
        <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>

三、springcloud-config-bus-eureka

1、pom文件

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>1.0.0</groupId>
    <artifactId>springcloud-config-bus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-config-bus-eureka</artifactId>
  <name>springcloud-config-bus-eureka</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
   <dependencies>
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
  </dependencies>
</project>

2、application.properties

spring.application.name=springcloud-config-bus-eureka

server.port=8006
## 表示是否将自己注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=false
## 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=false

## 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

3、启动类

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

@SpringBootApplication
@EnableEurekaServer
public class ConfigBusEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigBusEurekaApplication.class, args);
         System.out.println("config bus 注册中心服务启动...");
    }
}

四、springcloud-config-bus-server

1、pom文件

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>1.0.0</groupId>
        <artifactId>springcloud-config-bus</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>springcloud-config-bus-server</artifactId>
    <name>springcloud-config-bus-server</name>
    <url>http://maven.apache.org</url>
    <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-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>

    </dependencies>

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

2、application.properties

spring.application.name=springcloud-config-bus-server

server.port=9005

## 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/

## 读取本地文件,开启此属性时配合客户端的spring.cloud.config.discovery.enabled=true会从本地配置文件中读取属性覆盖git属性,但是本地属性文件更改后不会刷新
# spring.profiles.active=native


## 读取git的路径
# git仓库的地址
 spring.cloud.config.server.git.uri = https://github.com/11500667/springcloud/
# git仓库地址下的相对地址 多个用逗号","分割
spring.cloud.config.server.git.search-paths = /springcloud-config-bus/config-repo
# git仓库的账号
 spring.cloud.config.server.git.username =
# git仓库的密码
 spring.cloud.config.server.git.password =

# management.endpoints.web.exposure.include: bus-refresh
management.endpoints.web.exposure.include=bus-refresh
## bus

spring.cloud.bus.enabled = true
#  失败快速响应
spring.cloud.bus.trace.enabled = true

spring.rabbitmq.host:127.0.0.1
spring.rabbitmq.port:5672
spring.rabbitmq.username:guest
spring.rabbitmq.password:guest

3、启动类

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

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
        System.out.println("配置中心服务端启动成功!");
    }
}

4、本地属性配置文件

word=hello!!!

五、springcloud-config-bus-client2

1、pom.文件

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>1.0.0</groupId>
        <artifactId>springcloud-config-bus</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>springcloud-config-bus-client2</artifactId>
    <name>springcloud-config-bus-client2</name>
    <url>http://maven.apache.org</url>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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>

    </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>

2、application.properties

spring.application.name=springcloud-config-bus-client2
server.port=9007


# 不启用安全验证  springboot 1.x使用
# management.security.enabled=false
# 暴露refresh接入点 springboot 2.x使用
management.endpoints.web.exposure.include=refresh,health,info


## bus
spring.cloud.config.failFast=true

spring.cloud.bus.trace.enabled = true

spring.rabbitmq.host:127.0.0.1
spring.rabbitmq.port:5672
spring.rabbitmq.username:guest
spring.rabbitmq.password:guest

3、bootstrap.properties

spring.cloud.config.name=configtest
spring.cloud.config.profile=pro
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=springcloud-config-bus-server

eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/

4、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication2 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication2.class, args);
        System.out.println("配置中心第二个客户端启动成功!");
    }
}

5、测试contorller

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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope//必须有此注解的类才会刷新修改属性
public class ClientController {
    
    @Value("${word}")
    private String word;
    
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return name+","+this.word;
    }

}

六、更新属性后用以下两种方式刷新,提交的请求必须是post方式

1、springcloud-config-bus-server

ip:端口/actuator/bus-refresh

2、springcloud-config-bus-client2

ip:端口/actuator/refresh

 

SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)

标签:apach   plugins   clu   cat   param   rabbitmq   framework   file   led   

原文地址:https://www.cnblogs.com/xiaofengfree/p/10776866.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!