.net core+Spring Cloud学习之路 二
前言:
原本计划这次写一下搭建eureka群集。但是发现上次写的只是服务的注册,忘了写服务的发现,所以这次先把服务发现补上去。
- 我们基于上篇文章,再新建两个.net core web api项目,分别起名为order_one,order_two, 作为两个订单服务。我们以order_one为例。
- 同理先使用nuget添加Pivotal.Discovery.ClientCore库。
- Startup.cs 中添加
public void ConfigureServices(IServiceCollection services)
{
// services.AddDiscoveryClient(Configuration);
services.AddDiscoveryClient(Configuration);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseDiscoveryClient();
app.UseHttpsRedirection();
app.UseMvc();
}需要using Pivotal.Discovery.Client;
- appsettings.json 添加eureka服务配置
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"spring": {
"application": {
"name": "order"
}
},
"eureka": {
"client": {
"serviceUrl": "http://localhost:8888/eureka/",
"shouldFetchRegistry": true
},
"instance": {
"port": ,
"hostName": "localhost"
}
}
} - 修改launchSettings.json 端口改为5001
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5001/",
"sslPort":
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Order_One": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/order",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5001/"
}
}
} - 添加一个控制器,返回order one
[Route("api")]
public class ValuesController : Controller
{ // GET api/values
[HttpGet("value")]
public string Get()
{
return "Order One";
}
} - 再建一个同样的项目,order_two
- 上述项目创建完成后,我们先启动spring cloud项目。然后同时启动三个.net core项目。这个时候我们刷新一下spring cloud页面。
这个时候发现名为order的有两个服务。
- 修改一下orderserver的控制器代码
[Route("api")]
public class ValuesController : Controller
{ DiscoveryHttpClientHandler _handler; private const string GET_SERVICES_URL = "http://order/api/value";
private ILogger<ValuesController> _logger; public ValuesController(IDiscoveryClient client, ILoggerFactory logFactory = null)
{
_handler = new DiscoveryHttpClientHandler(client);
_logger = logFactory?.CreateLogger<ValuesController>();
} [HttpGet("order")]
public async Task<string> GetServices()
{
_logger?.LogInformation("GetServices");
var client = GetClient();
return await client.GetStringAsync(GET_SERVICES_URL); } private HttpClient GetClient()
{
var client = new HttpClient(_handler, false);
return client;
} }然后再次启动这三个.net core项目,并访问http://localhost:5000/api/order,如图,他成功返回了order two,多刷新几次会发现返回的order One 和 order two是来回变的。


说明eureka服务中心帮我们实现了负载均衡。
- 这个时候,我们可以把order_one 或者 order_two关掉一个。我们再次访问http://localhost:5000/api/order会出现一次错误,然后eureka会自动把有问题的服务踢掉(时间可配置),再次访问不再有问题。
参考资料:
总结
现在网络上类似这样的文章很多,自己再单独写一份就是为了做个笔记,跟各位大牛交流一下,自己也学习学习。
.net core+Spring Cloud学习之路 二的更多相关文章
- .net core+Spring Cloud学习之路 一
文章开头唠叨两句. 2019年了,而自己参加工作也两年有余了,用一个词来概括这两年多的生活,就是:“碌碌无为”. 也不能说一点收获都没有,但是很少.2019来了,我立志要打破现状,改变自己,突破自我. ...
- Spring Cloud进阶之路 | 二:服务提供者(discovery)
1 创建父项目 以前文所述,以spring boot 2.1.7.RELEASE为基,spring cloud版本为Greenwich.SR2,spring cloud alibaba版本为2.1.0 ...
- Spring Cloud 学习笔记(二)——Netflix
4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...
- Spring Boot 学习之路二 配置文件 application.yml
一.创建配置文件 如图所示,我们在resources文件夹中新建配置文件application.yml 结构图 二.一些基本配置 server: port: 8090 //配置端口 session ...
- Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)
Ribbon 是 Netflix 发布的开源项目,主要功能是为 REST 客户端实现负载均衡.它主要包括六个组件: ServerList,负载均衡使用的服务器列表.这个列表会缓存在负载均衡器中,并定期 ...
- Spring Cloud gateway 网关服务二 断言、过滤器
微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...
- spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法
turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...
- Spring框架学习之IOC(二)
Spring框架学习之IOC(二) 接着上一篇的内容,下面开始IOC基于注解装配相关的内容 在 classpath 中扫描组件 <context:component-scan> 特定组件包 ...
- 搭建高可用服务注册中心-Spring Cloud学习第一天(非原创)
文章大纲 一.Spring Cloud基础知识介绍二.创建单一的服务注册中心三.创建一个服务提供者四.搭建高可用服务注册中心五.项目源码与参考资料下载六.参考文章 一.Spring Cloud基础 ...
随机推荐
- 小菜鸟从0基础开始学Linux系统
随着当今信息时代的迅速发展,Linux凭借其诸多优势从操作系统中脱颖而出,受到越来越多电脑用户的青睐.Linux是一个集安全.稳定.自由等众多优点于一身的操作系统,不可思议的是这么好的系统还是免费的! ...
- python小总结3(异常、单例设计模式)
一.异常 AttributeError:试图访问一个对象没有的成员[属性和方法] ValueError:值错误,传入了一个不期望的值 ImportError:无法导入模块或者包:基本上路径问题 Ind ...
- centos 7添加硬盘及LVM扩容
一堆概念 分区工具 fdisk:支持MBR,对GPT支持的不好 parted:支持GPT partprobe(centos 5/6).partx(centos 7)通知内核强制重读磁盘分区表 分区表 ...
- 7. Reverse Integer Add to List★
题目内容: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 题目分 ...
- AnnotationTransactionAttributeSource is only available on Java 1.5 and higher
前言: 在eclipse中用到spring2.0的web项目,启动elipse自带的tomcat7,tomcat7报错如下: AnnotationTransactionAttributeSource ...
- 指导手册06:HBase安装部署
指导手册06:HBase安装部署 配置环境 1.参考文件: https://www.cnblogs.com/lzxlfly/p/7221890.html https://www.cnblogs.com ...
- ImitateUCM项目启动Tomcat的过程
启动各种报错:什么source not found ....然而解决后发现都不是问题关键 下面我讲下我的理解(也许这也不是关键,但是通过该这些,我的问题解决了) (1)重新部署Tomcat时,要同步更 ...
- 【Shell】单行注释和多行注释
单行注释 '# ' # echo "hello" 多行注释 方法1 : << ! 这是注释1 这是注释2 这是注释3 ! 方法2 :' 这是注释1 这是注释2 这是注释 ...
- Intellij idea 2018的注册方式
激活方式:License Server 第一步: 将地址 http://active.chinapyg.com/ 或者 http://idea.toocruel.net 任意一个复制到License ...
- python爬虫基础_requests和bs4
这些都是笔记,还缺少详细整理,后续会更新. 下面这种方式,属于入门阶段,手动成分比较多. 首先安装必要组件: pip3 install requests pip3 install beautifuls ...