.net framework 4.5 +steeltoe+ springcloud(二) 实现服务发现与调用功能
spring.application.name=java-service
server.port=3222
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
package com.ty.democlient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class DemoclientApplication {
public static void main(String[] args) {
SpringApplication.run(DemoclientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
}




{
"spring": {
"application": {
"name": "demo_netfetch"
}
},
"eureka": {
"client": {
"serviceUrl": "http://localhost:8761/eureka/",
"shouldFetchRegistry": true,
"shouldRegisterWithEureka": true,
"validate_certificates": false
},
"instance": {
"port":
// Remove comments to enable SSL requests
// More changes in Program.cs are required if using direct C2C communications
//,"securePortEnabled": true
}
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"Pivotal": "Debug",
"Steeltoe": "Debug"
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Extensions.Logging;
using Steeltoe.Common.Discovery;
namespace DemoNetFetchClient.Services
{
public class FetchServise : IFetchServise
{
DiscoveryHttpClientHandler _handler;
private const string RANDOM_FORTUNE_URL = "http://java-service/hi?name=tian";//服务中心已注册的服务地址
private ILogger<FetchServise> _logger;
public FetchServise(IDiscoveryClient client, ILoggerFactory logFactory = null)
{
_handler = new DiscoveryHttpClientHandler(client);
_logger = logFactory?.CreateLogger<FetchServise>();
}
public async Task<string> RandomFortuneAsync()
{
_logger?.LogInformation("RandomFortuneAsync");
var client = GetClient();
return await client.GetStringAsync(RANDOM_FORTUNE_URL);
}
private HttpClient GetClient()
{
var client = new HttpClient(_handler, false);
return client;
}
}
}
public class HomeController : Controller
{
IFetchServise _fortunes;
ILogger<HomeController> _logger;
public HomeController(IFetchServise fortunes, ILoggerFactory logFactory = null)
{
_fortunes = fortunes;
_logger = logFactory?.CreateLogger<HomeController>();
}
public async System.Threading.Tasks.Task<ActionResult> Index()
{
ViewBag.Message = await _fortunes.RandomFortuneAsync();
return View();
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ApplicationConfig.RegisterConfig("development");
var builder = new ContainerBuilder();
// Add Microsoft Options to container
builder.RegisterOptions();
// Add Microsoft Logging to container
builder.RegisterLogging(ApplicationConfig.Configuration);
// Add Console logger to container
builder.RegisterConsoleLogging();
// Register all the controllers with Autofac
builder.RegisterControllers(typeof(MvcApplication).Assembly);
// Register IDiscoveryClient, etc.
builder.RegisterDiscoveryClient(ApplicationConfig.Configuration);
// Register FortuneService
builder.RegisterType<FetchServise>().As<IFetchServise>().SingleInstance();
// Create the Autofac container
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// Get a logger from container
var logger = container.Resolve<ILogger<MvcApplication>>();
logger.LogInformation("Finished container build, starting background services");
// Start the Discovery client background thread container.StartDiscoveryClient();
logger.LogInformation("Finished starting background services");
}

.net framework 4.5 +steeltoe+ springcloud(二) 实现服务发现与调用功能的更多相关文章
- 浅谈SpringCloud (二) Eureka服务发现组件
上面学习到了如何由一个程序访问另一个程序,那么如果使用SpringCloud来进行访问,该如何访问呐? 可以借助Eureka服务发现组件进行访问. 可以借助官方文档:https://spring.io ...
- SpringCloud使用Nacos服务发现实现远程调用
本文使用SpringCloud结合Nacos服务发现,Feign远程调用做一个简单的Demo. 1 Nacos 关于Nacos之前写了两篇文章关于SpringBoot对它的使用,感兴趣可以查看一下. ...
- .net framework 4.5 +steeltoe+ springcloud(三)实现Hystrix断路器
在基于.net framework的服务客户端实现断路器功能,基本项目创建步骤可以参照我的另一篇发现和调用服务的笔记,地址:http://www.cnblogs.com/troytian/p/8621 ...
- SpringCloud接入EDAS——服务发现篇
旁白 很久没有写技术文章了,最近不是写水文就是写小说.说到底,还是最近很少研究技术的缘故,已经到了江郎才尽的地步了. 不过,LZ无意间看到自己团队的小伙伴写的一些文章,觉得还是不错的,于是便动了心思, ...
- SpringCloud之Nacos服务发现(十七)
一 Nacos简介 Nacos是以服务为主要服务对象的中间件,Nacos支持所有主流的服务发现.配置和管理. Nacos主要提供以下四大功能: 服务发现与服务健康检查 Nacos使服务更容易注册自己并 ...
- HttpClientFactory与Steeltoe结合来完成服务发现
前言 上一篇说了一下用HttpClientFactory实现了简单的熔断降级. 这篇就来简单说说用HttpClientFactory来实现服务发现.由于标题已经好明显的说了Steeltoe 因此这里会 ...
- .net framework 4.5 +steeltoe+ springcloud 实现服务注册功能
首先得先了解并熟悉一下springcloud,并手动去搭建一个服务中心,具体可度娘教程. 如果是.net core的话,实现注册也是没有问题的,网上教程很多,可自行度娘. 最难的就是基于Framewo ...
- springcloud(二) 微服务架构编码构建
微服务架构编码构建 1 基础知识 1.1 版本 2 微服务cloud整体聚合父工程Project 2.1 new project 2.2 字符编码设置 utf-8 2.3 pom.xml 2.4 父工 ...
- 第二章 SpringCloud之Eureka-Server服务发现组件
1.Eureka简介 文档:https://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html ############### ...
随机推荐
- 微信小程序记账本进度五
//index.wxss.account-detail{ height: 100rpx; padding: 0 30rpx; } .account-amount{ padding: 0 30rpx; ...
- 记录一次程序输出和DB查询不匹配的问题
今天发生一件很神奇的事情,我用TP读取DB数据,然后打印出来的数据,和直接通过sequal pro查询出来的数据(某一列),怎么对都对不起来,我尝试 清空TP缓存 MYSQL服务重启 mac重启 都无 ...
- django后台对某些字段设置颜色
在model.py的class函数中添加如下代码 #对签收状态设置颜色 def color_state(self): ': assign_state_name = '待签收' color_code = ...
- oracle 中 某个字段的长度不够的sql 语句
alter table 表名 modify(字段名 类型) 之前字段type 的长度为50 现在改为100 举例 : alter table psp_model modify(type var ...
- hive上传数据到oracle
# Oracle 建立hive外部表 使用oracle的大数据连接器向建立hive的外部表,在通过在oracle中根据外部表建立内部表(create table as select * from ex ...
- Python __dict__属性详解
本文转载自 https://www.cnblogs.com/alvin2010/p/9102344.html 感谢 //偏执 大佬 我们都知道Python一切皆对象,那么Python究竟是怎么管理对象 ...
- Visual Studio连接Oracle数据库
一.安装Oracle Developer Tools for Visual Studio 2015 其他的什么client一概不要装,装了的直接卸载. 下载时需要登录,如果之前已经注册账号,提醒一下密 ...
- DataTables warning : Requested unknown parameter '0' from the data source for row 0错误
在做datatables的项目,从后台取得数据后,返回给datatables界面时会报下面的错误: DataTables warning : Requested unknown parameter ' ...
- js 选择指定区域
/根据id 选择特定区域function SelectRange(id) { var div = document.getElementById(id); var controlRange; if ( ...
- 单点登录的三种实现方式 转自: https://blog.csdn.net/python_tty/article/details/53113612
单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任.单点登录在大型网站里使用得 ...