iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 23
0
Software Development

30天從零開始 使用 Spring Boot 跟 Spring Cloud 建構完整微服務架構系列 第 23

Day 23 - 使用 spring-cloud-stream

透過 redis 來轉發請求

修改 reservation-client 專案

先修改 build.gradle 把 spring-cloud-starter-stream-redis 加進來

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.cloud:spring-cloud-starter-stream-redis')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('com.h2database:h2')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}

主程式上方新增 @EnableBinding (Source.class)

@EnableZuulProxy
@EnableBinding (Source.class)
@EnableDiscoveryClient
@SpringBootApplication
public class ReservationClientApplication {

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

在 Controller 中加上

	@Autowired
	@Output(Source.OUTPUT)
	private MessageChannel messageChannel;
	
	@RequestMapping( method = RequestMethod.POST)
	public void write(@RequestBody Reservation r){
		this.messageChannel.send(MessageBuilder.withPayload(r.getReservationName()).build());
	}

修改 reservation-service

修改 build.gradle 把 spring-cloud-starter-stream-redis 加進來

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.cloud:spring-cloud-starter-stream-redis')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('com.h2database:h2')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}

然後在主程式加上 @EnableBinding(Sink.class)

@EnableBinding(Sink.class)
@EnableDiscoveryClient
@SpringBootApplication
public class ReservationServiceApplication {
	
	//起動的時候預先塞測試資料
	@Bean
	CommandLineRunner runner(ReservationRepository rr){
		return args -> {
			Arrays.asList("Dr. rod,Dr. Syer,Juergen,ALL THE COMMUNITY,Josh".split(","))
			.forEach( x -> rr.save(new Reservation(x)));;
			rr.findAll().forEach( System.out::println);
		};
	}

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

再增加一個訊息接入點

@MessageEndpoint
class MessageReservationReceiver{
	@Autowired
	private ReservationRepository reservationRepository;
	
	@ServiceActivator(inputChannel = Sink.INPUT)
	public void acceptReservation(String rn){
		this.reservationRepository.save(new Reservation(rn));
	}
}

然後再回到 Config-Server 的設定檔資料夾加上 spring.redis.host ,因為是透過 redis 來收送所以當然是要給位置才能用

spring.redis.host: "localhost"

這邊測試而已,還要裝個 redis 就太大費周章了,直接用 Docker 來跑吧

VAGRANTFILE_API_VERSION = "2"
 
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|  
  config.vm.box = "ubuntu/trusty64"
  config.vm.provision "docker"
  config.vm.provision "shell", inline:
    "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"
  
  config.vm.provider :virtualbox do |vb|
    vb.name = "redis"
    vb.gui = $vm_gui
    vb.memory = $vm_memory
    vb.cpus = $vm_cpus
  end
 
  config.vm.network :forwarded_port, guest: 6379, host: 6379
  
  config.ssh.username = "vagrant" 
  config.ssh.password = "vagrant" 
end
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Specify Vagrant version and Vagrant API version
#Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker'

$vm_gui = false
$vm_memory = 2048
$vm_cpus = 2

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.synced_folder ".", "/vagrant", disabled: true
  
  config.vm.define "redis" do |v|
    v.vm.provider "docker" do |d|
	  d.name = "redis"
      d.image = "redis"
      d.ports = ["6379:6379"]
      d.vagrant_vagrantfile = "./Vagrantfile.proxy"
    end
  end
end

兩個檔案放同一個資料夾後接著執行

vagrant up redis --provider=docker

好啦,程式跟環境都好了,接著把程式都叫起來,接著透過 POST 新增資料從 reservation-client -> redis -> reservation-service 寫入資料庫

curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d '{"reservationName":"Red Johnson"}' 'http://localhost:8050/reservations'

再重新查看 http://localhost:8050/reservations/names 就可以看到多了 Red Johnson 的名字


上一篇
Day 22 - 遠端服務機制 Ribbon
下一篇
Day 24 - 分散式追蹤系統視覺化介面 Zipkin UI
系列文
30天從零開始 使用 Spring Boot 跟 Spring Cloud 建構完整微服務架構35
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言