说明
服务注册与发现是一种动态管理服务实例的机制,其目的是让各个服务能够及时了解其他服务的状态与位置,以便能进行相互的通讯与协作。
依赖
添加 nacos2 的依赖 org.noear:nacos2-solon-cloud-plugin。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| dependencies { implementation platform(project(":demo-parent"))
implementation("org.noear:solon-web") implementation("org.noear:solon-logging-logback") implementation("org.noear:solon-openapi2-knife4j") implementation("org.noear:nacos2-solon-cloud-plugin") implementation("org.noear:solon-net-httputils") implementation("org.noear:nami-coder-snack3") implementation("org.noear:nami-channel-http")
annotationProcessor("org.mapstruct:mapstruct-processor:${mapstructVersion}")
testImplementation("org.noear:solon-test-junit5") }
|
配置
Nacos2 插件已经做了些默认值,所以我不需要过多的配置即可连接到 nacos 发现服务,如果使用了server 节点,意思是配置服务和发现服务使用同一个配置,或者单独使用solon.cloud.nacos.discovery.server配置。
1 2 3 4 5 6 7 8 9 10 11
| solon.app: name: "demo-cloud-config"
solon.cloud.nacos: server: "localhost:8848" config: load: "demo-cloud-config.yml"
|
服务调用
测试服务
测试服务中我们提供一个hello接口,并增加表示是从 Service01 的标识。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.example.demo.solon.controller;
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.noear.solon.annotation.Controller; import org.noear.solon.annotation.Mapping; import org.noear.solon.annotation.Param;
@Api("Demo") @Controller @Mapping public class DemoController { @ApiOperation("hello") @Mapping("/hello") public String hello(@Param(defaultValue = "world") String name) { return String.format("Hello %s!, By Service01", name); } }
|
DemoController
Solon 的 NamiClient 或者 HttpUtils 可以通过服务名来调用服务,其中NamiClient可以通过类似FeignClient的注解的方式进行使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package com.example.demo.solon.controller;
import com.example.demo.solon.client.DemoClient; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import java.io.IOException; import org.noear.nami.annotation.NamiClient; import org.noear.solon.annotation.Controller; import org.noear.solon.annotation.Mapping; import org.noear.solon.annotation.Param; import org.noear.solon.net.http.HttpUtils;
@Api("Demo") @Controller @Mapping public class DemoController { @NamiClient private DemoClient demoClient;
@ApiOperation("hello") @Mapping("/hello") public String hello(@Param(defaultValue = "world") String name) { try { return HttpUtils.http("demo-cloud-service01", "/hello").data("name", name).get(); } catch (IOException e) { throw new RuntimeException(e); } }
@ApiOperation("hello2") @Mapping("/hello2") public String hello2(@Param(defaultValue = "world") String name) { return demoClient.hello(name); } }
|
DemoClient
1 2 3 4 5 6 7 8 9 10 11
| package com.example.demo.solon.client;
import org.noear.nami.annotation.NamiClient;
@NamiClient(name = "demo-cloud-service01") public interface DemoClient { String hello(String name); }
|
验证
运行 demo-cloud-discovery 和 demo-cloud-service01,我们可以通过 nacos 看到服务到已经注册上来了。

接下来通过 hello 和 hello2 接口测试 两种不同方式的调用。
通过 httputils 调用服务

通过 NamiClient

小结
通过简单的配置,我们就可以把服务注册的 Nacos 中,并可以通过服务名的方式调用其他服务。当我们在普通的服务中增加服务配置和服务注册与发现功能配置后,就可以算作系统中的一个分布式微服务了,可以关注于业务的开发了。而 Solon Cloud 的接下来的更多的面向系统的可用性,可维护性等。