eureka java api_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

4.0.0

com.rockbb

scot

1.0-SNAPSHOT

../scot/pom.xml

scot-eureka

jar

Scot: Eureka Server

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-starter-test

test

scot-eureka

org.apache.maven.plugins

maven-compiler-plugin

org.apache.maven.plugins

maven-resources-plugin

org.springframework.boot

spring-boot-maven-plugin

repackage

org.apache.maven.plugins

maven-deploy-plugin

true

application.yml

将自己配置为 Eureka Server

server:

port: ${PORT:8761}

eureka:

instance:

hostname: localhost

client:

registerWithEureka:falsefetchRegistry:falseserver:

enableSelfPreservation:truerenewalPercentThreshold: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

4.0.0

com.rockbb

scot

1.0-SNAPSHOT

../scot/pom.xml

scot-commons-api

jar

Scot: Commons API

org.springframework.cloud

spring-cloud-starter-openfeign

com.rockbb

scot-commons-lib

${project.version}

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 list();

@RequestMapping(value = "/count", method = RequestMethod.GET)

long count();

}

.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值