第三课 Go容器化微服务系统实战-微服务注册中心和配置中心

第三课 Go容器化微服务系统实战-微服务注册中心和配置中心tags:GO慕课网categories:注册中心Consul文章目录第三课 Go容器化微服务系统实战-微服务注册中心和配置中心第一节 注册中心Consul1.1 注册中心Consul 基本介绍1.2 注册中心Consul集群架构1.3 注册中心Consul安装第二节 电商微服务分类模块开发2.1 分类模块目录建立2.2 分类模块代码proto开发2.3 分类模块代码domain开发2.4 分类模块代码Handler开发2.5
摘要由CSDN通过智能技术生成

第三课 Go容器化微服务系统实战-微服务注册中心和配置中心

tags:

  • GO
  • 慕课网

categories:

  • 注册中心Consul

第一节 注册中心Consul

1.1 注册中心Consul 基本介绍

  1. Consul是一种服务网格解决方案
  2. 提供具有服务发现,配置和分段功能的全功能控制平面
  3. Consul附带一个简单的内置代理,可以开箱即用
  4. 注册中心Consul关键功能
    • 服务发现:客户端可以注册服务,程序可以轻松找到它们所依赖的服务
    • 运行状况检查:Consul客户端可以提供任意数量的运行状况检查
    • KV存储:应用程序可以将Consul的层级键/值存储用于任何目的,包括动态配置,功能标记,协调,领导者选举等
    • 安全服务通信:Consul可以为服务生成和分发TLS证书,建立相互的TLS连接
    • 多数据中心:Consul支持多个数据中心

1.2 注册中心Consul集群架构

在这里插入图片描述

  1. 注册中心Consul两个重要协议
    • Gossip Protocol(八卦协议)
    • Raft Protocol(选举协议)
  2. 注册中心Consul Gossip协议
    • 局域网池(LAN Pool)
      • 让Client自动发现Server节点,减少所需的配置量
      • 分布式故障检测在某几个Server机上执行
      • 能够用来快速的广播事件
    • 广域网池(WAN Pool)
      • WAN Pool全局唯一的
      • 不同数据中心的Server都会加如WAN Pool
      • 允许服务器执行跨数据中心请求
        在这里插入图片描述

1.3 注册中心Consul安装

  1. docker pull consul:1.6.0
  2. docker run -d -p 8500:8500 consul:1.6.0
  3. 访问:http://192.168.242.142:8500/
    在这里插入图片描述

第二节 电商微服务分类模块开发

2.1 分类模块目录建立

  1. 用上节课方法自动生成我们的目录文件
sudo docker pull micro/micro
sudo docker run --rm -v $(pwd):$(pwd) -w $(pwd) micro/micro new category
  1. 新建domain文件夹,并在domain中创建model、repository、service三个文件夹。
  2. 删除go.mod,重新通过下面命令生成。
go mod init git.imooc.com/qnhyn/category
go mod tidy
  1. 新建common文件夹。

2.2 分类模块代码proto开发

  1. 编写proto。把category.proto文件放到category文件夹下。
  2. 编译生成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开发

  1. 在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"`
}
  1. 在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
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值