前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
一、新建 ribbon 工程:
1. file - new - module?
2. spring Initializr - module SDK 選擇自己的 JDK ,其余的可以不用填寫,next。
3. 填寫工程相關信息:包名、工程名等,next。
4. spring cloud discovery - 勾選 eureka discover client,next。
或:spring cloud routing - 勾選 openfeign,next。 ( 這步這2種都可。)
5. 工程名,代碼存放位置等,finish 。
6. 工程結構如下:
7.?pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.feign</groupId><artifactId>service-feign</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-feign</name><description>服務消費 feign 方式</description><parent><groupId>com.base</groupId><artifactId>base-config</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>
?8. 在工程啟動類上加注解:@EnableFeignClients?。
package com.feign.servicefeign;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;@SpringBootApplication/*** 基于接口的注解,可插拔,可使用 Feign注解 和 JAX-RS注解* Feign 默認集成 Ribbon,并和 Eureka 結合,默認實現負載均衡。*/
@EnableFeignClients// 標明自已為服務
@EnableEurekaClient
public class ServiceFeignApplication {public static void main(String[] args) {SpringApplication.run(ServiceFeignApplication.class, args);}}
9. 創建?interface?SeeParamService,使用注解指定要調用的服務?@FeignClient ( value = "服務名" )
package com.feign.servicefeign;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;/*** @Description:* @Auther: silence* @Date: 2019/7/23 20:09*/
@FeignClient(value = "see-param")
public interface SeeParamService {@RequestMapping(value = "/seeParam", method = RequestMethod.GET)String seeParam(@RequestParam(value = "param") String param);}
對外訪問 controller :
package com.feign.servicefeign;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.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** @author yujiang* @description* @date 2019/7/23 20:50*/
@RestController
public class SeeParamController {@AutowiredSeeParamService seeParamService;@RequestMapping(value = "/seeParam", method = RequestMethod.GET)public String seeParam(@RequestParam String param) {return seeParamService.seeParam(param);}}
10. 配置文件相關設置:
# 注冊中心 - 端口: 1234、工程名: eureka (見 eureka 工程中配置)。
eureka.client.serviceUrl.defaultZone= http://localhost:1234/eureka/# 端口
server.port= 8702# 工程名
spring.application.name= feign
11. 瀏覽器訪問:??http://localhost:8702/seeParam?param=我愛小熊 。刷新多次請求,得到不同端口服返回的結果 。
?
12. 從注冊中心可知,當前注冊了5 個服務,訪問注冊中心:http://localhost:1234/
13. 不斷刷新 ribbon 工程訪問地址,可見 8801、8802、8803 都有請求到。圖見第?11 點。
說明 負載均衡 已實現,消費者(服務請求方應用)請求到了不同的生產者(服務提供方應用)。
14.總結:
此時 整個工程體系為:
1個注冊中心:eureka 工程,端口:1234 。
3個生產者 see-param?,分別占用端口:8801、8802、8803 ,三者都向 eureka??注冊,暴露自已提供的服務。
1個消費者 ribbon 工程,端口:8701 。向?eureka 注冊 ,訂閱自已所需要的服務。
另 1 個消費者 feign 工程 ,端口:8702。向?eureka 注冊 ,訂閱自已所需要的服務。
feign 有作負載均衡,故 在調用生產者服務時,可輪流請求到不同的生產者服務。
-------------------------------------------------------------
下一篇:springCloud - 第5篇 - 斷路器 Hystrix ( Feign 、Ribbon )
源碼見:https://gitee.com/FJ_WoMenDeShiJie/springcloud-ribbon.git
-------------------------------------------------------------
PS:這個系列不定時更新,只是個人的學習分享,
內容全程參考書目:
《Spring Cloud 與 Docker 微服務架構空實戰?》、
《Spring Cloud 微服務實戰》及此書作者博客:http://blog.didispace.com/spring-cloud-learning/
《深入理解 Spring Cloud 與微服務構建》及此書作者博客:https://blog.csdn.net/forezp/article/details/70148833
--------------------------------------------------------------