消息总线bus

[复制链接]
发表于 2024-11-23 23:42:37 | 显示全部楼层 |阅读模式

image.png 复习单个项目手动刷新4.5

image.png

服务端刷新——直接告诉rabbitmq

image.png

客户端刷新

image.png

1.1 客户端刷新config-client-bus集群

1.1.1 pom.xml

<?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>com.agan.springcloud</groupId>

      <artifactId>config</artifactId>

      <version>0.0.1-SNAPSHOT</version>

   </parent>

   <artifactId>config-client-bus</artifactId>

   <name>config-client-bus</name>

   <url>http://maven.apache.org</url>

   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

   </properties>

   <dependencies>

      <dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-web</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>



   <build>

      <plugins>

         <plugin>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-maven-plugin</artifactId>

         </plugin>

      </plugins>

   </build>

</project>

1.1.1.1 bootstrap.properties

spring.application.name=config-client

server.port=9031



eureka.client.serviceUrl.defaultZone=http://user:123456@eureka1:8761/eureka/,http://user:123456@eureka2:8761/eureka/


#默认是hostname 注册,改成IP 注册

eureka.instance.perferIpAddress=true



#默认false,这里设置true,表示开启读取配置中心的配置

spring.cloud.config.discovery.enabled=true

#对应eureka中的配置中心serviceId,默认是configserver

spring.cloud.config.discovery.serviceId=config-server

#指定环境

spring.cloud.config.profile=dev

#git标签

spring.cloud.config.label=master






#springboot 默认开启了权限拦截 会导致 /refresh出现401,拒绝访问

management.security.enabled=false



spring.rabbitmq.host=192.168.10.17

spring.rabbitmq.port=5672

spring.rabbitmq.username=test

spring.rabbitmq.password=123456

spring.rabbitmq.virtualHost=/

1.1.1 启动类

package com.agan.book.config;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


/\*\*

 \* @author 阿甘 http://study.163.com/instructor/1016671292.htm

 \* @version 1.0

 \*/


@SpringBootApplication

@EnableEurekaClient

public class ConfigClientApplication {


   public static void main(String[] args) {

      SpringApplication.run(ConfigClientApplication.class, args);

   }

}

1.1.1 控制层TestController.java

package com.agan.book.config.controller;


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;


@RestController

@RefreshScope

public class TestController {


   @Value("\${book.config}")

   private String msg;



   @RequestMapping("/test")

   public String test(){

      return this.msg;

   }

}

1.1.1 测试复制项目建立端口号不同的集群,启动config-server

image.png

image.png

image.png

模拟post请求刷新9031

image.png

image.png

测试成功,rabbitmq自动刷新9032

image.png

1.1 服务端自动刷新——config-server-bus

1.1.1 pom.xml

<?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>com.agan.springcloud</groupId>

      <artifactId>config</artifactId>

      <version>0.0.1-SNAPSHOT</version>

   </parent>

   <artifactId>config-server-bus</artifactId>

   <name>config-server-bus</name>

   <url>http://maven.apache.org</url>

   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

   </properties>

   <dependencies>

      <dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-web</artifactId>

      </dependency>


      <dependency>

         <groupId>org.springframework.cloud</groupId>

         <artifactId>spring-cloud-config-server</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>

1.1.1.1 application.properties

spring.application.name=config-server

server.port=9030



eureka.client.serviceUrl.defaultZone=http://user:123456@eureka1:8761/eureka/,http://user:123456@eureka2:8761/eureka/


#默认是hostname 注册,改成IP 注册

eureka.instance.perferIpAddress=true



#springboot 默认开启了权限拦截 会导致 /refresh出现401,拒绝访问

management.security.enabled=false



spring.cloud.config.server.git.uri=https://gitee.com/agan\_jiagou/config

#spring.cloud.config.server.git.username=

#spring.cloud.config.server.git.password=



spring.rabbitmq.host=192.168.10.17

spring.rabbitmq.port=5672

spring.rabbitmq.username=test

spring.rabbitmq.password=123456

spring.rabbitmq.virtualHost=/

启动类

package com.agan.book.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;

import org.springframework.cloud.netflix.zuul.EnableZuulProxy;


/\*\*

 \* @author 阿甘 http://study.163.com/instructor/1016671292.htm

 \* @version 1.0

 \*/


@SpringBootApplication

@EnableConfigServer

@EnableEurekaClient

public class ConfigServerApplication {


   public static void main(String[] args) {

      SpringApplication.run(ConfigServerApplication.class, args);

   }

}

1.1.1 测试启动服务端config-server-bus,启动config-client-bus集群

image.png

image.png

修改配置文件

image.png

1.1.1 模拟post请求

image.png

image.png

image.png

1.1.1 指定局部刷新

image.png

暂时刷新a,观察没问题再全部刷新

image.png

指定刷新9031

image.png

效果

image.png

image.png

刷新用户,订单所有服务

image.png

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

GMT+8, 2025-9-7 23:57 , Processed in 0.102253 second(s), 35 queries Archiver|手机版|小黑屋|Attic ( 京ICP备2020048627号 )

快速回复 返回顶部 返回列表