微服务:独立的工程之间通过框架形成的一个集群的服务调用;这个集群称之为微服务集群;
微:集群中的工程(服务器),都是独立运行的项目;功能非常单一微小,例如后台的增删改查可以切分成4个独立运行的微服务工程.
springCloud的微服务框架具有很多的功能组件:
1 服务治理 eureka 所有工程都可以在eureak中注册自己的服务名称,如果名称一致,将会被eureka作为同一个服务来使用;
2 负载均衡调用组件:ribbon, 前端的客户端的组件
3 接口客户端组件: feign 底层依赖ribbon+template实现的调用。看不到实际调用的方法,利用接口,注解
4 熔断器:Hystrix, 当服务调用出现任何异常或者问题时,可以利用熔断的逻辑完成错误的解决;类似代码中的try catch
5 分布式配置:config,分布式配置组件;
6 网关组件:zuul,实现网关路由,监听,对当前的需要的服务进行网关治理;
Feign
负载均衡调用服务的客户端组件,声明式(注解)客户端。使得调用更简单(无需关心底层调用的api getForEntity getForObject post…);底层基于ribbon+restTemplate(内部整合了容错的断容器Hystrix)
1 quickstart 的骨架
可以再 start.spring.io 生成一个springboot工程
2 pom依赖
<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.arno</groupId>
<artifactId>feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>feign-client</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!--注意这个位置,必须是springboot支持的版本。否则会报出依赖会有错误,但是编译没有问题
运行出 java.net.UnknownHostException: 的错-->
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
3 application.properties
server.port=8095
//在eureka中心注册一个service-hi的服务,名字自定义,服务中心根据服务名称判断是否是处理同一种逻辑的服务,
//例如用户登录服务,统一一个名字,可以启动多个项目,到注册中心
spring.application.name=service-feign
//指向注册中心,与eureka center一致,服务启动后悔自动到下面的地址注册名称为service-feign的服务
eureka.client.serviceUrl.defaultZone=http://localhost:8090/eureka/
4 启动类、Controller、Service
@EnableEurekaClient 客户端去注册中心注册服务
@EnableFeignClients 打开Feign客户端
启动类
package com.arno;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class StarterFeign {
public static void main(String[] args) {
SpringApplication.run(StarterFeign.class, args);
}
}
Controller
package com.arno.controll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.arno.service.HiService;
@Controller
public class HiController {
@Autowired
private HiService hiService;
@RequestMapping("hi")
@ResponseBody
public String sayHi(String name){
String result=hiService.sayHi(name);
return "FEIGN:"+result;
}
}
Service
package com.arno.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;
//feignClient注解实现接口类的底层方法,
//注解的参数value表示当前接口所有方法调用的服务名称
//restTemplate.getForObject("http://service-hi/hi?name=name" String)
@FeignClient("service-hi")
public interface HiService {
//为了让底层template对象正确拼接方法的参数值,需要springmvc的注解帮忙
//请求地址为服务后面拼接/hi 请求方式为GET
//name是指定给服务传递的参数
@RequestMapping(value="hi",method=RequestMethod.GET)
String sayHi(@RequestParam(value="name")String name);
}