-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
caochaoping
committed
Oct 9, 2016
0 parents
commit 83390a4
Showing
80 changed files
with
1,756 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/Gemfile.lock | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
/gemfiles/*.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--format documentation | ||
--color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require: rubocop-rspec | ||
|
||
AllCops: | ||
Exclude: | ||
- 'spec/dummy/db/**/*' | ||
|
||
Metrics/AbcSize: | ||
Enabled: false | ||
|
||
Metrics/CyclomaticComplexity: | ||
Enabled: false | ||
|
||
Metrics/ClassLength: | ||
Max: 160 | ||
|
||
Metrics/LineLength: | ||
Max: 160 | ||
|
||
Metrics/MethodLength: | ||
Max: 40 | ||
|
||
Metrics/PerceivedComplexity: | ||
Max: 12 | ||
|
||
Style/Documentation: | ||
Enabled: false | ||
|
||
Style/FrozenStringLiteralComment: | ||
Enabled: false | ||
|
||
Style/GlobalVars: | ||
AllowedVariables: ['$user_service_publisher'] | ||
|
||
Style/ParallelAssignment: | ||
Enabled: false | ||
|
||
Style/RedundantReturn: | ||
Enabled: false | ||
|
||
RSpec/NamedSubject: | ||
Enabled: false | ||
|
||
RSpec/ExampleLength: | ||
Max: 16 | ||
|
||
RSpec/MultipleExpectations: | ||
Max: 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
- | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
eval_gemfile File.expand_path('../gemfiles/rails50.gemfile', __FILE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Souche Car Service Co., Ltd, HANGZHOU. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# OnsOnRails | ||
|
||
整合阿里云 ONS 到 Rails 项目 | ||
|
||
## 项目依赖 | ||
|
||
* Linux/Unix 系统 | ||
* Ruby 2.1.5 或以上版本 | ||
* Rails 4.1.0 或以上版本 | ||
|
||
## 如何使用 | ||
|
||
### 配置 Rails 环境 | ||
|
||
#### 在 Gemfile 添加依赖规则 | ||
|
||
```ruby | ||
gem 'ons', group: :linux | ||
gem 'ons_on_rails' | ||
``` | ||
|
||
#### 在 config/application.rb 添加 require 规则 | ||
|
||
```ruby | ||
Bundler.require(*Rails.groups) | ||
Bundler.require(RUBY_PLATFORM.match(/(linux|darwin)/)[0].to_sym) | ||
``` | ||
|
||
#### 在 config/ 目录下添加配置文件 ons\_on\_rails.yml | ||
|
||
```yaml | ||
# | ||
# access_key,阿里云官网身份验证访问码 | ||
# secret_key,阿里云身份验证密钥 | ||
# | ||
# user_service_subscriber,消费者名称,需要与实际定义的类名信息保持一致,具体见下文的消费者章节 | ||
# user_service_subscriber#consumer_id,阿里云 MQ 控制台创建的 Consumer ID | ||
# user_service_subscriber#topic,阿里云 MQ 控制台创建的 Topic | ||
# user_service_subscriber#tag,当前消费者订阅的 Topic 下所关注的消息标签表达式 | ||
# | ||
# user_service_publisher,生成者名称,具体见下文的生成者章节 | ||
# user_service_publisher#producer_id,阿里云 MQ 控制台创建的 Producer ID | ||
# user_service_publisher#topic,阿里云 MQ 控制台创建的 Topic | ||
# user_service_publisher#tag,当前生成者发布的消息所使用的消息标签 | ||
# | ||
default: &default | ||
access_key: <%= ENV['ONS_ACCESS_KEY'] %> | ||
secret_key: <%= ENV['ONS_SECRET_KEY'] %> | ||
user_service_subscriber: | ||
consumer_id: <%= ENV['ONS_CONSUMER_ID'] %> | ||
topic: <%= ENV['ONS_TOPIC'] %> | ||
tag: 'user_service' | ||
user_service_publisher: | ||
producer_id: <%= ENV['ONS_PRODUCER_ID'] %> | ||
topic: <%= ENV['ONS_TOPIC'] %> | ||
tag: 'user_service' | ||
|
||
development: | ||
<<: *default | ||
|
||
test: | ||
<<: *default | ||
|
||
production: | ||
<<: *default | ||
``` | ||
### 消费者 | ||
#### 在 app/subscribers 目录下添加消费者实现文件,比如 user\_service\_subscriber.rb | ||
```ruby | ||
# 注意,类名应当与 config/ons_on_rails.yml 中的配置保持一致 | ||
class UserServiceSubscriber | ||
include OnsOnRails::Subscriber | ||
|
||
def consume(message) | ||
# do something... | ||
end | ||
end | ||
``` | ||
|
||
#### 在 daemons/ 目录下添加守护进程定义文件,比如 user\_service\_subscriber\_control.rb | ||
|
||
```ruby | ||
require 'rubygems' | ||
require 'ons_on_rails' | ||
|
||
APP_PATH = File.expand_path('../..', __FILE__) | ||
OnsOnRails.run_subscriber_as_a_daemon(:user_service_subscriber, APP_PATH) | ||
``` | ||
|
||
#### 启动或关闭消费者进程,此进程会与阿里云 MQ 建立 TCP 连接,然后在本地消费消息 | ||
|
||
```bash | ||
$ RAILS_ENV=development bundle exec ruby daemons/user_service_subscriber_control.rb start | ||
$ RAILS_ENV=development bundle exec ruby daemons/user_service_subscriber_control.rb stop | ||
``` | ||
|
||
### 生产者 | ||
|
||
#### 在 config/initializers/ 目录下添加初始化文件,比如 ons\_on\_rails.rb | ||
|
||
```ruby | ||
# 注意,第一个参数应当与 config/ons_on_rails.yml 中的配置保持一致 | ||
$user_service_publisher = OnsOnRails.create_publisher(:user_service_publisher, backend: :tcp) | ||
``` | ||
|
||
#### 调用 OnsOnRails::Publisher#publish 方法 | ||
|
||
```ruby | ||
$user_service_publisher.publish(operate: :create, user: { name: '123456lkjhgf' }) | ||
``` | ||
|
||
#### 测试相关 | ||
|
||
设置 OnsOnRails::Publisher 的 backend 为 :test 方法即可,这样生产者会将消息保存到 OnsOnRails::Publisher.deliveries 数组中 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'bundler/setup' | ||
|
||
APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__) | ||
load 'rails/tasks/engine.rake' | ||
load 'rails/tasks/statistics.rake' | ||
|
||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new :spec do | ||
Rails.env = 'test' | ||
Rake::Task['app:db:environment:set'].invoke if Rails::VERSION::MAJOR == 5 | ||
Rake::Task['app:db:drop'].invoke | ||
Rake::Task['app:db:create'].invoke | ||
Rake::Task['app:db:schema:load'].invoke | ||
end | ||
|
||
task 'spec:all' do | ||
Dir.glob(File.join(__dir__, 'gemfiles', '*.gemfile')).each do |gemfile| | ||
sh "BUNDLE_GEMFILE=#{gemfile} bundle install --quiet" | ||
sh "BUNDLE_GEMFILE=#{gemfile} bin/rake spec" | ||
end | ||
end | ||
|
||
task default: 'spec:all' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'bundler/setup' | ||
require 'ons_on_rails' | ||
|
||
# You can add fixtures and/or initialization code here to make experimenting | ||
# with your gem easier. You can also use a different console, if you like. | ||
|
||
# (If you use this, don't forget to add pry to your Gemfile!) | ||
# require "pry" | ||
# Pry.start | ||
|
||
require 'irb' | ||
IRB.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
# | ||
# This file was generated by Bundler. | ||
# | ||
# The application 'rake' is installed as part of a gem, and | ||
# this file is here to facilitate running it. | ||
# | ||
|
||
require 'pathname' | ||
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', | ||
Pathname.new(__FILE__).realpath) | ||
|
||
require 'rubygems' | ||
require 'bundler/setup' | ||
|
||
load Gem.bin_path('rake', 'rake') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
# | ||
# This file was generated by Bundler. | ||
# | ||
# The application 'rspec' is installed as part of a gem, and | ||
# this file is here to facilitate running it. | ||
# | ||
|
||
require 'pathname' | ||
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', | ||
Pathname.new(__FILE__).realpath) | ||
|
||
require 'rubygems' | ||
require 'bundler/setup' | ||
|
||
load Gem.bin_path('rspec-core', 'rspec') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
# | ||
# This file was generated by Bundler. | ||
# | ||
# The application 'rubocop' is installed as part of a gem, and | ||
# this file is here to facilitate running it. | ||
# | ||
|
||
require 'pathname' | ||
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', | ||
Pathname.new(__FILE__).realpath) | ||
|
||
require 'rubygems' | ||
require 'bundler/setup' | ||
|
||
load Gem.bin_path('rubocop', 'rubocop') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'rails', '~> 4.1.0' | ||
|
||
gemspec path: '..' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'rails', '~> 4.2.0' | ||
|
||
gemspec path: '..' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'rails', '~> 5.0.0' | ||
|
||
gemspec path: '..' |
Oops, something went wrong.