Skip to content

Commit

Permalink
ocelot+consul 微服务docker化 完成
Browse files Browse the repository at this point in the history
  • Loading branch information
windcatcher committed Apr 9, 2018
1 parent 6824762 commit 78269ae
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 75 deletions.
15 changes: 12 additions & 3 deletions MicroService/03OcelotConsul_Docker/ApiGateWayApp/Ocelot.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,21 @@
}
}
],

"GlobalConfiguration": {
//local
//"BaseUrl": "http://testapi.com", //使用switchhosts配置dns 127.0.0.1 testapi.com
"BaseUrl": "http://localhost",
//"BaseUrl": "http://localhost",
//"ServiceDiscoveryProvider": {
// //"Host": "consulserver",
// "Host": "localhost",
// "Port": 8500
//},

//docker
"BaseUrl": "http://ocelotgateway",
"ServiceDiscoveryProvider": {
//"Host": "consulserver",
"Host": "localhost",
"Host": "consulserver",
"Port": 8500
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using ApiProductService.config;
using Consul;
using DnsClient;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -19,16 +20,22 @@ public class UserInfoController : Controller
{
private readonly IDnsQuery _dns;
private readonly IOptions<ServiceDisvoveryOptions> _options;
private IConsulClient _consulClient;


public UserInfoController(IDnsQuery dns, IOptions<ServiceDisvoveryOptions> options)
public UserInfoController(IDnsQuery dns, IOptions<ServiceDisvoveryOptions> options, IConsulClient consulClient)
{
_dns = dns ?? throw new ArgumentNullException(nameof(dns));
_options = options ?? throw new ArgumentNullException(nameof(options));
_consulClient = consulClient ?? throw new ArgumentNullException(nameof(consulClient));
}
[HttpGet("")]
[HttpHead("")]
public async Task<string> Get()
{
return await ByConsulClient();
}

private async Task<string> ByDnsClient()
{
var result = await _dns.ResolveServiceAsync("service.consul", _options.Value.DiscoveryServiceName);
var address = result.First();
Expand All @@ -39,6 +46,19 @@ public async Task<string> Get()
return serviceResult;
}
}

private async Task<string> ByConsulClient()
{
var result = await _consulClient.Catalog.Service(_options.Value.DiscoveryServiceName);
var resp = result.Response;
var address = resp.First();
using (var client = new HttpClient())
{
var _userApiUrl = $"http://{address.ServiceAddress}:{address.ServicePort}";
var serviceResult = await client.GetStringAsync($"{_userApiUrl}/api/user");
return serviceResult;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace ApiUserService.Controllers
{
[Route("api/[controller]")]
public class UserController : Controller
{
private readonly IOptions<ServiceDisvoveryOptions> _options;

public UserController(IOptions<ServiceDisvoveryOptions> options)
{
_options = options;
}

// GET api/User
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "User1", "User2" };
var userUri = new Uri(_options.Value.RegisterServerUrl);
var ipAdress = System.Net.Dns.GetHostAddresses(userUri.Host).Where(p => p.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).FirstOrDefault();
var serviceId = $"{ipAdress.ToString()}_{userUri.Port}";
return new string[] { serviceId + "User1", serviceId+"User2" };
}

// GET api/User/5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace ApiUserService
public class ServiceDisvoveryOptions
{
public string RegisterServiceName { get; set; }

public string ConsulUrl { get; set; }
public string RegisterServerUrl { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<ServiceDisvoveryOptions>(Configuration);
services.AddMvc();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ FROM microsoft/aspnetcore-build:2.0 AS build-env
#设置应用在docker中的工作路径
WORKDIR /app
#拷贝所有csproj文件到/app下
COPY *.csproj ./
COPY ./Client/SimpleClient/*.csproj ./
#执行还原命令
RUN dotnet restore
#拷贝所有文件到/app下
COPY . ./
COPY ./Client/SimpleClient ./
#执行发布命令,编译的发布文件生成到/app/out目录下
RUN dotnet publish -c Release -o out

Expand All @@ -19,5 +19,5 @@ WORKDIR /app
#从build-env生成/app/out目录下的所有文件拷贝到/app下
COPY --from=build-env /app/out ./
#设置应用的启动命令
ENTRYPOINT [ "dotnet","webapi_redis_docker.dll" ]
ENTRYPOINT [ "dotnet","SimpleClient.dll" ]

17 changes: 7 additions & 10 deletions MicroService/03OcelotConsul_Docker/OcelotConsul_Docker.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityServer", "IdentityS
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Client", "Client", "{6FB07EFA-4235-44E3-BA53-9D140DE86EF6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleClient", "Client\OwnerPasswordClient\SimpleClient.csproj", "{3EC783CE-B017-4B78-A10E-DC618091AC3B}"
ProjectSection(ProjectDependencies) = postProject
{A9CBD5AE-39F4-472C-A4ED-BE8645678BDB} = {A9CBD5AE-39F4-472C-A4ED-BE8645678BDB}
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B7D520E2-E86B-404C-8673-D3F1617B93B3}"
ProjectSection(SolutionItems) = preProject
docker-compose.yml = docker-compose.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleClient", "Client\SimpleClient\SimpleClient.csproj", "{B3512D4F-C98E-4012-BE22-4795EA25D828}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -61,16 +58,16 @@ Global
{200583E3-D6CC-4267-8131-19320B75F3E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{200583E3-D6CC-4267-8131-19320B75F3E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{200583E3-D6CC-4267-8131-19320B75F3E4}.Release|Any CPU.Build.0 = Release|Any CPU
{3EC783CE-B017-4B78-A10E-DC618091AC3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3EC783CE-B017-4B78-A10E-DC618091AC3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EC783CE-B017-4B78-A10E-DC618091AC3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EC783CE-B017-4B78-A10E-DC618091AC3B}.Release|Any CPU.Build.0 = Release|Any CPU
{B3512D4F-C98E-4012-BE22-4795EA25D828}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3512D4F-C98E-4012-BE22-4795EA25D828}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3512D4F-C98E-4012-BE22-4795EA25D828}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3512D4F-C98E-4012-BE22-4795EA25D828}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{3EC783CE-B017-4B78-A10E-DC618091AC3B} = {6FB07EFA-4235-44E3-BA53-9D140DE86EF6}
{B3512D4F-C98E-4012-BE22-4795EA25D828} = {6FB07EFA-4235-44E3-BA53-9D140DE86EF6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {29871066-14D6-45FF-8899-80D32031059A}
Expand Down
27 changes: 23 additions & 4 deletions MicroService/03OcelotConsul_Docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ services:
- "8400:8400"
- "8500:8500"
- "53:53"
- '8600:8600'
command: -server -ui-dir /ui -data-dir /tmp/consul -bootstrap-expect 1

user_service:
Expand All @@ -27,17 +26,35 @@ services:
dockerfile: ./ApiUserService/Dockerfile
ports:
- '8801:80'
# depends_on:
# - consulserver
depends_on:
- consulserver

user_service2:
image: user_service
environment:
- ASPNETCORE_URLS=http://0.0.0.0:80
- ConsulUrl=http://consulserver:8500
- RegisterServerUrl=http://user_service2
- RegisterServiceName=user_service
build:
context: .
dockerfile: ./ApiUserService/Dockerfile
ports:
- '8804:80'
depends_on:
- consulserver

product_service:
image: product_service
hostname: product_service
environment:
- ASPNETCORE_URLS=http://0.0.0.0:80
- ConsulUrl=http://consulserver:8500
- RegisterServerUrl=http://user_service
- RegisterServerUrl=http://product_service
- RegisterServiceName=product_service
- DiscoveryServiceName=user_service
- DiscoverDnsUrl=http://consulserver:8600

build:
context: .
dockerfile: ./ApiProductService/Dockerfile
Expand All @@ -46,6 +63,7 @@ services:

depends_on:
- consulserver
- user_service

identity_service:
image: identity_service
Expand Down Expand Up @@ -86,5 +104,6 @@ services:
- ocelotgateway
- user_service
- product_service
- identity_service


0 comments on commit 78269ae

Please sign in to comment.