欢迎来到菜鸟SpringCloud入门实战系列(SpringCloudForNoob),该系列经过层层递进的实战视角,来一步步学习和理解SpringCloud。html
本系列适合有必定Java以及SpringBoot基础的同窗阅读。java
每篇文章末尾都附有本文对应的Github源代码,方便同窗调试。git
Github仓库地址:程序员
https://github.com/qqxx6661/springcloud_for_noobgithub
你能够经过如下两种途径查看菜鸟SpringCloud入门实战系列:web
前文回顾:面试
本章节中须要有三个角色:服务注册中心(对应前文中咱们的eureka子模块)、服务提供者(对应前文中咱们的eureka-hi子模块)、服务消费者,其中服务注册中心就是咱们上一篇的eureka单机版启动既可,流程是首先启动注册中心,服务提供者生产服务并注册到服务中心中,消费者从服务中心中获取服务并执行。算法
子模块不须要作更改。spring
值得注意的是,你能够使用@EnableDiscoveryClient代替@EnableEurekaClient后端
二者的区别:
https://www.jianshu.com/p/f6db3117864f
注解@EnableEurekaClient上有@EnableDiscoveryClient注解,能够说基本就是EnableEurekaClient有@EnableDiscoveryClient的功能,另外上面的注释中提到,其实@EnableEurekaClientz注解就是一种方便使用eureka的注解而已,能够说使用其余的注册中心后,均可以使用@EnableDiscoveryClient注解,可是使用@EnableEurekaClient的情景,就是在服务采用eureka做为注册中心的时候,使用场景较为单一。
建立子模块service-feign,步骤和以前相似,请参考教程第一章。
修改pom.xml,引入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
复制代码
而后在主程序引入:
@EnableFeignClients
@EnableEurekaClient
复制代码
这时候,个人springboot2.0.3又出事了,@EnableFeignClients没法引入,须要将pom.xml的引入修改成:
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--spring boot 2.0.3版本解决方案:spring-cloud-starter-feign-->
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
复制代码
补充:Spring Boot 2下使用Feign找不到@EnableFeignClients的解决办法
https://blog.csdn.net/alinyua/article/details/80070890
该做者给了一个完整的解决没法引入包问题的思路,文章篇幅很长,思路值得学习!
以后,修改application.yml:
server:
# 服务端口号
port: 8765
spring:
application:
# 服务名,即serviceId
name: service-feign
eureka:
client:
serviceUrl:
# 安全认证的服务注册中心地址
defaultZone: http://localhost:8761/eureka
复制代码
首先回顾一下eureka-hi的方法,它提供了一个上述eureka-hi服务提供了一个RESTful风格的接口:
/** 获取端口号 */
@Value("${server.port}")
String port;
/**
* 定义一个简单接口
* @param name
* @return
*/
@GetMapping("/hi/{name}")
public String home(@PathVariable String name){
return "hi " + name + ",I am from service-hi, port :" + port;
}
复制代码
编写调用eureka-hi提供的接口的本地接口ServiceHi.java,以下:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* <p>一个Feign服务消费者接口</p>
**/
@FeignClient(value = "service-hi")
public interface ServiceHi {
/**
* <p>经过Feign伪Http客户端调用service-hi提供的服务</p>
* @author hanchao 2018/5/19 17:59
**/
@GetMapping("/hi/{name}")
String sayHiFromServiceHi(@PathVariable(value = "name") String name);
}
复制代码
说明:
以后,新建HelloController.java,以下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>服务消费控制层</p>
**/
@RestController
public class HelloController {
/** 注入服务"service-hi"的Feign客户端ServiceHi */
@Autowired
private ServiceHi serviceHi;
/**
* 调用Feign客户端提供的服务,自带负载均衡
* @param name
* @return
*/
@GetMapping("/hello/{name}")
public String sayHi(@PathVariable String name){
//调用Feign客户端ScheduleServiceHi的接口
return serviceHi.sayHiFromServiceHi(name);
}
}
复制代码
重点:
至此咱们已经配置完毕,依次启动服务eureka、eureka-hi和service-feign。能够看到两个服务都已经在eureka注册:
访问 http://localhost:8765/hello/rude3knife ,即service-feign提供的服务接口。 这个服务接口会经过Feign去调用服务eureka-hi提供的服务接口,结果显示服务间调用成功。
在该调用中,咱们feign并不须要指定端口号,它并不知道这个方法所在的服务提供者如今在哪一个端口运行,咱们只须要向eureka寻求服务。
三个模块的拓扑图以下:
Feign会对服务调用进行负载平衡,咱们须要同时打开两个eureka-hi服务,因为在同一台电脑上,就得把端口号从8763改成8764,而后同时开启8763和8764两个服务。
要同时运行两个端口不一样的相同服务,须要在run configuration里面把allow parallle打开:
运行结构是这样的:
访问http://localhost:8765/hello/rude3knife
连续访问两次,发现两次会分别取调用eureka-hi的两个服务节点:
最后是负载平衡的拓扑图:
https://github.com/qqxx6661/springcloud_for_noob/tree/master/04-servier-feign
Spring-Cloud笔记04:服务消费者Feign
https://blog.csdn.net/hanchao5272/article/details/80574441
springcloud(三):服务提供与调用
http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html
菜鸟SpringCloud实战专栏全导航:经过如下两种途径查看
我是蛮三刀把刀,后端开发。
主要关注后端开发,数据安全,爬虫等方向。
来微信和我聊聊:yangzd1102
Github我的主页:
同步更新公众号及如下博客
1. Csdn
拥有专栏:
2. 知乎
拥有专栏:
3. 掘金
4. 简书
若是文章对你有帮助,不妨收藏起来并转发给您的朋友们~