二、Spring Cloud - Netflix(Eureka+Ribbon+Feign)
部分代码参考两篇文章:
http://blog.didispace.com/springcloud1/
http://blog.didispace.com/springcloud2/
1.Spring Cloud 这一期实现的项目架构如下:
先看2017-Netflix_Service1和2017-Netflix_Service2的代码,其实没几个文件。Spring boot将spring、spring mvc繁杂的配置文件都写到jar里面,
tomcat也是用的内嵌,详细可以去官网和源码观摩是怎么实现的,http://blog.didispace.com/Spring-Boot基础教程/这是另一个参考。
2017_Netflix_Eureka_Server就是官网https://github.com/spring-cloud-samples/eureka,只是被我改了个名字
2017-Netflix_Ribbon 实现使用Ribbon实现客户端负载均衡的消费者
package com.jack.ssm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* ClassName:RibbonApplication.java
* Date: 2017年5月13日下午5:06:00
* @author Jack.Huang
* @version V1.0
* @since JDK 1.7.0_60/JDK 1.8.0_45
*/
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
package com.jack.ssm.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* ClassName:ConsumerController.java
* Date: 2017年5月13日下午5:10:00
* @author Jack.Huang
* @version V1.0
* @since JDK 1.7.0_60/JDK 1.8.0_45
*/
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/ribbonCallAddService", method = RequestMethod.GET)
public String add() {
return restTemplate.getForEntity("http://NETFLIX-SERVICE/add?a=10&b=20", String.class).getBody();
}
}
2.在上面的基础上使用Feign
Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。
pom.xml 要加上以下maven配置:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
全文件如下:
<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>
<groupId>com.sample</groupId>
<artifactId>2017-Netflix_Feign</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<!-- 指定source和target的版本 -->
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>
package com.jack.ssm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
/**
* ClassName:RibbonApplication.java
* Date: 2017年5月13日下午5:06:00
* @author Jack.Huang
* @version V1.0
* @since JDK 1.7.0_60/JDK 1.8.0_45
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
package com.jack.ssm.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* ClassName:ComputeService.java
* Date: 2017年5月13日下午10:06:07
* @author Jack.Huang
* @version V1.0
* @since JDK 1.7.0_60/JDK 1.8.0_45
*/
@FeignClient("NETFLIX-SERVICE")
public interface ComputeService {
@RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
package com.jack.ssm.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jack.ssm.service.ComputeService;
/**
* ClassName:ConsumerController.java
* Date: 2017年5月13日下午5:10:00
* @author Jack.Huang
* @version V1.0
* @since JDK 1.7.0_60/JDK 1.8.0_45
*/
@RestController
public class ConsumerController {
@Autowired
ComputeService computeService;
@RequestMapping(value = "/feignCallAddService", method = RequestMethod.GET)
public Integer add() {
return computeService.add(10, 20);
}
}
在前面的示例中,我们消费spring boot提供的Restful服务的时候,使用的是RestTemplate来实现的,实现起来还是比较复杂的,尤其是在消费复杂的Restful服务的时候,还需要进行一系列的转换,编解码等,使用Feign就完全不用考虑这个问题了。
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求,这整个调用过程和Dubbo的RPC非常类似。开发起来非常的优雅。