我们创建提供服务的客户端,并向服务注册中心注册自己。主要介绍服务的发现,所以我们在服务提供方中尝试着提供一个接口来获取当前所有的服务信息。
实战开干
首先,项目搭建这里不用多说了,,,
接口来获取当前所有的服务信息
实现/do请求处理接口,通过DiscoveryClient对象,在日志中打印出服务实例的相关内容
package com.scp.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
*/
@RestController
public class DoController {
@Autowired
DiscoveryClient discoveryClient;
/**
* 实现/dc请求处理接口,通过DiscoveryClient对象,在日志中打印出服务实例的相关内容
*
* @return
*/
@GetMapping("/dc")
public String dc() {
//通过DiscoveryClient接口的getServices获取了当前客户端缓存的所有服务清单
String services = "Services: " + discoveryClient.getServices();
System.out.println(services);
return services;
}
}
应用类
应用主类中通过加上
@EnableDiscoveryClient
注解,该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出
package com.scp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 通过加上@EnableDiscoveryClient注解,
* 该注解能激活Eureka中的DiscoveryClient实现,
* 这样才能实现Controller中对服务信息的输出。
*/
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args)
{
SpringApplication.run(EurekaClientApplication.class, args);
}
}
配置文件
通过
spring.application.name
属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。eureka.client.serviceUrl.defaultZone
属性对应服务注册中心的配置内容,指定服务注册中心的位置。为了在本机上测试区分服务提供方和服务注册中心,使用server.port
属性设置不同的端口。
#服务名称
spring:
application:
name: eureka-client
#配置端口
server:
port: 2001
#注册服务中心
eureka:
client:
service-url:
defaultZone: http://localhost:1001/eureka/
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.scp</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动程序,浏览器访问
可以直接访问http://localhost:1001/
我们也可以通过直接访问eureka-client
服务提供的/do
接口来获取当前的服务清单,只需要访问
请看下一篇讲解 服务消费 《SpringCloud系列-服务消费(Ribbon)(三)》