SpringCloud学习笔记---服务的注册和发现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/paditang/article/details/79089713

前言

​ 微服务的意义和概念在当下十分火热,而spring同样提供了实现微服务的SpringCloud框架。更多的也不多说,希望通过这个系列记录学习过程的的经验。

​ 所有版本以SpringBoot2.xSpringCloud Finchley为主,构建工具为gradle,IDE为基础的eclipse。如果使用maven构建可以在官网和其他推荐文章中参考构建pom代码。

​ 第一篇是关于在微服务中服务中心的注册和服务的发现。服务管理使用NetFlix Eureka。文后使用的Spring Cloud Netflix都是SpringCoud对Netflix公司一系列开源产品的包装。

正文

创建服务注册中心

首先创建一个gradle工程,命名eureka-server,在build.gradle中输入依赖内容:

buildscript {
    ext {
        springBootVersion = '2.0.0.M6'
    }
    repositories {
        mavenCentral()
        mavenLocal()
        maven {
            url 'https://repo.spring.io/libs-milestone'
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: "io.spring.dependency-management"
sourceCompatibility = 1.8

repositories {
    maven {
        url 'https://repo.spring.io/libs-milestone'
    }
}

dependencyManagement {
  imports {
    mavenBom ':spring-cloud-dependencies:Finchley.M5'
  }
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.cloud:spring-cloud-starter-config:2.0.0.M1'
    compile 'org.springframework.cloud:spring-cloud-starter-eureka-server:2.0.0.M1'
}

启动gradle引入依赖后,创建一个服务注册中心:

@EnableEurekaServer
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
         SpringApplication.run(Application.class, args);
    }
}

在默认设置中,服务注册中心会将自己作为客户端注册自己,因此需要在配置中配置避免这种情况。

spring.application.name=eureka-server
server.port=1001

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

启动SpringBootApplication,启动成功后,在设置好的端口可以看到服务注册中心页面:http://localhost:1001

服务注册中心

创建服务客户端(提供者)

同样创建gradle工程,以下是gradle文件

buildscript {
    ext {
        springBootVersion = '2.0.0.M6'
    }
    repositories {
        mavenCentral()
        mavenLocal()
        maven {
            url 'https://repo.spring.io/libs-milestone'
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: "io.spring.dependency-management"
sourceCompatibility = 1.8

repositories {
    maven {
        mavenLocal()
        url 'https://repo.spring.io/libs-milestone'
    }
}

dependencyManagement {
  imports {
    mavenBom ':spring-cloud-dependencies:Finchley.M5'
  }
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.cloud:spring-cloud-starter-config:2.0.0.M1'
    // https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client', version: '2.0.0.M2'

}

为了在客户端显示服务信息,创建一个控制器:

@RestController
public class DcController {

    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/dc")
    public String dc(){
        String services = "Services: " + discoveryClient.getServices();
        System.out.println(services);
        return services;
    }
}

客户端Appliction:


@EnableDiscoveryClient
@SpringBootApplication
@SpringBootConfiguration
@ComponentScan(basePackageClasses=DcController.class)
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
}

启动application。

可以在之前的服务注册中心看到这个新的服务已注册:

服务客户端

再点开注册的控制器

输出服务信息

以上便建立了服务与服务注册中心之间的联系。

参考文章:

猜你喜欢

转载自blog.csdn.net/paditang/article/details/79089713