码迷,mamicode.com
首页 > 编程语言 > 详细

spring cloud + .net core实现微服务架构

时间:2018-06-11 18:57:45      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:settings   release   配置   ceo   bapi   pid   https   地址   sde   

1.新建spring boot项目

2.添加spring-cloud-starter-eureka-server依赖(需提供版本信息)

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>

3.设置程序属性信息

spring.application.name=myservicehub
server.port=5000

#强制不注册到注册服务器
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

#驱逐下线的服务,间隔,5秒,默认是60,建议开发和测试环境配置
#org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean.evictionIntervalTimerInMs
eureka.server.evictionIntervalTimerInMs=5000

4.新建.net core webapi,并安装Pivotal.Discovery.Client nuget包

Install-Package Pivotal.Discovery.Client
public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls("http://*:5001")
                .Build();
    }
public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDiscoveryClient(Configuration);
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
            app.UseDiscoveryClient();
        }
    }

 

5.appsettings.json中,注册服务到eureka-server

{
"Logging": {
    "IncludeScopes": false,
    "LogLevel": {
        "Default": "Warning"
    }
},
"spring": {
    "application": {
        "name": "serviceone"
    }
},
"eureka": {
  "client": {
    "serviceUrl": "http://localhost:5000/eureka/",
    "shouldFetchRegistry": false,
    "shouldRegisterWithEureka": true
  },
    "instance": {
        "port": 5001
    }
}
}

 

spring cloud + .net core实现微服务架构

标签:settings   release   配置   ceo   bapi   pid   https   地址   sde   

原文地址:https://www.cnblogs.com/wangyunjie/p/9168304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!