nacos实现Java和.NetCore的服务注册和调用

用nacos作为服务注册中心,如何注册.NetCore服务,如何在Java中调用.NetCore服务呢?可以分为下面几个步骤:

  0.运行nacos

  1.开发.net core服务,然后调用nacos提供的.net core sdk注册服务。

  2.开发Java服务,然后注册服务。

  3.用RestTemplate调用.net core服务。

  4.用OpenFeign调用服务

 

  下面来看具体步骤:

  0.参考我之前的文章分布式配置nacos搭建踩坑指南(下) ,首先运行nacos.

 1.首先开发一个.net core web api,我们返回的数据是天气预报消息,新建一个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 _logger;
​
        public WeatherForecastController(ILogger logger)
        {
            _logger = logger;
        }
​
        [HttpGet]
        public IEnumerable 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";
        //}
​
    }
}

然后设置好访问的url,在launchSettings.json的修改 "applicationUrl": "http://192.168.1.110:5000",注意这里去掉了https://192.168.1.110:5001,是为了避免在后面Java调用时需要证书的麻烦。

最后我们在cmd中输入dotnet run,当服务正常运行起来后,在浏览器中输入:http://192.168.1.110:5000/weatherforecast,发现成功返回天气数据,格式为json,截图如下:

nacos实现Java和.NetCore的服务注册和调用插图

2.net core项目中引入nuget包:nacos-sdk-csharp,截图如下:

nacos实现Java和.NetCore的服务注册和调用插图1

3.调用nacos-sdk-csharp,进行服务注册,代码如下:

using System;
using Microsoft.Extensions.DependencyInjection;
using Nacos.V2;
using Nacos.V2.DependencyInjection;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace NacosDiscoveryProviderNetCoreTest1
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string serverAddr = "http://localhost:8848";
            string dataId = "config2";
            string group = "DEFAULT_GROUP";
​
            IServiceCollection services = new ServiceCollection();
            //register service 
            services.AddNacosV2Naming(
                 x =>
                 {
                     x.ServerAddresses = new Liststring>() { serverAddr };
                     //x.ConfigUseRpc = true;
​
                 }
                );
            IServiceProvider serviceProvider = services.BuildServiceProvider();
            var namingSvc = serviceProvider.GetService();
            await namingSvc.RegisterInstance("weatherforecast", "192.168.1.110", 5000);
            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(await namingSvc.GetAllInstances("weatherforecast")));
            Console.ReadKey();
​
​
        }
    }
}

我们进入nacos后台,如果服务注册成功,我们就会在服务列表中看到weatherforecast服务了,如下所示:

nacos实现Java和.NetCore的服务注册和调用插图2

 

有两个地方必须切记注意:

1).namingSvc.RegisterInstance("weatherforecast", "192.168.1.110", 5000);是一句很关键的代码,意思是注册一个名为weatherforecast,地址为:192.168.1.110,端口为:5000的服务。

2)launchSettings.json里的applicationUrl必须去掉包含https的设置,只保留http的设置,即只保留:"applicationUrl": "http://192.168.1.110:5000",否则在Java中调用会报证书错误。

 

4.参考nacos服务注册,利用阿里巴巴Spring boot脚手架,引入:spring-boot-starter-web,spring-cloud-starter-alibaba-nacos-discovery,spring-cloud-starter,spring-boot-starter-test,spring-cloud-starter-loadbalancer,spring-cloud-starter-openfeign。完整的pom如下:

xml version="1.0" encoding="UTF-8"?>
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    modelVersion>4.0.0modelVersion>
    parent>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-parentartifactId>
        version>2.6.11version>
        relativePath/> 
    parent>
    groupId>com.alibaba.cloudgroupId>
    artifactId>nocos-discovery-consumer-sampleartifactId>
    version>0.0.1-SNAPSHOTversion>
    name>nocos-discovery-consumer-samplename>
    description>Demo project for Spring Bootdescription>properties>
        java.version>1.8java.version>
        spring-cloud-alibaba.version>2021.0.4.0spring-cloud-alibaba.version>
        spring-cloud.version>2021.0.4spring-cloud.version>
    properties>dependencies>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>
        dependency>
            groupId>com.alibaba.cloudgroupId>
            artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        dependency>
            groupId>org.springframework.cloudgroupId>
            artifactId>spring-cloud-starterartifactId>
        dependency>dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-testartifactId>
            scope>testscope>
        dependency>
          dependency>
    groupId>org.springframework.cloudgroupId>
    artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
          dependency>
            groupId>org.springframework.cloudgroupId>
            artifactId>spring-cloud-starter-loadbalancerartifactId>
            version>3.0.1version>
        dependency>
        dependency>
    groupId>com.alibabagroupId>
    artifactId>fastjsonartifactId>
    version>1.2.47version>
