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

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.在启动类添加注解

@EnableEurekaServer
@SpringBootApplication
public class ServicehubApplication {}

 

5.新建.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();
        }
    }

 

6.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
    }
}
}

 

posted @ 2018-06-11 17:51  nerocloud  阅读(1773)  评论(0编辑  收藏  举报