Steeltoe之Service Discovery篇
在前文一窥Spring Cloud Eureka中,已经构建了基于Eureka的服务端与客户端,可用于实现服务注册与发现功能。而借助Steeltoe的类库,可以在.NET生态系统中使用Spring Cloud的现有模块。
Package
对于ASP.NET Core,使用Steeltoe.Discovery.ClientCore
类库。
对于Console/ASP.NET 4.x,使用Steeltoe.Discovery.EurekaBase
类库。
服务发现
先建立一个ASP.NET Core项目,其可以找到已在Eureka的服务端注册的服务,并利用这些服务完成所需功能。
appsettings.json
首先设置Eureka服务端地址,并确定当前应用程序能够发现服务但本身不会被注册为服务。
{
"eureka": {
"client": {
"serviceUrl": "http://localhost:8765/eureka/",
"shouldFetchRegistry": true,
"shouldRegisterWithEureka": false
}
}
}
Startup.cs
加入DiscoveryClient服务并使用它。
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDiscoveryClient(Configuration);
services.AddMvc();
}
public void Configure(IApplicationBuilder app, ...)
{
app.UseMvc();
app.UseDiscoveryClient();
}
Controller
从已注册的Eureka客户端,即之前建立的Spring Cloud服务中获取数据。
public class HomeController : Controller
{
DiscoveryHttpClientHandler _handler;
public HomeController(IDiscoveryClient client)
{
_handler = new DiscoveryHttpClientHandler(client);
}
public IActionResult Index()
{
var client = new HttpClient(_handler, false);
var result = client.GetStringAsync("http://SPRINGCLOUD-EUREKA-CLIENT/hello").Result;
ViewData["message"] = result;
return View();
}
}
该服务的地址是它用于注册的application name。
启动ASP.NET Core应用程序,可以看到页面显示了来自Spring Cloud服务的数据。
服务注册
再建立一个ASP.NET Core API项目,并将其注册到Eureka的服务端。
appsettings.json
该应用程序不需要发现服务,但需要在Eureka服务端上注册服务。这里可以看到比上一项目更多的配置,因为它需要提供应用名称,端口号及主机名称。
{
"eureka": {
"client": {
"serviceUrl": "http://localhost:8765/eureka/",
"shouldFetchRegistry": false,
"shouldRegisterWithEureka": true
},
"instance": {
"appName": "NET-API",
"port": 5000,
"hostName": "localhost"
}
}
}
Startup.cs
与上一项目同样的配置。
Controller
建立一个简单的API方法。
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<string> Get()
{
return "Hello World NET-API!";
}
}
启动服务
可以在Eureka服务端中看到新注册的服务。
调用服务
将上一个项目中所调用API的地址改成新服务的。
public IActionResult Index()
{
var client = new HttpClient(_handler, false);
var result = client.GetStringAsync("http://NET-API/api/values").Result;
ViewData["message"] = result;
return View();
}
启动后,可以看到所显示的值已发生变化,因为其是从新的服务中取得的。
Steeltoe之Service Discovery篇的更多相关文章
- Service Discovery And Health Checks In ASP.NET Core With Consul
在这篇文章中,我们将快速了解一下服务发现是什么,使用Consul在ASP.NET Core MVC框架中,并结合DnsClient.NET实现基于Dns的客户端服务发现 这篇文章的所有源代码都可以在G ...
- Service Discovery with Apache Curator
Curator的介绍 Curator就是Zookeeper的一个客户端工具(不知道Zookeeper的同学可以到http://www.ibm.com/developerworks/cn/opensou ...
- Android WiFiDirect 学习(二)——Service Discovery
Service Discovery 简介 在Android WifiDirect学习(一 )中,简单介绍了如何使用WifiDirect进行搜索——连接——传输. 这样会有一个问题,那就是你会搜索到到附 ...
- Open-Source Service Discovery
Service discovery is a key component of most distributed systems and service oriented architectures. ...
- [译]Ocelot - Service Discovery
原文 你可以指定一个service discovery provider,ocelot将使用它来找下游的host和port. Consul 下面的配置要放在GlobalConfiguration中.如 ...
- ZooKeeper之service discovery
Zookeeper整体介绍 ZooKeeper is a centralized service for maintaining configuration information, naming, ...
- Service discovery
https://www.cnblogs.com/dirt2/p/5987067.html Use Assigned Numbers in the Service Discovery Protocol ...
- 【转帖】Service Discovery: 6 questions to 4 experts
https://highops.com/insights/service-discovery-6-questions-to-4-experts/ What’s Service Discovery? I ...
- Service Discovery in WCF 4.0 – Part 2 z
Service Discovery in WCF 4.0 – Part 2 In the previous post I discussed about the basic usage of WCF ...
随机推荐
- 转 可能是最漂亮的Spring事务管理
Snailclimb 2018年05月21日阅读 4246 可能是最漂亮的Spring事务管理详解 Java面试通关手册(Java学习指南):github.com/Snailclimb/… 微信阅读地 ...
- pdfcrop不能使用
最近,用到了pdfcrop,用来去除pdf中空白的边. 但是使用pdfcrop --margins 0 *.pdf 后,给出了错误: Error: pdfcrop cannot call ghost ...
- Git应用实践(二)
[时间:2017-08] [状态:Open] [关键词:Git,git diff, git apply, git format-patch, git am, git log] 0-背景 距上次总结Gi ...
- 解剖 Elasticsearch 集群 - 之一
解剖 Elasticsearch 集群 - 之一 本篇文章是一系列涵盖 Elasticsearch 底层架构和原型示例的其中一篇.在本篇文章中,我们会讨论底层的存储模型以及 CRUD(创建.读取.更新 ...
- 【css】css 中文字体 unicode 对照表
css 中文字体可以用 unicode 格式来表示,比如“宋体”可以用 \5B8B\4F53 来表示.具体参考下表: 中文名 英文名 unicode 宋体 SimSun \5B8B\4F53 黑体 S ...
- Java知多少(76)语言包(java.lang)简介
Java语言包(java.lang)定义了Java中的大多数基本类,由Java语言自动调用,不需要显示声明.该包中包含了Object类,Object类是整个类层次结构的根结点,同时还定义了基本数据类型 ...
- tensorflow模型量化
tensorflow模型量化/DATA/share/DeepLearning/code/tensorflow/bazel-bin/tensorflow/tools/graph_transforms/t ...
- 如何查看WAS生成的Snap.***.trc文件
WAS发生heapdump时随之还产生了javacore和Snap.***.trc文件 Snap.***.trc文件无法直接查看,需要对其进行格式化,就算用文本编辑器打开看见的也是有很多乱码 跟踪格式 ...
- Docker for Windows 代理设置(linux container)
https://blog.csdn.net/mzhangsf/article/details/79747979
- Javascript 检测键盘按键
Javascript中 有3个事件句柄在对应键盘的输入状态:keydown.keypress和keyup. 分别对应的意思是:按键被按下(按下按键但还没有抬起).点击按键(按下并抬起按键).按键抬起( ...