ASP.NET Core分布式项目实战

默认教学计划
1062人加入学习
(33人评价)
价格 ¥398.00
教学计划

var address = query.ResolveService("service.consul", options.Value.UserServiceName);
            var addressList = address.First().AddressList;
            var host = addressList.Any() ?
                addressList.First().ToString() : address.First().HostName.Substring(0, address.First().HostName.Length - 1);
            var port = address.First().Port;

 

host 如果通过hostname去取,会多一个点,这里注意下

[展开全文]

.net core 2.2下会找不到UseInMemoryDatabase方法

 

解决方法:

添加这个包

Microsoft.EntityFrameworkCore.InMemory

[展开全文]
using System;
using System.Net.Http;
using IdentityModel.Client;

namespace ThirdPartyDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            ConnectServer();

            var token = GetToken();

            AccessApi(token);

            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }

        private static void ConnectServer()
        {
            // 连接IdentityServer服务端
            var httpClient = new HttpClient();
            var disco = httpClient.GetDiscoveryDocumentAsync("http://localhost:5005").Result;
            if (disco.IsError)
            {
                Console.WriteLine(disco.Exception);
            };
        }

        private static string GetToken()
        {
            var httpClient = new HttpClient();
            var response = httpClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address = "http://localhost:5005/connect/token",
                ClientId = "123",
                ClientSecret = "pwd"
            }).Result;

            if (response.IsError)
            {
                Console.WriteLine(response.Exception);
                return null;
            }
            else
            {
                Console.WriteLine(response.AccessToken);
                return response.AccessToken;
            }
        }

        private static void AccessApi(string token)
        {
            if (string.IsNullOrEmpty(token)) { return;}

            var httpClient = new HttpClient();
            httpClient.SetBearerToken(token);
            var response = httpClient.GetAsync("https://localhost:5001/api/values").Result;
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            }
            else
            {
                Console.WriteLine("error");
            }
        }
    }
}

 

[展开全文]

老师这节视频中的做法是没有问题的。但是这里更新下eshop新的使用方式。

ResilientHttpClient是基于NetCore2.1的做法,在2.2出来之后,随着 HttpClientFactory 的发布eShopOnContainers

项目已经弃用了这样的构建。

各位小伙伴可以参考https://docs.microsoft.com/zh-cn/dotnet/architecture/microservices/implement-resilient-applications/implement-http-call-retries-exponential-backoff-polly

或者直接下载eshop的代码参考。

个人笔记记录,不足之处请指正。

[展开全文]

static void Main(string[] args)
{
    //DiscoveryClient已过时了,用HttpClient代替
    //官方文档:https://identitymodel.readthedocs.io/en/latest/client/discovery.html
    HttpClient hc = new HttpClient();
    var diso = hc.GetDiscoveryDocumentAsync("http://localhost:5000").Result;

    if (diso.IsError)
    {
        Console.WriteLine(diso.Error);
    }
    else
    {
        //TokenClient已过时,用HttpClient代替
        //官方文档:https://identitymodel.readthedocs.io/en/latest/client/token.html
        var tokenResponse = hc.RequestTokenAsync(new ClientCredentialsTokenRequest
        {
            Address = diso.TokenEndpoint,
            GrantType = "client_credentials",
            ClientId = "Client1",
            ClientSecret = "secret",
            Scope = "api1"
        }).Result;
        if (tokenResponse.IsError)
        {
            Console.WriteLine(tokenResponse.Error);
        }
        else
        {
            Console.WriteLine(tokenResponse.Json);
            hc.SetBearerToken(tokenResponse.AccessToken);
            var response=hc.GetAsync("http://localhost:5001/api/values").Result;
            if (response.IsSuccessStatusCode)
            {
                string result=response.Content.ReadAsStringAsync().Result;
                Console.WriteLine(result);
            }
            else
            {
                Console.WriteLine(response.StatusCode);
            }
        }
                
    }
    Console.ReadKey();
}

[展开全文]

AddIdentityServerAuthentication 无法使用

添加:IdentityServer4.AccessTokenValidation包,引用

public void ConfigureServices(IServiceCollection services)
{
    var authenticationProviderKey = "TestKey";
    Action<IdentityServerAuthenticationOptions> options = o =>
        {
            o.Authority = "https://whereyouridentityserverlives.com";
            o.ApiName = "api";
            o.SupportedTokens = SupportedTokens.Both;
            o.ApiSecret = "secret";
        };

    services.AddAuthentication()
        .AddIdentityServerAuthentication(authenticationProviderKey, options);

    services.AddOcelot();
}
[展开全文]

gitlab cl tags

搭建一个自动化平台

自动测试脚本
ui

性能测试
自动发布

gitlab cli 持续集成

 

[展开全文]

