.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 ############### ...
随机推荐
- java_25 FileReader类和FileWriter类
1.FileWriter 1.1FileWriter 用于写入字符流.要写入原始字节流,请考虑使用 FileOutputStream. public class Demo { public stati ...
- nginx 添加response响应头
硬添
- phpexcel 使用模板导出报表
function exportExcel($list, $filename, $indexKey = array()){ require_once(getcwd() . '../../app/Libs ...
- 基于IDEA工具 lombok 的使用
一.简介 Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO).它通过注解实现这一目的. 二.lombok的添加和常用注解 ...
- 201771010134杨其菊《面向对象程序设计(java)》第十三周学习总结
第十三周学习总结 第一部分:理论知识 第11章 事件处理(事件处理基础; 动作; 鼠标事件;AWT事件继承层次) 1. 事件源(event source):能够产生事件的对象都可 以成为事件源,如文本 ...
- Linux关闭防火墙命令
下面是red hat/CentOs7关闭防火墙的命令! 1:查看防火状态 systemctl status firewalld service iptables status 2:暂时关闭防火墙 s ...
- python3 第二十九章 - 内置函数之tuple相关
Python元组包含了以下内置函数 序号 方法及描述 实例 1 len(tuple)计算元组元素个数. >>> tuple1 = ('Google', 'Baidu', 'Taoba ...
- 非root用户安装cuda和cudnn
1.根据自己的系统在官网下载cuda (选择runfile(local)) https://developer.nvidia.com/cuda-downloads 2.进入下载目录,并执行 sh cu ...
- JavaScript Json(转)
JSON是JavaScript Object Notation的缩写,它是一种数据交换格式. 终于,在2002年的一天,道格拉斯·克罗克福特(Douglas Crockford)同学为了拯救深陷水深火 ...
- linux sort排序命令的高级用法(按多个列值进行排列)
http://www.jquerycn.cn/a_9076 在linux中,使用sort按行进行排序是很简单的.不过有时,生活总是爱抛给你一个一个的问题.如果使用sort按多个列值排列,同时使用tab ...