.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 ############### ...
随机推荐
- 区间dp(入门题)
区间dp:顾名思义就是在区间上进行动态规划,通过合并小区间求解一段区间上的最优解. 常见模板: for(int len=1;len<n;len++){//区间长度 for(int be=1;be ...
- vue的事件处理梳理
一.事件函数 1.v-on绑定click,执行一个函数 <input type="button" v-on:click="test" value=&quo ...
- 2018-2019-2 20165315 《网络对抗技术》Exp4 恶意代码分析
2018-2019-2 20165315 <网络对抗技术>Exp4 恶意代码分析 一.实验要求 1.系统运行监控 使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是 ...
- android中ScrollView嵌套ListView或GridView显示位置问题
Android中ScrollView中嵌套ListView或GridView时在开始进入界面时总是显示中间位置,开头的位置显示不出来.这种情况下只需要在ScrollView的父控件中添加以下两行代码即 ...
- docker--在centos镜像安装mysql
一.安装centos镜像 1.拉取最新版本centos镜像(拉取centos7 则使用centos:7即可) docker pull centos:lasted 2.查看已有镜像 docker ima ...
- 斗地主 ai的一些资料
zt https://programming.iteye.com/blog/1491470 https://blog.csdn.net/abc1234679/article/details/79458 ...
- 《Java并发编程的艺术》Java并发机制的底层实现原理(二)
Java并发机制的底层实现原理 1.volatile volatile相当于轻量级的synchronized,在并发编程中保证数据的可见性,使用 valotile 修饰的变量,其内存模型会增加一个 L ...
- mysql 1055
在 /etc/my.cnf 文件里加上如下: sql_mode=NO_ENGINE_SUBSTITUTION
- python3 第三十章 - 内置函数之Dictionary相关
Python字典包含了以下内置函数: 序号 函数及描述 实例 1 len(dict)计算字典元素个数,即键的总数. >>> dict = {'Name': 'cnblogs', 'A ...
- apache ab 结果Failed requests探究
Failed requests: 537 (Connect: 0, Receive: 3, Length: 268, Exceptions: 266) Receive:当客户端connect成功后,并 ...