当前位置: 移动技术网 > IT编程>开发语言>c# > .net core使用ocelot---第七篇 服务发现

.net core使用ocelot---第七篇 服务发现

2019年08月08日  | 移动技术网IT编程  | 我要评论
简介 .net core使用ocelot 第一篇 简单使用 .net core使用ocelot 第二篇 身份验证使用 .net core使用ocelot 第三篇 日志记录 .net core使用ocelot 第四篇 限流熔断 .net core使用ocelot 第五篇 服务质量 .net core使 ...

简介

    
   
   
   
   
   

         本文我们介绍用spring cloud eureka server介绍ocelot的服务发现模块。

什么是服务发现

服务发现是自动检测这些设备在计算机网络上提供的设备和服务。服务发现协议(sdp)是帮助完成服务发现的网络协议。服务发现旨在减少用户的配置工作。

在ocelot中我们可以使用许多的服务发现,比如consul, eureka等等。本文我使用eureka进行介绍。ocelot使用steeltoe与eureka进行通信,eureka是一个开源项目,使.net开发人员能够在云上构建弹性微服务时能实现满足行业标准的最佳实践。

我将使用ocelot的7.1.0-unstable0011版本向您展示此功能。

step1

         首先创建两个api服务,一个是默认的asp.net core web api项目。在api网关发现我们的api服务之前,我们需要在eureka服务上注册。

         在appsettings.json添加一些配置

1.    "spring": {  
2.        "application": {  
3.            "name": "service-a"  
4.        }  
5.    },  
6.    "eureka": {  
7.        "client": {  
8.            "serviceurl": "http://192.168.0.107:8761/eureka/",  
9.            "shouldfetchregistry": true,  
10.            "validatecertificates": false  
11.        },  
12.        "instance": {  
13.            "port": 9001,  
14.            "instanceid": "192.168.0.103:9001",  
15.            "hostname": "192.168.0.103",  
16.            "healthcheckurlpath": "/api/values/healthcheck",  
17.            "statuspageurlpath": "/api/values/info"                  
18.        }  
19.    }  

注意

  1. service-a 是ocelot发现服务的重要标志。
  2. serviceurl是eureka server的端点。
  3. 获得更多信息,看

  添加服务发现必要的代码

public class startup  
{  
    public startup(iconfiguration configuration)  
    {  
        configuration = configuration;  
    }  
  
    public iconfiguration configuration { get; }  
  
    public void configureservices(iservicecollection services)  
    {  
        services.adddiscoveryclient(configuration);  
        services.addmvc();  
    }  
  
    public void configure(iapplicationbuilder app, ihostingenvironment env)  
    {  
        if (env.isdevelopment())  
        {  
            app.usedeveloperexceptionpage();  
        }  
  
        app.usediscoveryclient();  
        app.usemvc();  
    }  
}  

  在这里,我将使用三个api服务来演示,两个api服务名称service-a具有不同的端口(9001和9003),一个api服务名称service-b。

step2

         新建一个apigateway项目,添加名为ocelot.json的配置文件。

{  
    "reroutes": [  
        {  
            "downstreampathtemplate": "/api/values",  
            "downstreamscheme": "http",  
            "upstreampathtemplate": "/a",  
            "useservicediscovery": true,  
            "servicename": "service-a",  
            "upstreamhttpmethod": [ "get" ],  
            "qosoptions": {  
                "exceptionsallowedbeforebreaking": 3,  
                "durationofbreak": 1000,  
                "timeoutvalue": 5000  
            },  
            "filecacheoptions": { "ttlseconds": 15 },  
            "loadbalanceroptions": {  
                "type": "roundrobin"  
            }  
        },  
        {  
            "downstreampathtemplate": "/api/values",  
            "downstreamscheme": "http",  
            "upstreampathtemplate": "/b",  
            "useservicediscovery": true,  
            "servicename": "service-b",  
            "upstreamhttpmethod": [ "get" ],  
            "qosoptions": {  
                "exceptionsallowedbeforebreaking": 3,  
                "durationofbreak": 1000,  
                "timeoutvalue": 5000  
            },  
            "filecacheoptions": { "ttlseconds": 15 }  
        }  
    ],  
    "globalconfiguration": {  
        "requestidkey": "ocrequestid",  
        "administrationpath": "/administration",  
        "servicediscoveryprovider": { "type": "eureka" }  
    }  
}  

  这里有几点需要注意。

  对于reroutes

  1. 将useservicediscovery设为true。
  2. 将servicename值设为在api服务里定义的服务名称。
  3. 不用指明downstreamhostandports的值。
  4. 将loadbalanceroptions设成roundrobin。

  对于globalconfiguration

         设置servicediscoveryprovider的type为eureka。这是使用eureka至关重要的配置。

         回到program.cs,我们需要让ocelot可以使用。

public class program  
{  
    public static void main(string[] args)  
    {  
        buildwebhost(args).run();  
    }  
  
    public static iwebhost buildwebhost(string[] args) =>  
        webhost.createdefaultbuilder(args)  
           .useurls("http://*:9000")  
           .configureappconfiguration((hostingcontext, config) =>  
            {  
                config  
                    .setbasepath(hostingcontext.hostingenvironment.contentrootpath)                        
                    .addjsonfile("ocelot.json")  
                    .addenvironmentvariables();  
            })  
           .configureservices(s =>  
            {  
                s.addocelot();  
            })  
            .configure(a =>  
            {  
                a.useocelot().wait();  
            })  
            .build();  
}  

step3

         让eureka跑起来。

  如你所见,eureka服务启动起来了,但是没有可以使用的实例。

注意

         为了在你的电脑上跑eureka服务,你可以按照下面的步骤尝试。

  1. 安装java 8 的jdk
  2. 安装maven3.x
  3. 复制spring cloud samples eureka 代码库()
  4. 转到eureka服务的目录(eureka)并使用mvn spring-boot:run启动它

step4

         我们如何注册我们的服务?只需运行我们的项目,我们就会得到service-a实例。

         当运行我们的api服务,你就会发现service-a 运行在eureka 服务里了

 

  好了,启动我们的apigateway,不幸的是,当我们运行起项目。

  查看异常信息,我们忘了在apigateway项目中配置eureka。在appsettings.json添加下面的配置。

"spring": {  
    "application": {  
        "name": "service-gw"  
    }  
},  
"eureka": {  
    "client": {  
"serviceurl": "http://192.168.0.107:8761/eureka/",  
        "shouldregisterwitheureka": false,  
        "validatecertificates": false  
    },  
    "instance": {  
        "port": 9000,  
        "instanceid": "192.168.0.103:9000",  
        "hostname": "192.168.0.103"  
    }  
}  

  重新启动apigateway,终于正常了。

  

  通过apigat访问service-a 

  通过apigateway访问未启动的service-b

  毫无疑问我们不可能访问service-b

  启动后

  再次启动

  最后注册另一个service-a

  访问service-a你得到的结果有时来自9001,有时来自9003 。因为我们负载均衡算法设为roundrobin。

  当我们停止service-a的一个服务,我们依然可以访问,但是终止的那个服务不能访问。

  源码在此

总结

         本文介绍了ocelot使用eureka实现服务发现的简单示例。当让还有consul也可以实现服务发现,下篇介绍。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网