下面咱们建立提供服务的客户端,并向服务注册中心注册本身。本文咱们主要介绍服务的注册与发现,因此咱们不妨在服务提供方中尝试着提供一个接口来获取当前全部的服务信息.html
1. 首先咱们先建立一个SpringBoot 的项目,命名为:erueka-client 在pom 中添加以下的配置:web
======================================================spring
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>bootstrap
<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.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>架构
=====================================================app
其次,实现/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中对服务信息的输出。分布式
代码以下图:spring-boot
@EnableDiscoveryClient
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(
ClientApplication.class)
.web(true).run(args);
}
}
修改配置文件:application.properties ,具体修改内容以下:
spring.application.name=eureka-client
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
经过spring.application.name
属性,咱们能够指定微服务的名称后续在调用的时候只须要使用该名称就能够进行服务的访问。eureka.client.serviceUrl.defaultZone
属性对应服务注册中心的配置内容,指定服务注册中心的位置。为了在本机上测试区分服务提供方和服务注册中心,使用server.port
属性设置不一样的端口。
启动该工程后,再次访问:http://localhost:1001/。能够以下图内容,咱们定义的服务被成功注册了。
固然,咱们也能够经过直接访问eureka-client
服务提供的/dc
接口来获取当前的服务清单,只须要访问:http://localhost:2001/dc,咱们能够获得以下输出返回:
其中,方括号中的eureka-client
就是经过Spring Cloud定义的DiscoveryClient
接口在eureka的实现中获取到的全部服务清单。因为Spring Cloud在服务发现这一层作了很是好的抽象,因此,对于上面的程序,咱们能够无缝的从eureka的服务治理体系切换到consul的服务治理体系中区。
Spring Cloud Consul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,它包含多个组件,可是做为一个总体,在微服务架构中为咱们的基础设施提供服务发现和服务配置的工具。它包含了下面几个特性:
因为Spring Cloud Consul项目的实现,咱们能够轻松的将基于Spring Boot的微服务应用注册到Consul上,并经过此实现微服务架构中的服务治理。
以以前实现的基于Eureka的示例(eureka-client)为基础,咱们如何将以前实现的服务提供者注册到Consul上呢?方法很是简单,咱们只须要在pom.xml
中将eureka的依赖修改成以下依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
接下来再修改一下application.properites
,将consul须要的配置信息加入便可,好比:(下面配置是默认值)
spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 |
到此为止,咱们将eureka-client转换为基于consul服务治理的服务提供者就完成了。前文咱们已经有提到过服务发现的接口DiscoveryClient
是Spring Cloud对服务治理作的一层抽象,因此能够屏蔽Eureka和Consul服务治理的实现细节,咱们的程序不须要作任何改变,只须要引入不一样的服务治理依赖,并配置相关的配置属性就能轻松的将微服务归入Spring Cloud的各个服务治理框架中。
下面能够尝试让consul的服务提供者运行起来。这里可能读者会问,不须要建立相似eureka-server的服务端吗?因为Consul自身提供了服务端,因此咱们不须要像以前实现Eureka的时候建立服务注册中心,直接经过下载consul的服务端程序就可使用。
(
作服务发现的框架经常使用的有
)
到官网上面下载consul 服务器:
https://www.consul.io/downloads.html
环境变量配置:
在安装的位置解压获得 consul.exe 文件(个人解压位置是:D:\Program Files\consul)
环境变量增长一条:
增长一条D:\Program Files\consul
cmd 命令窗口执行:consul agent -dev
consul 自带 UI 界面,打开网址:http://localhost:8500 ,能够看到当前注册的服务界面
cmd 命令窗口执行:consul.exe agent -server ui -bootstrap -client 0.0.0.0 -data-dir="E:\consul" -bind X.X.X.X
其中X.X.X.X为服务器ip,便可使用http://X.X.X.X:8500 访问ui而不是只能使用localhost链接
具体启动命令以下图所示:
启动以前得项目,进入界面访问: