Skip to content

Commit

Permalink
Update Discovery sample
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-vmware committed Aug 30, 2024
1 parent 37b66ac commit 5c4006f
Show file tree
Hide file tree
Showing 25 changed files with 55 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Discovery/src/FortuneTeller/FortuneTeller.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FortuneTellerService", "FortuneTellerService\FortuneTellerService.csproj", "{57A04D00-FAC2-4AD1-847B-03E58A95CAED}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Samples.FortuneTellerService", "FortuneTellerService\Steeltoe.Samples.FortuneTellerService.csproj", "{57A04D00-FAC2-4AD1-847B-03E58A95CAED}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FortuneTellerWeb", "FortuneTellerWeb\FortuneTellerWeb.csproj", "{BAB80D02-6C31-4772-9C43-E08A309DA7B1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Samples.FortuneTellerWeb", "FortuneTellerWeb\Steeltoe.Samples.FortuneTellerWeb.csproj", "{BAB80D02-6C31-4772-9C43-E08A309DA7B1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1073526A-A0F2-4EE7-8EE5-6D95EA4ADBD1}"
ProjectSection(SolutionItems) = preProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FortuneTellerService.Models;
using Microsoft.AspNetCore.Mvc;
using Steeltoe.Samples.FortuneTellerService.Models;

namespace FortuneTellerService.Controllers;
namespace Steeltoe.Samples.FortuneTellerService.Controllers;

[ApiController]
[Route("api/[controller]")]
Expand All @@ -24,7 +24,9 @@ public ConfigurationLookupResult Get()
}

string? configServerUri = _configuration.GetValue<string?>("Spring:Cloud:Config:Uri");
_logger.LogInformation($"GET api/configuration: Resolved key '{ConfigurationKey}' to '{value}' from Config Server at '{configServerUri}'.");

_logger.LogInformation("GET api/configuration: Resolved key '{Key}' to '{Value}' from Config Server at '{Uri}'.", ConfigurationKey, value,
configServerUri);

return new ConfigurationLookupResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using FortuneTellerService.Data;
using FortuneTellerService.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Steeltoe.Samples.FortuneTellerService.Data;
using Steeltoe.Samples.FortuneTellerService.Models;

namespace FortuneTellerService.Controllers;
namespace Steeltoe.Samples.FortuneTellerService.Controllers;

