.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 ############### ...
随机推荐
- 100-days: twenty-two
Title: Why urban millennials love Uniqlo(优衣库) urban adj.都市的:具有城市或城市生活特点的; 市内; millennial n.千禧世代 mill ...
- 微信小程序记账本进度四
//index.wxml <view class="container"> <form catchsubmit="formSubmit" &g ...
- vue-实现全选单选
在获取列表页面数据时,通过forEach遍历存储数据的对象,给对象中添加一个selected变量,值为布尔值. 点击全选时,通过遍历将对象中selected的布尔值改变 点击单选时,被点中的通过筛选加 ...
- Vue-input框checkbox强制刷新
在引用input框的checkbox属性时,选中后会出现数据已经刷新,checkbox选中状态不会改变.这时在事件触发后可以调用this.$forceUpdate(),强制刷新页面解决这个问题. in ...
- linux之systemd---学习
linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 bootloader 载入内核,进行内核初始化.内核初始化的最后一步就是启动 PID 为 1 的 init 进 ...
- PCB设计基本流程
[PCB设计基本流程]1.准备原理图和网络表2.电路板规划3.参数设置4.导入网标5.布局6.布线7.规则检查与手工调整8.输出文件 [具体步骤]1.在原理图环境下:Tool——>Footpri ...
- js实现一个简单的登录页面
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- c语言01次作业--分支,顺序结构
C语言--第01次作业 1.1思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 本章学习让我体会良多.首先,不得不承认自己是一个非常马虎的人.常见的问题就是输出格式上常因为没有与题目要 ...
- javaMail的使用以及trying to connect to host "1xxx@163.com", port 25, isSSL false异常
最近项目用到邮件系统,开始了解javaMail...话不多说先上代码: pom依赖: <!-- 邮件 https://mvnrepository.com/artifact/javax.m ...
- vs2015 不能启动 iis express
删除以下目录的文件 <<path_to_solution_folder>>\.vs\config\applicationhost.config具体地址http://stacko ...