chen888的gravatar头像
chen888 2017-11-28 10:12:35
spring cloud学习之eureka-client服务提供方
  • 创建“服务提供方”

下面我们创建提供服务的客户端,并向服务注册中心注册自己。本文我们主要介绍服务的注册与发现,所以我们不妨在服务提供方中尝试着提供一个接口来获取当前所有的服务信息。

首先,在springCloudDemo父工作中创建一个基本的Spring Boot应用。命名为demo-client,在pom.xml中,加入如下配置:

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.8.RELEASE</version>

<relativePath />

</parent>

 

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

</dependencies>

 

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Dalston.RC1</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

其次,实现/dc请求处理接口,通过DiscoveryClient对象,在日志中打印出服务实例的相关内容。

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

最后在应用主类中通过加上@EnableDiscoveryClient注解,该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出。

 

@SpringBootApplication

@EnableDiscoveryClient // 向服务中心注册

public class ClientApplication extends SpringBootServletInitializer {

 

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

return builder.sources(ClientApplication.class);

}

 

public static void main(String[] args) {

SpringApplication.run(ClientApplication.class, args);

}

}

我们在完成了服务内容的实现之后,再继续对application.yml做一些配置工作,具体如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8090
spring: 
  application:
    name: DEMO-CLIENT 

通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。为了在本机上测试区分服务提供方和服务注册中心,使用server.port属性设置不同的端口。

启动该工程后,再次访问:http://localhost:8761/。可以如下图内容,我们定义的服务被成功注册了。

spring cloud学习之eureka-client服务提供方

当然,我们也可以通过直接访问eureka-client服务提供的/dc接口来获取当前的服务清单,只需要访问:http://localhost:8090/dc,我们可以得到如下输出返回:

Services: [demo-client]

其中,方括号中的demo-client就是通过Spring Cloud定义的DiscoveryClient接口在eureka的实现中获取到的所有服务清单。由于Spring Cloud在服务发现这一层做了非常好的抽象,所以,对于上面的程序,我们可以无缝的从eureka的服务治理体系切换到consul的服务治理体系中区。

 

  • Spring Cloud Consul

Spring Cloud Consul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,它包含多个组件,但是作为一个整体,在微服务架构中为我们的基础设施提供服务发现和服务配置的工具。它包含了下面几个特性:

  • 服务发现
  • 健康检查
  • Key/Value存储
  • 多数据中心

由于Spring Cloud Consul项目的实现,我们可以轻松的将基于Spring Boot的微服务应用注册到Consul上,并通过此实现微服务架构中的服务治理。

更多关于Consul的使用指南,读者可查看官方文档:https://www.consul.io/

源码路径:spring cloud简单的demo例子,适合初学者


打赏

已有1人打赏

最代码官方的gravatar头像
最近浏览
opq221  LV3 2023年3月22日
taowen2002  LV1 2020年3月23日
栗劲松  LV2 2019年11月26日
安安an  LV17 2019年7月23日
zyl  LV34 2018年6月29日
jxjyzzc  LV7 2018年5月24日
kinggode  LV14 2018年4月12日
KimOHHH  LV5 2018年2月8日
wilco  LV6 2017年12月6日
liuliu  LV23 2017年11月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友