文章开头唠叨两句.
2019 年了, 而自己参加工作也两年有余了, 用一个词来概括这两年多的生活, 就是:"碌碌无为".
也不能说一点收获都没有, 但是很少. 2019 来了, 我立志要打破现状, 改变自己, 突破自我. 现在明白了只有不断的学习才能不被淘汰, 只有不断的学习才能拿到更高的工资.
言归正传, 在今早发现张队在元旦前的博客 "年末展望: Oracle 对 JDK 收费和. NET Core 给我们的机遇", 说明了以后. net core 会越来越好, 而我本人在 2017 年的时候开始接触过. net core 的时候, 就没有放弃过对. net core 的学习, 现在觉得微服务是个不错的方向, 而自己也在学习这块的东西, 所以写个博客记录一下自己的学习的笔记.
我采用的是 java 的 Spring cloud + .net core.
先打开 idea 创建一个 Spring boot 项目, 在 pom 文件中添加如下 maven 依赖:
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Dalston.SR3</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-config</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka-server</artifactId>
- </dependency>
- </dependencies>
下载 eureka 需要的 jar 包.
新建一个《application.properties》配置文件, 添加如下几行配置:
- server.port=8888 #客户端默认情况下会将自己注册到一个 8761 的端口, 我们修改端口为 8888
- eureka.client.register-with-eureka=false #默认会把自己当作一个客户端注册到 eureka 服务, 把他设置为 false.
- eureka.client.fetch-registry=false #默认去抓取注册信息, 自己本身是一个 eureka 服务器, 没必要去抓取, 应该是客户端去抓取, 所以把这个配置设置为 false.
添加一个启动类.
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.builder.SpringApplicationBuilder;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
- @SpringBootApplication
- @EnableEurekaServer
- public class ServerApp {
- public static void main(String[] args){
- new SpringApplicationBuilder(ServerApp.class).web(true).run(args);
- }
- }
启动这个启动类, 然后浏览器里输入: http://localhost:8888(8888 是我 spring boot 的端口), 然后看到如下页面, 这个时候说明 Spring boot eureka 服务启动成功.
打开 vs, 创建一个. net core Web API 项目 , 使用 nuget 添加 Pivotal.Discovery.Client 库.
Startup.cs 中添加
- public void ConfigureServices(IServiceCollection services)
- {
- // services.AddDiscoveryClient(Configuration);
- services.AddDiscoveryClient(Configuration);
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
- }
- public void Configure(IApplicationBuilder App, IHostingEnvironment env,ILoggerFactory loggerFactory)
- {
- loggerFactory.AddConsole(Configuration.GetSection("Logging"));
- loggerFactory.AddDebug();
- if (env.IsDevelopment())
- {
- App.UseDeveloperExceptionPage();
- }
- else
- {
- App.UseHsts();
- }
- App.UseDiscoveryClient();
- App.UseHttpsRedirection();
- App.UseMvc();
- }
需要 using Pivotal.Discovery.Client;
修改端口,
打开 launchSettings.JSON
修改端口为 5000
- "Demo": {
- "commandName": "Project",
- "launchBrowser": true,
- "launchUrl": "api/values",
- "applicationUrl": "http://localhost:5000",
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
- }
打开 appsettings.JSON 添加 eureka 服务配置
- {
- "Logging": {
- "LogLevel": {
- "Default": "Warning"
- }
- },
- "spring":{
- "application":{
- "name":"serviceone"
- }
- },
- "eureka":{
- "client":{
- "serviceUrl":"http://localhost:8888/eureka",
- "shouldFetchRegistry":false,
- "shouldRegisterWithEureka":true
- },
- "instance":{
- "port":5000
- }
- }
- }
到此为止我们算是配置完成了一个简单的注册服务的小 demo, 我们先启动 eureka 服务, 再启动. net core 服务. 这个时候在 eureka 服务的页面中发现名字为 serviceone 的客户端成功注册到了 eureka 服务中.
参考资料:
Spring Cloud https://github.com/SpringCloud/spring-cloud-docs
备注: 最近先更新 Spring Cloud 知识, 之后会使用完整的 Spring Cloud + .net core 完成一个完整的项目.
下周更新: 搭建 eureka 群集.
来源: https://www.cnblogs.com/f-z-h/p/10211651.html