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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM