Skip to content

Commit e45eb5c

Browse files
committed
Have a working docker-compose with consul service name in config
1 parent 469ca4f commit e45eb5c

File tree

10 files changed

+181
-56
lines changed

10 files changed

+181
-56
lines changed

ApiGateway/Configuration/ocelot.global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"DownstreamScheme": "http",
66
"ServiceDiscoveryProvider": {
77
"Type": "PollConsul",
8-
"Host": "consul",
8+
"Host": "http://consul",
99
"Port": 8500,
1010
"ConfigurationKey": "ApiGateway_1",
1111
"PollingInterval": 100

ApiGateway/Configuration/ocelot.movies.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{
22
"Routes": [
3+
{
4+
"DownstreamPathTemplate": "/api/movies/{movieId}/reviews",
5+
"DownstreamScheme": "http",
6+
"UpstreamPathTemplate": "/api/movies/{movieId}/reviews",
7+
"ServiceName": "ReviewsService",
8+
"SwaggerKey": "ReviewsService",
9+
"FileCacheOptions": { "TtlSeconds": 15 },
10+
"LoadBalancerOptions": {
11+
"Type": "LeastConnection"
12+
}
13+
},
314
{
415
"DownstreamPathTemplate": "/api/movies/{movieId}/reviews/{everything}",
516
"DownstreamScheme": "http",
@@ -12,6 +23,17 @@
1223
}
1324
},
1425

26+
{
27+
"DownstreamPathTemplate": "/api/movies",
28+
"DownstreamScheme": "http",
29+
"UpstreamPathTemplate": "/api/movies",
30+
"ServiceName": "MoviesService",
31+
"SwaggerKey": "MoviesService",
32+
"FileCacheOptions": { "TtlSeconds": 15 },
33+
"LoadBalancerOptions": {
34+
"Type": "LeastConnection"
35+
}
36+
},
1537
{
1638
"DownstreamPathTemplate": "/api/movies/{everything}",
1739
"DownstreamScheme": "http",

ApiGateway/Configuration/ocelot.reviews.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{
22
"Routes": [
3+
{
4+
"DownstreamPathTemplate": "/api/reviews",
5+
"DownstreamScheme": "http",
6+
"UpstreamPathTemplate": "/api/reviews",
7+
"ServiceName": "ReviewsService",
8+
"SwaggerKey": "ReviewsService",
9+
"FileCacheOptions": { "TtlSeconds": 15 },
10+
"LoadBalancerOptions": {
11+
"Type": "LeastConnection"
12+
}
13+
},
314
{
415
"DownstreamPathTemplate": "/api/reviews/{everything}",
516
"DownstreamScheme": "http",

ApiGateway/Configuration/ocelot.users.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{
22
"Routes": [
3+
{
4+
"DownstreamPathTemplate": "/api/users/{userId}/movies",
5+
"DownstreamScheme": "http",
6+
"UpstreamPathTemplate": "/api/users/{userId}/movies",
7+
"ServiceName": "MoviesService",
8+
"SwaggerKey": "MoviesService",
9+
"FileCacheOptions": { "TtlSeconds": 15 },
10+
"LoadBalancerOptions": {
11+
"Type": "LeastConnection"
12+
}
13+
},
314
{
415
"DownstreamPathTemplate": "/api/users/{userId}/movies/{everything}",
516
"DownstreamScheme": "http",
@@ -12,6 +23,17 @@
1223
}
1324
},
1425

26+
{
27+
"DownstreamPathTemplate": "/api/users/{userId}/reviews",
28+
"DownstreamScheme": "http",
29+
"UpstreamPathTemplate": "/api/users/{userId}/reviews",
30+
"ServiceName": "ReviewsService",
31+
"SwaggerKey": "ReviewsService",
32+
"FileCacheOptions": { "TtlSeconds": 15 },
33+
"LoadBalancerOptions": {
34+
"Type": "LeastConnection"
35+
}
36+
},
1537
{
1638
"DownstreamPathTemplate": "/api/users/{userId}/reviews/{everything}",
1739
"DownstreamScheme": "http",
@@ -24,6 +46,18 @@
2446
}
2547
},
2648

49+
{
50+
"DownstreamPathTemplate": "/api/users",
51+
"DownstreamScheme": "http",
52+
"UpstreamPathTemplate": "/api/users",
53+
"ServiceName": "UsersService",
54+
"SwaggerKey": "UsersService",
55+
"UpstreamHttpMethod": [ "Get", "Post" ],
56+
"FileCacheOptions": { "TtlSeconds": 15 },
57+
"LoadBalancerOptions": {
58+
"Type": "LeastConnection"
59+
}
60+
},
2761
{
2862
"DownstreamPathTemplate": "/api/users/{everything}",
2963
"DownstreamScheme": "http",

Compose/docker-compose.yml

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
version: '3.5'
22

3+
networks:
4+
# Network for API gateway
5+
network-gateway:
6+
driver: bridge
7+
ipam:
8+
driver: default
9+
config:
10+
- subnet: 172.28.2.0/24
11+
# Network for microservices
12+
network-microservice:
13+
#internal: true
14+
driver: bridge
15+
ipam:
16+
driver: default
17+
config:
18+
- subnet: 172.28.4.0/24
19+
320
services:
421
api:
522
build: ../ApiGateway
623
container_name: api
7-
restart: on-failure
24+
restart: unless-stopped
825
ports:
926
- "8080:80"
1027
networks:
11-
- docker-network
28+
- network-gateway
29+
- network-microservice
30+
links:
31+
- users_service
32+
- movies_service
33+
- reviews_service
1234
depends_on:
1335
- consul
1436

@@ -18,35 +40,52 @@ services:
1840
context: ../
1941
dockerfile: Src/UsersService/Dockerfile
2042
container_name: users_service
21-
restart: on-failure
43+
restart: unless-stopped
2244
networks:
23-
- docker-network
45+
- network-microservice
46+
links:
47+
- rabbitmq
48+
- db
49+
- mongo
50+
- consul
2451
depends_on:
2552
- rabbitmq
2653
- db
2754
- mongo
2855
- consul
56+
2957
movies_service:
3058
build:
3159
context: ../
3260
dockerfile: Src/MoviesService/Dockerfile
3361
container_name: movies_service
34-
restart: on-failure
62+
restart: unless-stopped
3563
networks:
36-
- docker-network
64+
- network-microservice
65+
links:
66+
- rabbitmq
67+
- db
68+
- mongo
69+
- consul
3770
depends_on:
3871
- rabbitmq
3972
- db
4073
- mongo
4174
- consul
75+
4276
reviews_service:
4377
build:
4478
context: ../
4579
dockerfile: Src/ReviewsService/Dockerfile
4680
container_name: reviews_service
47-
restart: on-failure
81+
restart: unless-stopped
4882
networks:
49-
- docker-network
83+
- network-microservice
84+
links:
85+
- rabbitmq
86+
- db
87+
- mongo
88+
- consul
5089
depends_on:
5190
- rabbitmq
5291
- db
@@ -56,25 +95,28 @@ services:
5695
db:
5796
image: mcr.microsoft.com/mssql/server:2019-latest
5897
container_name: db
98+
restart: on-failure
5999
environment:
60100
- "ACCEPT_EULA=Y"
61101
- "SA_PASSWORD=pass123!"
62102
ports:
63103
- "1433:1433"
64104
networks:
65-
- docker-network
105+
- network-microservice
66106

67107
mongo:
68108
image: mongo
69109
container_name: mongo
110+
restart: on-failure
70111
ports:
71112
- "27017:27017"
72113
networks:
73-
- docker-network
114+
- network-microservice
74115

75116
elasticsearch:
76117
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1
77118
container_name: elasticsearch
119+
restart: on-failure
78120
ports:
79121
- "9200:9200"
80122
environment:
@@ -83,57 +125,61 @@ services:
83125
volumes:
84126
- elasticsearch-data:/usr/share/elasticsearch/data
85127
networks:
86-
- docker-network
128+
- network-microservice
87129

88130
kibana:
89131
image: docker.elastic.co/kibana/kibana:7.8.1
90132
container_name: kibana
133+
restart: on-failure
91134
ports:
92135
- "5601:5601"
93136
depends_on:
94137
- elasticsearch
95138
networks:
96-
- docker-network
139+
- network-microservice
97140

98141
apm:
99142
image: docker.elastic.co/apm/apm-server:7.8.1
100143
container_name: apm
144+
restart: on-failure
101145
ports:
102146
- "8200:8200"
103147
depends_on:
104148
- kibana
105149
networks:
106-
- docker-network
150+
- network-microservice
107151

108152
rabbitmq:
109153
image: rabbitmq:3-management
110154
container_name: rabbitmq
155+
restart: on-failure
156+
environment:
157+
- RABBITMQ_DEFAULT_USER=user
158+
- RABBITMQ_DEFAULT_PASS=password
111159
ports:
112160
- "5672:5672"
113161
- "15672:15672"
114162
networks:
115-
- docker-network
163+
- network-microservice
116164

117165
# kafka:
118166
# image: wurstmeister/kafka
119167
# container_name: kafka
168+
# restart: on-failure
120169
# ports:
121170
# - "9092:9092"
122171
# - "9093:9093"
123172
# networks:
124-
# - docker-network
173+
# - network-microservice
125174

126175
consul:
127176
image: consul
128177
container_name: consul
178+
restart: on-failure
129179
ports:
130180
- "8500:8500"
131181
networks:
132-
- docker-network
133-
134-
networks:
135-
docker-network:
136-
driver: bridge
182+
- network-microservice
137183

138184
volumes:
139185
elasticsearch-data:

Infrastructure/Consul/ConsulExtensions.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static IServiceCollection AddConsul(this IServiceCollection services, ICo
2222

2323
services.AddSingleton<IConsulClient, ConsulClient>(p => new ConsulClient(consulConfig =>
2424
{
25-
var address = options.Address;
25+
var address = options.ConsulAddress;
2626
consulConfig.Address = new Uri(address);
2727
}));
2828

@@ -42,16 +42,24 @@ public static IApplicationBuilder UseConsul(this IApplicationBuilder app, IHostA
4242
var logger = loggingFactory.CreateLogger<IApplicationBuilder>();
4343

4444
// Get server IP address
45-
var features = app.Properties["server.Features"] as FeatureCollection;
46-
var addresses = features.Get<IServerAddressesFeature>();
47-
var address = addresses.Addresses.First();
45+
var address = consulConfig.Value.ServiceAddress;
46+
47+
if (string.IsNullOrWhiteSpace(address))
48+
{
49+
var features = app.Properties["server.Features"] as FeatureCollection;
50+
var addresses = features.Get<IServerAddressesFeature>();
51+
address = addresses.Addresses.First();
52+
53+
Console.WriteLine($"Could not find service address in config. Using '{address}'");
54+
}
4855

4956
// Register service with consul
5057
var uri = new Uri(address);
58+
var serviceName = consulConfig.Value.Name ?? AppDomain.CurrentDomain.FriendlyName.Trim().Trim('_');
5159
var registration = new AgentServiceRegistration()
5260
{
53-
ID = consulConfig.Value.Id ?? Guid.NewGuid().ToString(),
54-
Name = consulConfig.Value.Name ?? AppDomain.CurrentDomain.FriendlyName.Trim().Trim('_'),
61+
ID = $"{serviceName.ToLowerInvariant()}-{consulConfig.Value.Id ?? Guid.NewGuid().ToString()}",
62+
Name = serviceName,
5563
Address = uri.Host,
5664
Port = uri.Port,
5765
Tags = consulConfig.Value.Tags

Infrastructure/Consul/ConsulOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public class ConsulOptions
55
public string Id { get; set; }
66
public string Name { get; set; }
77
public string[] Tags { get; set; }
8-
public string Address { get; set; }
8+
public string ConsulAddress { get; set; }
9+
public string ServiceAddress { get; set; }
910
}
1011
}

0 commit comments

Comments
 (0)