[ApiController]
[Route("api/[controller]")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FortuneTellerService.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Steeltoe.Samples.FortuneTellerService.Models;

namespace FortuneTellerService.Data;
namespace Steeltoe.Samples.FortuneTellerService.Data;

public sealed class FortuneDbContext(DbContextOptions<FortuneDbContext> options) : DbContext(options)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using FortuneTellerService.Data;
using FortuneTellerService.Models;
using Steeltoe.Discovery.Eureka;
using Steeltoe.Discovery.Eureka;
using Steeltoe.Discovery.Eureka.AppInfo;
using Steeltoe.Samples.FortuneTellerService.Data;
using Steeltoe.Samples.FortuneTellerService.Models;

namespace FortuneTellerService;
namespace Steeltoe.Samples.FortuneTellerService;

internal sealed class DatabaseSeederHostedService(IServiceProvider serviceProvider, EurekaApplicationInfoManager appManager) : IHostedService
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Steeltoe.Common.HealthChecks;

namespace FortuneTellerService;
namespace Steeltoe.Samples.FortuneTellerService;

internal sealed class ExampleHealthContributor(HealthStatus? healthStatus) : IHealthContributor
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FortuneTellerService.Models;
namespace Steeltoe.Samples.FortuneTellerService.Models;

public sealed class ConfigurationLookupResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FortuneTellerService.Models;
namespace Steeltoe.Samples.FortuneTellerService.Models;

public sealed class Fortune
{
Expand Down
17 changes: 11 additions & 6 deletions Discovery/src/FortuneTeller/FortuneTellerService/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using FortuneTellerService;
using FortuneTellerService.Data;
using Microsoft.EntityFrameworkCore;
using Steeltoe.Common;
using Steeltoe.Common.Extensions;
using Steeltoe.Common.HealthChecks;
using Steeltoe.Configuration.CloudFoundry;
using Steeltoe.Configuration.CloudFoundry.ServiceBinding;
using Steeltoe.Configuration.ConfigServer;
using Steeltoe.Discovery.Consul;
using Steeltoe.Discovery.Eureka;
using Steeltoe.Management.Endpoint;
using Steeltoe.Samples.FortuneTellerService;
using Steeltoe.Samples.FortuneTellerService.Data;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

Expand All @@ -22,17 +24,20 @@
builder.Services.AddDbContext<FortuneDbContext>(options => options.UseInMemoryDatabase("Fortunes"));
builder.Services.AddHostedService<DatabaseSeederHostedService>();

// Steeltoe: Read credentials to Eureka server from VCAP_SERVICES on CloudFoundry.
builder.AddCloudFoundryConfiguration();
builder.Configuration.AddCloudFoundryServiceBindings();

// Steeltoe: Add service discovery clients for Consul and Eureka.
builder.Services.AddConsulDiscoveryClient();
builder.Services.AddEurekaDiscoveryClient();

// Steeltoe: Add Config Server to demonstrate discovery-first (resolves the URL to Config Server from Eureka).
builder.Configuration.AddConfigServer();

if (Platform.IsCloudFoundry)
{
// Steeltoe: Read credentials to Eureka server from VCAP_SERVICES on CloudFoundry.
builder.AddCloudFoundryConfiguration();
builder.Configuration.AddCloudFoundryServiceBindings();
}

// Steeltoe: Add actuator endpoints.
builder.AddAllActuators();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
applications:
- name: fortuneService
- name: fortune-service-sample
random-route: true
memory: 256M
stack: windows
Expand All @@ -12,4 +12,4 @@ applications:
DOTNET_CLI_TELEMETRY_OPTOUT: "true"
DOTNET_NOLOGO: "true"
services:
- myDiscoveryService
- sampleDiscoveryService
4 changes: 2 additions & 2 deletions Discovery/src/FortuneTeller/FortuneTellerService/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
applications:
- name: fortuneService
- name: fortune-service-sample
random-route: true
buildpacks:
- dotnet_core_buildpack
Expand All @@ -13,4 +13,4 @@ applications:
# Uncomment the following line to use Container-to-Container (C2C)
#Eureka__Instance__RegistrationMethod: "direct"
services:
- myDiscoveryService
- sampleDiscoveryService
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Diagnostics;
using FortuneTellerWeb.Models;
using FortuneTellerWeb.Services;
using Microsoft.AspNetCore.Mvc;
using Steeltoe.Samples.FortuneTellerWeb.Models;
using Steeltoe.Samples.FortuneTellerWeb.Services;

namespace FortuneTellerWeb.Controllers;
namespace Steeltoe.Samples.FortuneTellerWeb.Controllers;

public sealed class HomeController(FortuneService fortuneService) : Controller
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FortuneTellerWeb.Models;
namespace Steeltoe.Samples.FortuneTellerWeb.Models;

public class ErrorViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FortuneTellerWeb.Models;
namespace Steeltoe.Samples.FortuneTellerWeb.Models;

public sealed class FortuneModel
{
Expand Down
2 changes: 1 addition & 1 deletion Discovery/src/FortuneTeller/FortuneTellerWeb/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using FortuneTellerWeb.Services;
using Steeltoe.Configuration.CloudFoundry;
using Steeltoe.Configuration.CloudFoundry.ServiceBinding;
using Steeltoe.Discovery.Configuration;
using Steeltoe.Discovery.Consul;
using Steeltoe.Discovery.Eureka;
using Steeltoe.Discovery.HttpClients;
using Steeltoe.Samples.FortuneTellerWeb.Services;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FortuneTellerWeb.Models;
using Steeltoe.Samples.FortuneTellerWeb.Models;

namespace FortuneTellerWeb.Services;
namespace Steeltoe.Samples.FortuneTellerWeb.Services;

public sealed class FortuneService(HttpClient httpClient, ILogger<FortuneService> logger)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model FortuneModel?
@model Steeltoe.Samples.FortuneTellerWeb.Models.FortuneModel?
@{
ViewData["Title"] = "Home Page";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model ErrorViewModel
@model Steeltoe.Samples.FortuneTellerWeb.Models.ErrorViewModel
@{
ViewData["Title"] = "Error";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<title>@ViewData["Title"] - FortuneTellerWeb</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/FortuneTellerWeb.styles.css" asp-append-version="true" />
</head>
<body>
<header>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
@using FortuneTellerWeb
@using FortuneTellerWeb.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
applications:
- name: fortuneWeb
- name: fortune-web-sample
random-route: true
memory: 256M
stack: windows
Expand All @@ -12,4 +12,4 @@ applications:
DOTNET_CLI_TELEMETRY_OPTOUT: "true"
DOTNET_NOLOGO: "true"
services:
- myDiscoveryService
- sampleDiscoveryService
4 changes: 2 additions & 2 deletions Discovery/src/FortuneTeller/FortuneTellerWeb/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
applications:
- name: fortuneWeb
- name: fortune-web-sample
random-route: true
buildpacks:
- dotnet_core_buildpack
Expand All @@ -11,4 +11,4 @@ applications:
DOTNET_CLI_TELEMETRY_OPTOUT: "true"
DOTNET_NOLOGO: "true"
services:
- myDiscoveryService
- sampleDiscoveryService
9 changes: 5 additions & 4 deletions Discovery/src/FortuneTeller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ This variant uses service instances that are registered in [Spring Cloud Eureka]
1. Create a Eureka service instance in an org/space:
```
cf target -o your-org -s your-space
cf create-service p.service-registry standard myDiscoveryService
cf create-service p.service-registry standard sampleDiscoveryService
```
1. Wait for the service to become ready (you can check with `cf services`)
1. Run the `cf push` command from the FortuneTellerService directory, wait until it has started, then run it from the FortuneTellerWeb directory
Expand All @@ -147,10 +147,10 @@ This variant uses service instances that are registered in [Spring Cloud Eureka]
### Running on Tanzu Platform for Cloud Foundry (Container-to-Container)
Same steps as above, but uncomment the related line in `manifest.yaml` before push. Additionally:
1. In **Apps Manager**, go to **fortuneWeb** in your org/space
1. In **Apps Manager**, go to **fortune-web-sample** in your org/space
1. Go to **Container Networking** in the left-side menu
1. Click **CREATE POLICY**
1. Select the **fortuneService** app and specify **port** `8080`
1. Select the **fortune-service-sample** app and specify **port** `8080`
For more information, read the Cloud Foundry documentation on [container-to-container](https://docs.cloudfoundry.org/devguide/deploy-apps/cf-networking.html) and [custom ports](https://docs.cloudfoundry.org/devguide/custom-ports.html).
Expand Down Expand Up @@ -218,7 +218,8 @@ with Eureka at startup. FortuneTellerService is configured to obtain the URL to
```
1. Navigate to the [Eureka dashboard](http://localhost:8761/) in your browser and observe both "FORTUNESERVICE" and "CONFIGSERVER" are registered
- Detailed information about the registrations can be viewed using the [Eureka API](http://localhost:8761/eureka/apps)
1. Navigate to the [configuration endpoint](https://localhost:7251/api/configuration) in your browser and observe the configuration source is `http://host.docker.internal:8888/`. Without using discovery-first, the configuration source would have been `http://localhost:8888`.
1. Navigate to the [configuration endpoint](https://localhost:7251/api/configuration) in your browser and observe the configuration source is `http://host.docker.internal:8888/`.
Without using discovery-first, the configuration source would have been `http://localhost:8888`.
---
Expand Down

0 comments on commit 5c4006f

Please sign in to comment.