SpringCloud Bus 메시지 버스 의 구체 적 인 사용

메시지 버스
1.개념
마이크로 서비스 구조 에서 보통 경량급 정보 대 리 를 사용 하여 공유 하 는 정보 주 제 를 구축 하여 각 마이크로 서비스 인 스 턴 스 를 연결 합 니 다.이 방송 정 보 는 등록 센터 에 있 는 모든 마이크로 서비스 인 스 턴 스 에 의 해 감청 되 고 소 비 됩 니 다.또한 메시지 버스 라 고도 부 릅 니 다.
2. SpringCloud Bus
SpringCloud 에 도 대응 하 는 해결 방안 이 있다.SpringCloud Bus 는 분포 식 노드 를 가 벼 운 메시지 에이전트 로 연결 하면 메시지 버스 를 쉽게 구축 하고 SpringCloud config 와 결합 하여 마이크로 서비스 응용 설정 정보의 동적 업 데 이 트 를 실현 할 수 있다.
3.기타
메시지 대 리 는 미들웨어 에 속한다.디자인 에이전트 의 목적 은 응용 프로그램 에서 메 시 지 를 전달 하고 특별한 조작 을 수행 하 는 것 이다.액 티 브 MQ,Kafka,RabbitMQ,RocketMQ 등 현재 springCloud 는 RabbitMQ 와 Kafka 만 지원 하고 있다.본 고 는 RabbitMQ 를 이용 하여 이 기능 을 실현 한다.
분포 식 배치 센터 를 구축 하 다.
1.Config 구조

시스템 의 프로필 이 바 뀌 었 을 때 이 서 비 스 를 다시 시작 해 야 새로운 프로필 이 적 용 됩 니 다.spring cloud config 는 마이크로 서비스 에 있 는 모든 시스템 의 프로필 을 통일 적 으로 관리 할 수 있 고 프로필 이 바 뀌 었 을 때 시스템 이 자동 으로 업데이트 하여 새로운 설정 을 가 져 올 수 있 습 니 다.
2.Git 환경 구축
코드 클 라 우 드 환경 구축 git
코드 클 라 우 드 환경 주소:https://gitee.com/guopf/springcloud_bus
3.Git 서버 에서 프로필 업로드
규범 화 된 서비스 이름-버 전.yml 예 를 들 어 configclientdev.yml

4.유레카 서비스 등록 센터 구축
구체 적 으로 환경 을 구축 한 후에 보충 하면 내 가 배치 한 것 을 사용 할 수 있다.http://47.105.86.222:8100 (설정 주소http://47.105.86.222:8100/eureka)
5.config-server 서비스 구축
1.maven 의존

<dependencies>
    <!-- SpringBoot  Web   -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--spring-cloud    config-server -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
      <version>2.0.2.RELEASE</version>
    </dependency>

    <!-- SpringBoot  eureka    -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      <version>2.0.2.RELEASE</version>
    </dependency>
 </dependencies>
2.프로필

###        
server:
 port: 8800
###     
spring:
 application:
  name: config_server
 cloud:
  config:
   server:
    git:
     ### git   
     uri: https://gitee.com/guopf/springcloud_bus.git
     username:
     password:
     ###        
     search-paths: config
   ###   
   label: master

### eureka   
eureka:
 client:
  service-url:
   defaultZone: http://47.105.86.222:8100/eureka
  register-with-eureka: true
  fetch-registry: true
3.시동

/**
 * @EnableEurekaClient :    eureka    
 * @EnableConfigServer :    config    
 *
 */
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class App {
  public static void main(String[] args) {
    SpringApplication.run(App.class,args);
  }
}
config-client 서비스 구축
1.수 동 업데이트 
1.maven 의존

  <dependencies>
    <!-- SpringBoot  Web   -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-client</artifactId>
      <version>2.0.2.RELEASE</version>
    </dependency>
    <!-- SpringBoot  eureka    -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      <version>2.0.2.RELEASE</version>
    </dependency>
    <!--  jar ,  rabbitMQ      bus
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    -->
    <!-- actuator     -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

  </dependencies>
2.프로필(bootstrap.yml)

###   
server:
 port: 8801
 ### eureka     
eureka:
 client:
  service-url:
   defaultZone: http://47.105.86.222:8100/eureka
  fetch-registry: true
  register-with-eureka: true

###       ,  config           
spring:
 application:
  name: configclient
 cloud:
  config:
   ###     
   profile: dev
   discovery:
    ###
    enabled: true
    ### config            
    service-id: config_server
management:
 endpoints:
  web:
   exposure:
    include: "*"
3.시동

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

/**
 *                  @RefreshScope   
 */
@RestController
@RefreshScope
public class AppController {

  @Value("${userAge}")
  private String userAge;

  @GetMapping("/userAge")
  public String config(){
    System.out.println("userAge : " + userAge);
    return userAge;
  }
}
git 창고 설정 수정,수 동 업데이트,post 요청
http://127.0.0.1:8801/actuator/refresh  리 셋 시작 cofnig서버 읽 기
2.메시지 버스 로 업데이트
1.의존 정보 추가
configserver,config_client 에 추가 

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    <!-- actuator     -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
2.프로필 수정
rabbitMQ 에 대한 설정,rabbitMQ 서비스 와 config 추가server,config_client 는 한 서버 에서 기본 설정 을 사용 하면 됩 니 다.

### rabbitmq     
 rabbitmq:
  addresses: 47.105.86.222
  username: guest
  password: guest
  port: 5672
  virtual-host: /
3.리 셋
http://127.0.0.1:8801/actuator/bus-refresh   post 방식
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기