搭建前言
接着前面的幾文章我們已經搭建好了注冊中心和服務的生産者,這篇文章我們來搭建服務的消費方。
SpringBoot版本:2.0
2.0.4.RELEASE
spring-cloud-dependencies 版本:
Finchley.SR1
pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
配置檔案
spring.application.name=eureka-consumer
# Eureka 注冊中心位址
eureka.client.service-url.defaultZone=http://localhost:10000/eureka/
management.endpoints.web.exposure.include=refresh,health,info,env,loggers,metrics,trace,dump
management.endpoint.health.show-details=always
# Ϊ11000 11001 11002 ...
server.port= 8080
啟動類
@EnableFeignClients
@SpringBootApplication
@ComponentScan(value = "cn.lpck.eurekaconsumerfeign")
public class EurekaConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerFeignApplication.class, args);
}
}
注解解釋:
@EnableFeignClients
:由于我們使用的試Feign 元件 ,我們需要在SpringBoot的啟動類裡面去開啟Feign 元件的用戶端,在啟動時即注冊。
@ComponentScan(value = "cn.lpck.eurekaconsumerfeign")
: 元件掃描,識别feign元件
SpringCloud 發展至今提供了很多元件,我們去官方檢視大概是有二十幾個元件.使用Feign的好處就是他其實是封裝了Http請求,簡化了原本複雜的架構,實作了類似于Dubbo的
@Refrence
注解的作用,但是二者實作的原理卻大不一樣。
消費使用
到了這裡我們其實就已經實作整個SpringCloud架構種最核心的SOA思想,搭建不同的生産者與消費者就可以實作業務拆分.
消費者的服務請求接口
/**
* @Author: Liupu
* @Date: 2018/9/12 0012 上午 11:46
* @Description:
* @Version 1.0
*/
@Component
@FeignClient(name = "eureka-producer")
public interface HelloService {
@GetMapping("/hello/")
String hello(@RequestParam(value= "name") String name);
/* @RequestMapping("/hello/{user}")
String getHello(@PathVariable("user") String user);*/
@GetMapping("/hello/name")
List<String> getNames(@RequestParam("name") String name);
}
下面貼上我們之前搭建好的生産者的Controller
/**
* @Author: Liupu
* @Date: 2018/9/11 0011 下午 4:52
* @Description:
* @Version 1.0
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private UserService userService;
@GetMapping("/")
public String getHello(@RequestParam String name){
return "hello,"+ name +" "+ new Date();
}
/*@RequestMapping("/user")
public String helloUser(@RequestParam String name){
return "hello,"+ name +"aboyliupu"+ new Date();
}*/
@GetMapping("/name")
public List<String> getList(@RequestParam String name){
ArrayList<String> names = new ArrayList<>();
names.add("aaa");
names.add(name);
return names;
}
}
分析:消費者接口中的子父Mapping 與生産者中的子父Mapping一樣,然後通過注解
@FeignClient(name = "eureka-producer")
指向我們的消費者中心找到整個消費者然後就請求接口中的資料.
如果抛開整個生産者和注冊中心來看,這個項目就像是一個資料表現層,沒有自己擷取資料的能力,所有資料由第三方的接口提供。
然後再看整個項目 SOA的思想已經通過 生産者-----注冊中心-----消費者 展現的淋漓盡緻。我們學習分布式架構的目的也就是為了了解這樣的一套架構思想。
最後再看下 消費者的Controller
/**
* @Author: Liupu
* @Date: 2018/9/12 0012 上午 11:44
* @Description:feign 可以實作負載均衡 :@Value("${config.producer.instance:0}")
* 需要打包啟動 類似注冊中心叢集
* @Version 1.0
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/{name}")
public String index(@PathVariable("name") String name) {
return helloService.hello(name + "!");
}
@GetMapping("/name/{name}")
public List<String> getNames(@PathVariable("name") String name){
List<String> names = helloService.getNames(name);
return names;
}
}