dependency>
​
​
​
dependency>
groupId>org.netbeans.externalgroupId>
artifactId>org-apache-commons-httpclientartifactId>
version>RELEASE126version>
dependency>
​
​
    dependencies>dependencyManagement>
        dependencies>
            dependency>
                groupId>org.springframework.cloudgroupId>
                artifactId>spring-cloud-dependenciesartifactId>
                version>${spring-cloud.version}version>
                type>pomtype>
                scope>importscope>
            dependency>
            dependency>
                groupId>com.alibaba.cloudgroupId>
                artifactId>spring-cloud-alibaba-dependenciesartifactId>
                version>${spring-cloud-alibaba.version}version>
                type>pomtype>
                scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>build>
        plugins>
            plugin>
                groupId>org.apache.maven.pluginsgroupId>
                artifactId>maven-compiler-pluginartifactId>
                version>3.8.1version>
                configuration>
                    source>1.8source>
                    target>1.8target>
                    encoding>UTF-8encoding>
                configuration>
            plugin>
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>project>

5.application.properties的设置同前面的文章里介绍的设置一样,代码如下所示:

spring.application.name=nocos-discovery-consumer-sample
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.namespace=public
spring.main.allow-circular-references=true
server.port=9091

 

6.新建一个名为WeatherService的接口,代码如下:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
​
​
@FeignClient("weatherforecast") 
@LoadBalancerClient("weatherforecast")
public interface WeatherService {
​
  @GetMapping("/Weatherforecast")
  public String getWeather();

}

 

 7.新建一个RestTemplateController,代码如下:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
​
import com.alibaba.fastjson.JSONObject;
​
import org.springframework.context.annotation.Bean;
import org.apache.commons.httpclient.methods.GetMethod;
//import org.apache.http.client.HttpClient;
import org.apache.commons.httpclient.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import java.io.*;
​
​
​
@RestController
public class RestTemplateController {
​
   //@LoadBalanced
   @Autowired
   public RestTemplate resttemplate;

   //@LoadBalanced
   @Bean
   public RestTemplate restTemplate() {
     return new RestTemplate();
   }

   @Autowired
   private EchoService echoService;

   @Autowired
   private WeatherService weatherService;

   @Autowired DiscoveryClient discoveryClient;

   //resttemplate test
   @GetMapping("/call/echo") 
   public String callEcho() { 

     System.out.println("callEcho");

     ServiceInstance serviceInstance=discoveryClient.getInstances("weatherforecast").get(0);
     System.out.println("Host is: "+serviceInstance.getHost()+" ,port is: "+serviceInstance.getPort());
         String urlString=serviceInstance.getHost()+":"+serviceInstance.getPort()+"/weatherforecast";   
     urlString="http://"+urlString;     
     //RestTemplate test
     return resttemplate.getForObject(urlString, String.class);

   }

   //openFeign test
   @GetMapping("/getWeather")
   public String getWeather() {

     return weatherService.getWeather();
​
   }
}

其中要注意的几点:

1) ServiceInstanceserviceInstance=discoveryClient.getInstances("weatherforecast").get(0);是一句关键的代码,用于获取weatherforecast服务的实例。

2)callEcho()是调用RestTemplage访问netcore服务

3)getWeather是调用openFeiign访问netcore服务

 

8.启动类代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
​
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NocosDiscoveryConsumerSampleApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(NocosDiscoveryConsumerSampleApplication.class, args);
    }
​
}

9.运行,访问http://192.168.1.110:5000/weatherforecast和http://localhost:9091/getWeather:

nacos实现Java和.NetCore的服务注册和调用插图3

 

nacos实现Java和.NetCore的服务注册和调用插图4

 

文章来源于互联网:nacos实现Java和.NetCore的服务注册和调用

THE END
分享
二维码