Java B2B2C多用户商城springboot架构——springcloud整合bus

2024-03-14,,

bus的使用主要是配合springcloud config部分来一起使用,并没有单独使用

首先建立服务端:

<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

由于本次使用的是rabbitmq进行监听

了解springcloud架构可以加求求:三五三六二四七二五九

所以需要在依赖中引入amqp。这是rabbitmq采用的协议

server:
  port: 8080
spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri: https://git.oschina.net/itmuch/spring-cloud-config-repo      # 配置Git仓库的地址
          username:                                                         # Git仓库的账号
          password:                                                         # Git仓库的密码
    bus:
      trace:
        enabled: true     # 开启cloud bus的跟踪
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
password: guest

然后再次配置连接,连接rabbitmq的地址

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}

再配置服务端:

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

application.yml

server:
port: 8081

bootstrap.yml

复制代码
spring:
  application:
    name: microservice-foo    # 对应config server所获取的配置文件的{application}
  cloud:
    config:
      uri: http://localhost:8080/
      profile: dev            # profile对应config server所获取的配置文件中的{profile} 
      label: master           # 指定Git仓库的分支,对应config server所获取的配置文件的{label}
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
password: guest

《Java B2B2C多用户商城springboot架构——springcloud整合bus.doc》

下载本文的Word格式文档,以方便收藏与打印。