Update java.md
This commit is contained in:
parent
494827474e
commit
5e513f38f9
135
java.md
135
java.md
|
@ -1,21 +1,126 @@
|
|||
// Refer to document: https://github.com/nacos-group/nacos-examples/tree/master/nacos-spring-cloud-example/nacos-spring-cloud-config-example
|
||||
package com.alibaba.nacos.example.spring.cloud.controller;
|
||||
/*
|
||||
Demo for Basic Nacos Opreation
|
||||
App.csproj
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
<ItemGroup>
|
||||
<PackageReference Include="nacos-sdk-csharp" Version="${latest.version}" />
|
||||
</ItemGroup>
|
||||
*/
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/config")
|
||||
@RefreshScope
|
||||
public class ConfigController {
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Nacos.V2;
|
||||
using Nacos.V2.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@Value("${useLocalCache:false}")
|
||||
private boolean useLocalCache;
|
||||
class Program
|
||||
{
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
string serverAddr = "http://localhost:8848";
|
||||
string dataId = "application-prod.yml";
|
||||
string group = "DEFAULT_GROUP";
|
||||
|
||||
@RequestMapping("/get")
|
||||
public boolean get() {
|
||||
return useLocalCache;
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
services.AddNacosV2Config(x =>
|
||||
{
|
||||
x.ServerAddresses = new List<string> { serverAddr };
|
||||
x.Namespace = "cs-test";
|
||||
|
||||
// swich to use http or rpc
|
||||
x.ConfigUseRpc = true;
|
||||
});
|
||||
|
||||
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
var configSvc = serviceProvider.GetService<INacosConfigService>();
|
||||
|
||||
var content = await configSvc.GetConfig(dataId, group, 3000);
|
||||
Console.WriteLine(content);
|
||||
|
||||
var listener = new ConfigListener();
|
||||
|
||||
await configSvc.AddListener(dataId, group, listener);
|
||||
|
||||
var isPublishOk = await configSvc.PublishConfig(dataId, group, "content");
|
||||
Console.WriteLine(isPublishOk);
|
||||
|
||||
await Task.Delay(3000);
|
||||
content = await configSvc.GetConfig(dataId, group, 5000);
|
||||
Console.WriteLine(content);
|
||||
|
||||
var isRemoveOk = await configSvc.RemoveConfig(dataId, group);
|
||||
Console.WriteLine(isRemoveOk);
|
||||
await Task.Delay(3000);
|
||||
|
||||
content = await configSvc.GetConfig(dataId, group, 5000);
|
||||
Console.WriteLine(content);
|
||||
await Task.Delay(300000);
|
||||
}
|
||||
|
||||
internal class ConfigListener : IListener
|
||||
{
|
||||
public void ReceiveConfigInfo(string configInfo)
|
||||
{
|
||||
Console.WriteLine("receive:" + configInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Refer to document: https://github.com/nacos-group/nacos-sdk-csharp/tree/dev/samples/MsConfigApp
|
||||
Demo for ASP.NET Core Integration
|
||||
MsConfigApp.csproj
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="nacos-sdk-csharp.Extensions.Configuration" Version="${latest.version}" />
|
||||
</ItemGroup>
|
||||
*/
|
||||
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.Enrich.FromLogContext()
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("System", LogEventLevel.Warning)
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
|
||||
try
|
||||
{
|
||||
Log.ForContext<Program>().Information("Application starting...");
|
||||
CreateHostBuilder(args, Log.Logger).Build().Run();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<Program>().Fatal(ex, "Application start-up failed!!");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args, Serilog.ILogger logger) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureAppConfiguration((context, builder) =>
|
||||
{
|
||||
var c = builder.Build();
|
||||
builder.AddNacosV2Configuration(c.GetSection("NacosConfig"), logAction: x => x.AddSerilog(logger));
|
||||
})
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>().UseUrls("http://*:8787");
|
||||
})
|
||||
.UseSerilog();
|
||||
}
|
Loading…
Reference in New Issue