前置博文
本博文在博文SpringCloud Netflix之Eureka Server的基础上继续搭建的,需要环境准备的请参考该博文。
服务消费者搭建
我们在scn模块下创建子模块scn-service-auto-deliver,其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">
<parent>
<artifactId>scn</artifactId>
<groupId>com.rubin</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>scn-service-auto-deliver</artifactId>
<dependencies>
<dependency>
<groupId>com.rubin</groupId>
<artifactId>scn-service-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
创建启动类:
package com.rubin.scn.service.autodeliver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class ScnServiceAutoDeliverBootstrap {
public static void main(String[] args) {
SpringApplication.run(ScnServiceAutoDeliverBootstrap.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
编写配置文件:
server:
port: 9001
spring:
application:
name: scn-service-auto-deliver
eureka:
instance:
hostname: 127.0.0.1
prefer-ip-address: true
# 配置中心读取的配置不能解析@。。@来标注的pom文件中的变量,只能写死或者配置在该配置文件中用${}的形式引用
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:1.0-SNAPSHOT
# 租约续约间隔时间,默认30秒
lease-renewal-interval-in-seconds: 30
# 租约到期,服务时效时间,默认值90秒,服务超过90秒没有发生⼼跳,EurekaServer会将服务从列表移除
lease-expiration-duration-in-seconds: 90
metadata-map:
name: rubin
age: 27
work: java programer
client:
service-url:
defaultZone: http://eureka-host:8761/eureka/,http://eureka-host:8762/eureka/
register-with-eureka: true
# 每隔多久拉取一次服务列表
registry-fetch-interval-seconds: 30
fetch-registry: true
# 配制了该项 回阻止将该实例注册为一个eureka client 默认是true 默认自动加入一个Maker类标记 所以引入eureka-client依赖之后 加不加@EnableEurekaClient都会默认注册进EurekaServer
# enabled: false
# springboot中暴露健康检查等断点接口
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的细节
endpoint:
health:
show-details: always
我们编写一个Controller来对外提供服务,代码如下:
package com.rubin.scn.service.autodeliver.controller;
import com.rubin.scn.common.entity.RResume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("auto-deliver")
public class AutoDeliverController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("service/{id}")
public List<ServiceInstance> getServiceInfos(@PathVariable("id") String id) {
return discoveryClient.getInstances(id);
}
@GetMapping("resume/{userId}")
public RResume getResumeByUserId(@PathVariable("userId") Long userId) {
List<ServiceInstance> instances = getServiceInfos("scn-service-resume");
ServiceInstance instance = instances.get(0);
String host = instance.getHost();
Integer port = instance.getPort();
String url = new StringBuffer("http://")
.append(host)
.append(":")
.append(port)
.append("/resume/detail?userId=")
.append(userId)
.toString();
RResume rResume = restTemplate.getForObject(url, RResume.class);
return rResume;
}
}
由上述代码我们可以看到,我们可以使用DiscoveryClient
来获取服务信息。我们可以在此之上做一些扩展:我们可以在配置项eureka.instance.metadata-map
配置项下添加一些自定义的元数据信息便于我们做一些特殊业务扩展。
至此,我们最简单的一个服务消费者就创建完成了。欢迎小伙伴积极留言交流~~
文章评论