Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,控制总微代理,线,一次性令牌,全局锁定,领导选举,分布式会话,集群状态)。分布式系统的协调导致锅炉板模式,使用Spring Cloud开发人员可以快速站起来实现这些模式的服务和应用程序。它们适用于任何分布式环境,包括开发人员自己的笔记本电脑,裸机数据中心和Cloud Foundry等托管平台。
分布式就是将一个系统拆分成很多的子系统,比如淘宝系统,他有许许多多的子系统:会员系统、订单系统、积分系统等等,这样已拆分就大大的减轻了服务器的压力,如果想深入了解分布式的架构,还请百度分布式架构,这里不多做说明。
既然一个系统被拆分成了无数个子系统,那系统之间是如何进行通信的呢?假如我现在在订单系统中想获取到会员的信息,此时,你能怎么做?
可能你会说用外链接查询,这个也没啥错,但是正规的分布式系统都是将对应的表分给了对应的库,所以这里链接查询是走不通的,这时,你可能在想,要是我能直接调用会员服务的方法那该多好,Feign就来了,他解决了服务与服务之间的通讯。
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求,类似于Dubbo的RPC。
如果想实现服务与服务之前的调用,那么必须要有eureka这个注册中心,eureka在这里不多做解释,直接看代码:
1.创建一个eureka-service的springboot项目。
4.0.0
com.ymy
sprintcloud
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
不需要的jar可以不引用。
3.添加配置文件:
server:
port: 8769
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
这样eureka服务算是大功告成。
创建一个springboot项目:
pom文件:
4.0.0
com.ymy.client1
cloud-client1
0.0.1-SNAPSHOT
boot1
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
新建配置文件:application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8769/eureka/
server:
port: 8762
spring:
application:
name: service-hi
启动类:我将controller直接写在了启动类了,你们可以新建一个controller包
package com.ymy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@EnableEurekaClient
@SpringBootApplication
@RestController
@RequestMapping("c")
public class App1 {
public static void main(String[] args) {
SpringApplication.run(App1.class, args);
}
@Value("${server.port}")
String port;
@GetMapping("hello")
public String hello(@RequestParam String name){
return "hello "+name+" 我的服务端口是:"+port;
}
}
新建一个springboot项目:
pom文件:
4.0.0
com.ymy.client1
cloud-client1
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
新建配置文件:application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8769/eureka/
server:
port: 8763
spring:
application:
name: service-hi
启动类:
package com.ymy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@EnableEurekaClient
@SpringBootApplication
@RestController
@RequestMapping("c")
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
@Value("${server.port}")
String port;
@GetMapping("hello")
public String hello(@RequestParam String name){
return "hello "+name+" 我的服务端口是:"+port;
}
}
新建一个springboot项目
pom配置文件:
4.0.0
com.ymy.client3
cloud-client3
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
新建application.yml文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8769/eureka/
server:
port: 8765
spring:
application:
name: service-feign
feign:
hystrix:
enabled: true
controller类:
package com.ymy.controller;
import javax.inject.Inject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ymy.feign.ClientFeign;
import com.ymy.feign.HystrixClientFeign;
@RestController
public class MyController {
@Inject
private ClientFeign clientFeign;
// @Autowired
// private HystrixClientFeign hystrixClientFeign;
@GetMapping("hi")
public String hi(@RequestParam String name){
return clientFeign.getHello(name);
}
/*@GetMapping("hihi")
public String hihi(@RequestParam String name){
System.out.println("哈哈哈哈");
return hystrixClientFeign.hello(name);
}*/
}
重点来了,Feign的运用:
ClientFeign类:
package com.ymy.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "service-hi", fallback = HystrixClientFeign.class)
public interface ClientFeign {
@GetMapping("c/hello")
String getHello(@RequestParam(value = "name") String name);
}
HystrixClientFeign类:
package com.ymy.feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Component
public class HystrixClientFeign implements ClientFeign{
// @Autowired
// private RestTemplate restTemplate;
// @HystrixCommand(defaultFallback="hiError")
// public String hello(String name){
// return restTemplate.getForObject("http://service-hi/hello?name="+name, String.class);
// }
/*public String hiError(String name) {
return "hi," + name + ",sorry,error!";
}*/
@Override
public String getHello(String name) {
return "hello " + name + ", error!";
}
}
两个服务端我做的是集群,客服端以轮询的方式访问,有兴趣的小伙伴可以试试哦,需要源码的可以留言哦。