RPC方式需要添加-api模块(定义接口,报文),服务提供方和消费方双方均需要引用此模块依赖,且双方需引入org.springframework.cloud:spring-cloud-starter-feign依赖。
以下绿色注明RPC方式与REST方式的区别
parent:
<modules>
<module>eurekaServer</module>
<module>eurekaClient</module>
<module>eurekaClient-api</module>
<module>eurekaConsumer</module>
</modules>
<springcloud.version>Dalston.SR5</springcloud.version>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${springcloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
eureka Server (服务治理) :
pom
<artifactId>eurekaServer</artifactId>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>1.4.2.RELEASE</version> </dependency> </dependencies>
配置
server.port:8761 eureka.instance.hostname: localhost eureka.client.registerWithEureka: false eureka.client.fetchRegistry: false eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
启动
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
eureka Client (服务提供):
pom
<artifactId>eurekaClient</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>com.dragon.test</groupId>
<artifactId>eurekaClient-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
配置
server.port=18765 eureka.instance.hostname=localhost eureka.client.registerWithEureka=true eureka.client.fetchRegistry=true eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ spring.application.name: eureka-client-first
启动
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients(basePackages = { "com.eureka.test.api"})
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
具体服务
@RestController public class TestController implements TestAPI { @PostMapping("/test/hello") public Map<String, Object> hello(@RequestBody String word){ Map<String,Object> result = new HashMap<String,Object>(); result.put("name", "lulu"); result.put("age", "18"); result.put("word", word); return result; } }
eureka Client API (服务接口):
pom
<artifactId>eurekaClient-api</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
</dependencies>
接口
package com.eureka.test.api;
@FeignClient("eureka-client-first")
public interface TestAPI {
@PostMapping("/test/hello")
Map<String, Object> hello(@RequestBody String word);
}
eureka Consumer (服务消费):
pom
<artifactId>eurekaConsumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>com.dragon.test</groupId>
<artifactId>eurekaClient-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
配置
server.port=18766 eureka.instance.hostname=localhost eureka.client.registerWithEureka=true eureka.client.fetchRegistry=true eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ spring.application.name: eureka-client-consumer
启动
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients(basePackages = { "com.eureka.test.api"})
public class EurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
}
具体服务调用
@RestController public class TestController { @Autowired TestAPI testAPI; @RequestMapping("/test/hello") public Map<String, Object> hello(@RequestParam String word){ return testAPI.hello(word); } }