前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
一、新建 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?- 勾選 ribbon,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.ribbon</groupId><artifactId>service-ribbon</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-ribbon</name><description>服務消費 ribbon 方式</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-netflix-ribbon</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. 在工程啟動類上加注解:@EnableDiscoveryClient??。
關于 2 個注解的區別見文章:springcloud 注解 @EnableDiscoveryClient 與 @EnableEurekaClient 的區別
package com.ribbon.serviceribbon;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;// 標明自已為服務
@EnableDiscoveryClient@SpringBootApplication
public class ServiceRibbonApplication {public static void main(String[] args) {SpringApplication.run(ServiceRibbonApplication.class, args);}/*** 向 ioc 注入 bean : restTemplate;* 注解 @LoadBalanced :此 bean 開啟負載均衡。* @return*/@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}
}
實現對于生產者服務的調用:
SeeParamService
package com.ribbon.serviceribbon;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;/*** @author yujiang* @description* @date 2019/7/22 11:45*/
@Service
public class SeeParamService {@AutowiredRestTemplate restTemplate;public String seeService(String param) {return restTemplate.getForObject("http://see-param/seeParam?param=" + param, String.class);}}
?SeeParamController:
package com.ribbon.serviceribbon;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** /**** @author yujiang* @description* @date 2019/7/22 13:27*/
@RestController
public class SeeParamController {@AutowiredSeeParamService seeParamService;@RequestMapping(value = "/seeParam")public String see(@RequestParam String param) {return seeParamService.seeService(param);}
}
9. 配置文件相關設置:
# 注冊中心 - 端口: 1234、工程名: eureka (見 eureka 工程中配置)。
eureka.client.serviceUrl.defaultZone= http://localhost:1234/eureka/# 端口
server.port= 8701# 工程名
spring.application.name= ribbon
10.啟動工程:
?
11. 瀏覽器訪問:??http://localhost:8701/seeParam?param=參數啊 。刷新多次請求,得到不同端口服返回的結果 。
?
12. 從注冊中心可知,當前注冊了4 個服務,訪問注冊中心:http://localhost:1234/
?
13. 不斷刷新 ribbon 工程訪問地址,可見 8801、8802、8803 都有請求到。圖見第?11 點。
說明 負載均衡 已實現,消費者(服務請求方應用)請求到了不同的生產者(服務提供方應用)。
14.總結:
此時 整個工程體系為:
1個注冊中心:eureka 工程,端口:1234 。
3個生產者 see-param?,分別占用端口:8801、8802、8803 ,三者都向 eureka? 注冊,暴露自已提供的服務。
1個消費者 ribbon 工程,端口:8701 。向?eureka 注冊 ,訂閱自已所需要的服務。
ribbon 有作負載均衡,故 在調用生產者服務時,可輪流請求到不同的生產者服務。
------------------------------------------------------------------------------
遇到 問題1:
解決方法見文章:解決:There was an unexpected error (type=Internal Server Error,..). No instances available for XXX
?
遇到 問題2 :
解決方法見文章:解決:Whitelabel Error Page This application has no explicit mapping for /error...UnknownHostException
------------------------------------------------------------------------
下一篇:springCloud - 第4篇 - 消費者調用服務 ( Feign )
源碼見:https://gitee.com/FJ_WoMenDeShiJie/springcloud-ribbon
--------------------------------------------------------------
PS:這個系列不定時更新,只是個人的學習分享,
內容全程參考書目:
《Spring Cloud 與 Docker 微服務架構空實戰?》、
《Spring Cloud 微服務實戰》及此書作者博客:http://blog.didispace.com/spring-cloud-learning/
《深入理解 Spring Cloud 與微服務構建》及此書作者博客:https://blog.csdn.net/forezp/article/details/70148833
--------------------------------------------------------------