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 | 上一篇主要介绍了相关理论,这一篇开始我们 ...
随机推荐
- sql server 由于登入失败而无法启动服务
到控制面板——管理工具——服务,找到mssqlserver这个服务,在属性里把登陆帐户改成你目前登录windows的帐户或选择本地系统账户再重新启动服务就好了
- [BUG] Dashboard报错:if usages['subnets']['available'] <= 0: KeyError: 'available'
Openstack版本号:Liberty 系统平台:CentOS 7.2 64bit ######################################################### ...
- Android -- Handling back button press Inside Fragments
干货(1) 首先创建一个抽象类BackHandledFragment,该类有一个抽象方法onBackPressed(),所有BackHandledFragment的子类在onBackPressed方法 ...
- (转)【Unity Shaders】Alpha Test和Alpha Blending
转自:http://blog.csdn.net/candycat1992/article/details/41599167 写在前面 关于alpha的问题一直是个比较容易摸不清头脑的事情,尤其是涉及到 ...
- 调试工具DebugView
可以用来跟踪如下函数的输出: Win32 OutputDebugString Kernel-mode DbgPrint All kernel-mode variants of DbgPrint i ...
- Redis实战总结-配置、持久化、复制
Redis的配置主要放置在redis.conf,可以通过修改配置文件实现Redis许多特性,比如复制,持久化,集群等. redis.conf部分配置详解 # 启动redis,显示加载配置redis.c ...
- DISQLite3在XE4中的安装
时隔这么久,因为工作中需要将一些图片序列文件进行分析,然后将结果进行分组统计,而分组统计用SQL语法很容易实现,但是要求程序运行的环境中安装有庞大的数据库系统,经过网上的寻找,终于发现了SQLite. ...
- 关于Promise的一些个人理解jQuery的deferred
一.什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组), ...
- 优雅地乱玩Linux-6-Chrome端SSH插件
文章最初发表于szhshp的第三边境研究所转载请注明 优雅地乱玩Linux-6-Chrome端SSH插件 最近注册了个AWS,免费一年,一年内可以各种乱玩~ 从自己的电脑连接云主机一般需要SSH这样的 ...
- 【转】使用 Android 的日志工具LogCat
Android中的日志工具类是 Log(android.util.Log),这个类中提供了如下几个方法来供我们打印日志. 1. Log.v() 这个方法用于打印那些最为琐碎的,意义最小的日志信息 ...