第三课 Go容器化微服务系统实战-微服务注册中心和配置中心
tags:
- GO
- 慕课网
categories:
- 注册中心Consul
文章目录
第一节 注册中心Consul
1.1 注册中心Consul 基本介绍
- Consul是一种服务网格解决方案
- 提供具有服务发现,配置和分段功能的全功能控制平面
- Consul附带一个简单的内置代理,可以开箱即用
- 注册中心Consul关键功能
- 服务发现:客户端可以注册服务,程序可以轻松找到它们所依赖的服务
- 运行状况检查:Consul客户端可以提供任意数量的运行状况检查
- KV存储:应用程序可以将Consul的层级键/值存储用于任何目的,包括动态配置,功能标记,协调,领导者选举等
- 安全服务通信:Consul可以为服务生成和分发TLS证书,建立相互的TLS连接
- 多数据中心:Consul支持多个数据中心
1.2 注册中心Consul集群架构
- 注册中心Consul两个重要协议
- Gossip Protocol(八卦协议)
- Raft Protocol(选举协议)
- 注册中心Consul Gossip协议
- 局域网池(LAN Pool)
- 让Client自动发现Server节点,减少所需的配置量
- 分布式故障检测在某几个Server机上执行
- 能够用来快速的广播事件
- 广域网池(WAN Pool)
- WAN Pool全局唯一的
- 不同数据中心的Server都会加如WAN Pool
- 允许服务器执行跨数据中心请求
- 局域网池(LAN Pool)
1.3 注册中心Consul安装
- docker pull consul:1.6.0
- docker run -d -p 8500:8500 consul:1.6.0
- 访问:http://192.168.242.142:8500/
第二节 电商微服务分类模块开发
2.1 分类模块目录建立
- 用上节课方法自动生成我们的目录文件。
sudo docker pull micro/micro
sudo docker run --rm -v $(pwd):$(pwd) -w $(pwd) micro/micro new category
- 新建domain文件夹,并在domain中创建model、repository、service三个文件夹。
- 删除go.mod,重新通过下面命令生成。
go mod init git.imooc.com/qnhyn/category
go mod tidy
- 新建common文件夹。
2.2 分类模块代码proto开发
- 编写proto。把category.proto文件放到category文件夹下。
- 编译生成go文件。
# 生成之后看下category.pb.micro.go 用到的版本 主要要是v2(v3会在编译时报错)
protoc *.proto --gofast_out=. --micro_out=.
syntax = "proto3";
package go.micro.service.category;
service Category {
rpc CreateCategory(CategoryRequest) returns (CreateCategoryResponse) {}
rpc UpdateCategory(CategoryRequest) returns (UpdateCategoryResponse) {}
rpc DeleteCategory(DeleteCategoryRequest) returns (DeleteCategoryResponse){}
rpc FindCategoryByName(FindByNameRequest) returns (CategoryResponse) {}
rpc FindCategoryByID(FindByIdRequest) returns (CategoryResponse){}
rpc FindCategoryByLevel(FindByLevelRequest) returns (FindAllResponse) {}
rpc FindCategoryByParent(FindByParentRequest) returns (FindAllResponse) {}
rpc FindAllCategory(FindAllRequest) returns (FindAllResponse){}
}
message CategoryRequest {
string category_name = 1;
uint32 category_level = 2;
int64 category_parent = 3;
string category_image = 4;
string category_description = 5;
}
message CreateCategoryResponse {
string message =1 ;
int64 category_id =2;
}
message UpdateCategoryResponse {
string message = 1;
}
message DeleteCategoryRequest {
int64 category_id =1 ;
}
message DeleteCategoryResponse {
string message =1;
}
message FindByNameRequest {
string category_name =1;
}
message CategoryResponse {
int64 id = 1;
string category_name =2;
uint32 category_level = 3;
int64 category_parent =4;
string category_images =5;
string category_description =6;
}
message FindByIdRequest {
int64 category_id = 1;
}
message FindByLevelRequest {
uint32 level =1;
}
message FindByParentRequest {
int64 parent_id =1;
}
message FindAllRequest {
}
message FindAllResponse {
// 复用之前的CategoryResponse
repeated CategoryResponse category =1;
}
2.3 分类模块代码domain开发
- 在model文件夹下,新建模型category.go。
package model
type Category struct{
ID int64 `gorm:"primary_key;not_null;auto_increment" json:"id"`
CategoryName string `gorm:"unique_index,not_null" json:"category_name"`
CategoryLevel uint32 `json:"category_level"`
CategoryParent int64 `json:"category_parent"`
CategoryImage string `json:"category_image"`
CategoryDescription string `json:"category_description"`
}
- 在repository文件夹下,新建category_repository.go
package repository
import (
"github.com/jinzhu/gorm"
"git.imooc.com/coding-447/category/domain/model"
)
type ICategoryRepository interface{
InitTable() error
FindCategoryByID(int64) (*model.Category, error)
CreateCategory(*model.Category) (int64, error)
DeleteCategoryByID(int64) error
UpdateCategory(*model.Category) error
FindAll()([]model.Category,error)
FindCategoryByName(string) (*model.Category,error)
FindCategoryByLevel(uint32) ([]model.Category,error)
FindCategoryByParent(int64) ([]model.Category,error)
}
//创建categoryRepository
func NewCategoryRepository(db *gorm.DB) ICategoryRepository {
return &CategoryRepository{
mysqlDb:db}
}
type CategoryRepository struct {
mysqlDb *gorm.DB
}
//初始化表
func (u *CategoryRepository)InitTable() error {
return u.mysqlDb.CreateTable(&model.Category{
}).Error
}
//根据ID查找Category信息
func (u *CategoryRepository)FindCategoryByID(categoryID int64) (category *model.Category,err error) {
category = &model.Category{
}
return category, u.mysqlDb.First(category,categoryID).Error
}
//创建Category信息
func (u *CategoryRepository) CreateCategory(category *model.Category) (int64, error) {
return category.ID, u.mysqlDb.Create(category).Error
}