Spring Cloud开发实践 - 02 - Eureka服务和接口定义
服务注册 EurekaServer
Eureka服务模块只有三个文件, 分别是pom.xml, application.yml 和 EurekaServerApplication.java, 内容如下
pom.xml
spring-boot-maven-plugin: 使用 goal=repackage 可以打包出一个包含所有依赖的fat jar
maven-deploy-plugin: skip=true 表示当执行deploy时, 这个模块不会被提交到maven的repository
<?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> <parent>
<groupId>com.rockbb</groupId>
<artifactId>scot</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../scot/pom.xml</relativePath>
</parent>
<artifactId>scot-eureka</artifactId>
<packaging>jar</packaging>
<name>Scot: Eureka Server</name> <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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<finalName>scot-eureka</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml
将自己配置为 Eureka Server
server:
port: ${PORT:} eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
server:
enableSelfPreservation: true
renewalPercentThreshold: 0.1
EurekaServerApplication.java
@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
.
接口定义 Commons API
scot-commons-api模块定义了服务的接口Service和参数类型DTO. 因为Spring Cloud的特殊性, 这里的Service定义使用了@FeignClient和@RequestMapping注解, 这样在被下游调用时, 可以通过 @EnableFeignClients(basePackages = {"com.rockbb.scot.commons.api"}) 很方便地将服务引入.
本模块只有三个文件, pom.xml, UserDTO.java, UserDTOService.java
pom.xml
<?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> <parent>
<groupId>com.rockbb</groupId>
<artifactId>scot</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../scot/pom.xml</relativePath>
</parent>
<artifactId>scot-commons-api</artifactId>
<packaging>jar</packaging>
<name>Scot: Commons API</name> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.rockbb</groupId>
<artifactId>scot-commons-lib</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
UserDTO.java
注意如果要添加带参数的 constructor, 一定要把无参的constructor也加上, 否则下游无法将对象反序列化.
public class UserDTO implements Serializable {
private String id;
private String name; public UserDTO initialize(String id, String name) {
this.id = id;
this.name = name;
return this;
} public String getId() { return id; }
public void setId(String id) { this.id = id; } public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
UserDTOService.java
- @FeignClient 的value, 来自于 scot-commons-impl 模块的 spring.application.name, 多个DTOService可以使用相同的value.
- @RequestParam 一定要添加 value = "xx" , 否则在调用中即使你指定了method=GET, feign依然会使用POST进行调用, 导致错误
- @RequestMapping 可以像Controller一样同时定义于class和method
@FeignClient(value = "scot-commons")
@RequestMapping(value = "/user")
public interface UserDTOService { @RequestMapping(value = "/diagnos", method = RequestMethod.GET)
String diagnos(@RequestParam(value = "name") String name); @RequestMapping(value = "/get", method = RequestMethod.GET)
UserDTO get(@RequestParam(value = "id") String id); @RequestMapping(value = "/list", method = RequestMethod.GET)
List<UserDTO> list(); @RequestMapping(value = "/count", method = RequestMethod.GET)
long count();
}
.
Spring Cloud开发实践 - 02 - Eureka服务和接口定义的更多相关文章
- Spring Cloud开发人员如何解决服务冲突和实例乱窜?(IP实现方案)
一.背景 在我上一篇文章<Spring Cloud开发人员如何解决服务冲突和实例乱窜?>中提到使用服务的元数据来实现隔离和路由,有朋友问到能不能直接通过IP来实现?本文就和大家一起来讨论一 ...
- Spring Cloud开发实践 - 01 - 简介和根模块
简介 使用Spring Boot的提升主要在于jar的打包形式给运维带来了很大的便利, 而Spring Cloud本身的优点不是那么明显, 相对于Dubbo而言, 可能体现在跨语言的交互性上(例如可以 ...
- Spring Cloud开发实践 - 04 - Docker部署
Docker的安装和命令可以参考 https://www.cnblogs.com/milton/p/9866963.html . 资源规划 这一步要区分传统资源和Docker资源, 为后面的细节定好基 ...
- Spring Cloud开发实践 - 03 - 接口实现和下游调用
接口实现 Scot Commons Impl 接口实现模块 scot-commons-impl, 一方面实现了 scot-commons-api 的接口, 一方面将自己暴露为 REST 服务. 有4个 ...
- Spring Cloud开发人员如何解决服务冲突和实例乱窜?
一.背景 在我们开发微服务架构系统时,虽然说每个微服务都是孤立的可以单独开发,但实际上并非如此,要调试和测试你的服务不仅需要您的微服务启动和运行,还需要它的上下文服务.依赖的基础服务等都要运行:但如果 ...
- 没使用Spring Cloud的版本管理导致Eureka服务无法注册到Eureka服务注册中心
创建了一个Eureka Server的服务注册集群(两个Eureka服务),都能相互注册,写了一个Eureka客户端服务无法注册到服务发现注册中心 注册中心1: 注册中心2: 服务正常: pom依赖文 ...
- Spring Cloud 入门教程(一): Eureka 服务注册
创建一个Maven工程,New-Other-Maven-Maven Probject 点击Next,红色框里的选上 点击Next 点击Finsh就完成了一个Maven Probject的创建. (1) ...
- Spring Cloud官方文档中文版-服务发现:Eureka服务端
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR3/#spring-cloud-eureka-server 文中例子我做了一些 ...
- Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】
Spring Cloud(二):服务注册与发现 Eureka[Finchley 版] 发表于 2018-04-15 | 更新于 2018-05-07 | 上一篇主要介绍了相关理论,这一篇开始我们 ...
随机推荐
- Unreal Engine 4 减少编辑器的帧率
默认的,打开UE4的编辑器,显卡会以全速渲染场景,在我的机器上.是120FPS. 整个机器就開始轰鸣了.资源占用太凶了.事实上全然不是必需这样,帧率在60左右就足够了. 那怎么改动呢,试了非常多办法. ...
- Java NIO ServerSocketChannel
A Java NIO ServerSocketChannel is a channel that can listen for incoming TCP connections, just like ...
- Java学习笔记——IO操作之以图片地址下载图片
以图片地址下载图片 读取给定图片文件的内容,用FileInputStream public static byte[] mReaderPicture(String filePath) { byte[] ...
- fortran中提取字符串中可见字符的索引
fortran中常常需要提取字符串中可见字符的索引,下面是个小例子: !============================================================= su ...
- 如何回收vRealize Automation里被分配出去了的IP地址
在vRealize里写代码部署虚机,时间长了,便出现了很多虚机在vCenter里不存在,但在vRealize里还存在的这台虚机的注册信息的现象.最直接的后果是,这些影子虚机会占着IP池里的IP地址不放 ...
- 遛老虎网 http://6laohu.com/
遛老虎网 http://6laohu.com/
- java 从网络Url中下载文件 (转)
http://blog.csdn.net/xb12369/article/details/40543649/ /** * 从网络Url中下载文件 * @param urlStr ...
- Anroid 解决小米和魅族不能在mac上调试
第一种方法 1.mac->关于本机->系统报告->usb->copy厂商ID** 2.cmd->echo " 0x2a45" >> ~/. ...
- How to duplicate the records in a MongoDB collection
// Fill out a literal array of collections you want to duplicate the records in. // Iterate over e ...
- Modbus常用功能码协议详解
Modbus常用功能码协议详解 01H-读线圈状态 1)描述:读从机线圈寄存器,位操作,可读单个或者多个: 2)发送指令: 假设从机地址位0x01,寄存器开始地址0x0023,寄存器结束抵制0x003 ...