git flow

公司团队
持续更新

大多数互联网产品 适用 gitflow

 

[展开全文]

特点

1 一组小的服务

2 松散耦合

3 独立部署 独立的进程 无集中式管理

4 服务大小取决于上下文 bounded context

5  轻量级通信

6 基于业务的能力

 

带来的问题

1 分布式的问题

2 问题追踪 测试 部署 监控 分布式事务

3 服务治理

4 开发难度加大:聚合查询

 

[展开全文]

新建一个控制台项目:console

dotnet new console

制作dockerfile文件

FROM microsoft/dotnet

WORKDIR /app

COPY . /app

CMD ["dotnet","run"]

打包镜像

docker build -t liuguojiang/console:dev

docker images

docker run --name console=dev liuguojiang:dev

docker ps -a

docker logs console-dev

 

制作新dockerfile文件

FROM microsoft/dotnet:sdk AS build-env

WORKDIR /code

COPY *.csproj /code

RUN dotnet restore

COPY . /code

RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:runtime

WORKDIR /app

COPY --from=build-env /code/out /app

ENTRYPOINT ["dotnet","console.dll"]

 

docker build -t liuguojiang/console:build

dotnet run --name console-prod liuguojiang/console:prod

 

 

 

 

[展开全文]

 瀑布式开发:严格按照流程,规范化的开发过程,强调组织性,但后期无法轻易改动(涉及面很广)

 

[展开全文]

mkdir docker

cd docker

mkdir mysql

cd mysql/

mkdir config

ls 查看

mkdir db

cd config

vi my.cnf:添加

[mysqld]

user=mysql

 

 

 

[展开全文]

操作步骤:

  1. docker -d -p 3306:3306 name mysql01 mysql/mysql server  --character-set-server=utf8 --collation-server=utf8_general_ci:设置字符集
  2. docker -d -p 3306:3306 -e MYSQL_USER="LIUGUOJIANG" -e MYSQL_PASSWORD="PWD123456" --name mysql01 mysql/mysql server  --character-set-server=utf8 --collation-server=utf8_general_ci:设置用户名密码
  3. docker rm mysql01:上一节课已经创建了一个mysql01的镜像
  4. 重新执行第2步命令
  5. docker ps查看运行镜像
  6. docker logs mysql01
  7. docker exec -it mysql01 bash
  8. mysql -uliuguojiang -p;
  9. use mysql;
  10. exit;
  11. docker logs mysql01:查看寻找root密码
  12. docker -d -p 3306:3306 -e MYSQL_USER="LIUGUOJIANG"   -e MYSQL_PASSWORD="PWD123456" -e MYSQL_ROOT_PASSWORD="pwd123456" --name mysql01 mysql/mysql server  --character-set-server=utf8 --collation-server=utf8_general_ci

 

[展开全文]

操作步骤:

  1. 下载镜像:docker pull mysql/mysql-server
  2. docker run -d -p 3306:3306 --name mysql01 mysql/mysql-server
  3. 查看:docker ps
  4. docker logs mysql01--拷贝密码
  5. docker exec -it mysql01 bash
  6. mysql -u root  -p {password}
  7. use mysql
  8. SET PASSWORD FOR 'root'@'localhost'=PASSWORD('MyNewPassword');
  9. select user,host from user;
  10. CREATE USER 'liuguojiang'@'localhost' IDENTIFIED BY 'pwd23456';
  11. GRANT ALL PRIVILEGES ON *.* TO 'liuguojiang'@'localhost' WITH GRANT OPTION;
  12. CREATE USER 'liuguojiang'@'%' IDENTIFIED BY 'pwd123456';
  13. GRANT ALL ON *.* TO 'liuguojiang'@'%' WITH GRANT OPTION;
  14. SELECT USER,HOST FROM USER
  15. show vriables '%char%';
  16. exit;
  17. cd /etc
  18. cd my.cnf
  19. yun install vim
  20. cd /etc
  21. vim my.cnf
  22. 修改MYSQL默认字符集
    1. [mysql]
    2. character-set-server=utf8
    3. [client]
    4. default-character-set=utf8
    5. [mysql]
    6. default-character-set=utf8
  23. esc,输入wq
  24. mysql --help

 

[展开全文]

在2.1及以上版本中,使用以下镜像,详细内容:

FROM microsoft/dotnet:2.2-sdk as build-env
WORKDIR /code
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Releash -o out

FROM microsoft/dotnet:2.2-aspnetcore-runtime 
WORKDIR /app
COPY --from=build-env /code/out ./
EXPOSE 80
ENTRYPOINT ["dotnet","User_API.dll"]

[展开全文]

授课教师

程序员

课程特色

下载资料(2)
视频(144)
讨论(1)
图文(2)