用nacos作為服務(wù)注冊中心,如何注冊.NetCore服務(wù),如何在Java中調(diào)用.NetCore服務(wù)呢?可以分為下面幾個步驟:
? 0.運(yùn)行nacos
? 1.開發(fā).net core服務(wù),然后調(diào)用nacos提供的.net core sdk注冊服務(wù)。
? 2.開發(fā)Java服務(wù),然后注冊服務(wù)。
? 3.用RestTemplate調(diào)用.net core服務(wù)。
? 4.用OpenFeign調(diào)用服務(wù)
? 下面來看具體步驟:
? 0.參考我之前的文章分布式配置nacos搭建踩坑指南(下)?,首先運(yùn)行nacos.
?1.首先開發(fā)一個.net core web api,我們返回的數(shù)據(jù)是天氣預(yù)報消息,新建一個WeatherForecastController,代碼如下:
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; ? namespace WebApi.Controllers { [ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; ? private readonly ILogger<WeatherForecastController> _logger; ? public WeatherForecastController(ILogger<WeatherForecastController> logger) { _logger = logger; } ? [HttpGet] public IEnumerable<WeatherForecast> Get() { var rng = new Random(); return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray(); } //public String Get() //{ // return "sunny"; //} ? } }
然后設(shè)置好訪問的url,在launchSettings.json的修改?"applicationUrl": "http://192.168.1.110:5000",注意這里去掉了https://192.168.1.110:5001,是為了避免在后面Java調(diào)用時需要證書的麻煩。
最后我們在cmd中輸入dotnet run,當(dāng)服務(wù)正常運(yùn)行起來后,在瀏覽器中輸入:http://192.168.1.110:5000/weatherforecast,發(fā)現(xiàn)成功返回天氣數(shù)據(jù),格式為json,截圖如下:
2.net core項(xiàng)目中引入nuget包:nacos-sdk-csharp,截圖如下:
文章來源:http://www.zghlxwxcb.cn/news/detail-798553.html
3.調(diào)用nacos-sdk-csharp,進(jìn)行服務(wù)注冊,代碼如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-798553.html
using System; using Microsoft.Extensions.DependencyInjection; using Nacos.V2; using Nacos.V2.Depe
到了這里,關(guān)于nacos實(shí)現(xiàn)Java和.NetCore的服務(wù)注冊和調(diào)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!