consul启动 . http://192.168.1.6:8500/
#以server方式启动,UI可以访问,boot引导自己选为leader,网段内PC可访问 consul agent -server -ui -bootstrap -data-dir=data -client=0.0.0.0
建两个项目coreApi、coreApiClient 。分别nuget添加 Steeltoe.Discovery.Consul
在StartUp 配置服务 services.AddDiscoveryClient(Configuration);
appsettings.json 配置 coreApi 项目
"consul": { "host": "192.168.1.6", "port": 8500, "discovery": { "register": true, "serviceName": "coreApi", "preferIpAddress": true } }
coreApiClient 项目
"consul": { "host": "192.168.1.6", "port": 8500, "discovery": { "register": true, "serviceName": "coreApiClient", "preferIpAddress": true } }
coreApi项目提供服务
[HttpGet] public UsrDto getUsrDto() { var header = contextAccess.HttpContext.Request.Headers; return new UsrDto() { Age = 34, CreTime = DateTime.Now, Id = $"BaseDirectory={AppContext.BaseDirectory},ApplicationName={env.ApplicationName},EnvironmentName={env.EnvironmentName}", LoginAccount = configuration.GetSection("Urls").Value, Score = 9823.123456m }; } [HttpPost] public async Task< string> addUsr(UsrDto usr, string age) { var token = Request.Headers["Authorization"].ToString(); await Task.Delay(3 * 1000); return token; }
coreApiClient项目调用服务
public static class Ext { public static async Task<string> Get(this System.Net.Http.HttpClient httpClient, string uri, string token = "", string tokenKey = "Authorization") { using var request = new HttpRequestMessage(HttpMethod.Get, uri); request.Headers.TryAddWithoutValidation(tokenKey, token); var resp = await httpClient.SendAsync(request); var content = await resp.Content.ReadAsStringAsync(); return content; } public static async Task<string> Post(this System.Net.Http.HttpClient httpClient, string uri, object data, string token="", string tokenKey= "Authorization") { using var request = new HttpRequestMessage(HttpMethod.Post, uri); request.Headers.TryAddWithoutValidation(tokenKey, token); request.Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); var resp = await httpClient.SendAsync(request); var content = await resp.Content.ReadAsStringAsync(); return content; } }
[ApiController] [Route("[controller]/[action]")] public class WeatherController : ControllerBase { System.Net.Http.HttpClient httpClient; public WeatherController(Steeltoe.Discovery.IDiscoveryClient client) { Steeltoe.Common.Discovery.DiscoveryHttpClientHandler httpClientHandler = new Steeltoe.Common.Discovery.DiscoveryHttpClientHandler(client); httpClient = HttpClientHelper.GetHttpClient(httpClientHandler, 10 * 1000); //httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "123456"); } [HttpGet] public async Task< string> getUsrDtoProxy() { return await httpClient.Get("http://coreApi/usr/getUsrDto", "token"); } [HttpGet] public async Task<string> addUsrProxy() { return await httpClient.Post("http://coreApi/usr/addUsr?age=12" , new { Id = "hc", Age = 34, CreTime = DateTime.Now, Score = 23.34 } , "0987"); } }
运行两个项目,consul已经注册了
浏览器测试 coreApiClient 的两个接口
http://localhost:20001/Weather/getUsrDtoProxy 正常返回:
http://localhost:20001/Weather/addUsrProxy 浏览器等待3秒后 返回:0987
以上,只测试consul 服务注册与发现,get、post方式。
=======================================
下面,测试一下consul的配置管理 key/value
[HttpGet] public async Task< string> getConsulConfig(string key) { using ConsulClient client = new ConsulClient((i) => { i.Address = new Uri($"http://{configuration["consul:host"]}:{configuration["consul:port"]}"); }); var byteV = await client.KV.Get(key); return Encoding.UTF8.GetString(byteV.Response.Value); }
在consul配置 user、product,测试后可正常取出value。
如果是新建项目,配置项目可通过命令导出和导入:
#导出kv配置,从192.168.1.6节点,具体仅导出user项目,导出到文件consul-kv.json consul kv export -http-addr=http://192.168.1.6:8500 user > consul-kv.json #导入kv配置,从文件consul-kv.json,只导入文件中内容,不覆盖 consul kv import -http-addr=http://192.168.1.6:8500 @consul-kv.json
© 版权声明
文章版权归作者所有,未经允许请勿转载,侵权请联系 admin@trc20.tw 删除。
THE END