javascript
SpringCloud-创建服务消费者-Ribbon方式(附代码下载)
場景
SpringCloud-服務注冊與實現-Eureka創建服務注冊中心(附源碼下載):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102535957
SpringCloud-服務注冊與實現-Eureka創建服務提供者(附源碼下載):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558004
在上面已經實現服務注冊中心和服務提供者的基礎上,再創建服務消費者,即使用上面提供服務的一方。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
參考上面構建項目的方式,依次建立目錄hello-spring-cloud-web-admin-ribbon目錄以及在
目錄下新建pom.xml,并將其托管。然后新建src/main/java目錄和src/main/resources目錄并分別進行目錄設置。
然后在java下新建包,包下新建啟動類,在resources下新建配置文件application.yml。
完成后的目錄為:
?
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><parent><groupId>com.badao</groupId><artifactId>hello-spring-cloud-dependencies</artifactId><version>1.0.0-SNAPSHOT</version><relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath></parent><artifactId>hello-spring-cloud-web-admin-ribbon</artifactId><packaging>jar</packaging><name>hello-spring-cloud-web-admin-ribbon</name><url>https://blog.csdn.net/badao_liumang_qizhi</url><inceptionYear>2019-Now</inceptionYear><dependencies><!-- Spring Boot Begin --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- Spring Boot End --><!-- Spring Cloud Begin --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><!-- Spring Cloud End --><!-- 解決 thymeleaf 模板引擎一定要執行嚴格的 html5 格式校驗問題 --><dependency><groupId>net.sourceforge.nekohtml</groupId><artifactId>nekohtml</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.funtl.hello.spring.cloud.web.admin.ribbon.WebAdminRibbonApplication</mainClass></configuration></plugin></plugins></build> </project>注:
這里的parent標簽要與上面的統一的依賴管理對應起來。
要修改指定的程序入口類為自己相應的路徑。
然后應用啟動類的代碼:
package com.badao.hello.spring.cloud.web.admin.ribbon;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication @EnableDiscoveryClient public class WebAdminRibbonApplication {public static void main(String[] args) {SpringApplication.run(WebAdminRibbonApplication.class, args);} }注:
通過 @EnableDiscoveryClient 注解注冊到服務中心
然后是配置文件代碼:
spring:application:name: hello-spring-cloud-web-admin-ribbonthymeleaf:cache: falsemode: LEGACYHTML5encoding: UTF-8servlet:content-type: text/htmlserver:port: 8764eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/注:
1.服務注冊與發現是根據上面的name去尋找。
2.port表示端口號。
3.serviceURL設置eureka的地址,與上面創建服務注冊中心時的URL對應。
然后新建配置類注入RestTemplte的Bean并通過@LoadBalanced 注解表明開啟負載均衡功能。
在包下新建config包并新建RestTemplateConfiguration配置類
package com.badao.hello.spring.cloud.web.admin.ribbon.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;@Configuration public class RestTemplateConfiguration {@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();} }?
為了體現出負載均衡效果,我們要啟動兩臺service-admin,即啟動兩個服務提供者。
我們先啟動服務注冊中心Eureka服務8761端口,再以8762端口啟動一個服務提供者,然后點擊Run-Edit Configuration,將啟動單實例去掉。
?
然后修改服務提供者的配置文件中端口號為8763,再啟動一個服務提供者。
?
然后我們在Eureka的界面即:http://localhost:8761/刷新就可以看到兩個服務提供者。
?
消費者要想實現負載均衡的效果,應該一會訪問8762的服務提供者,一會訪問8763的服務提供者。
所以我們在服務消費者配置了@LoadBalanced即可實現。
既然是消費服務者,所以要新建調用服務的controller和service
在ribbon包下新建service包,包下新建AdminService
package com.badao.hello.spring.cloud.web.admin.ribbon.service;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;@Service public class AdminService {@Autowiredprivate RestTemplate restTemplate;public String sayHi(String message) {return restTemplate.getForObject("http://HELLO-SPRING-CLOUD-SERVICE-ADMIN/hi?message=" + message, String.class);} }這里使用的是Ribbon+RestTemlate進行服務的調用,使用RestTeplate的getForObject()方法。其中url參數就是
上面服務提供者的配置文件中的name。
然后再新建controller包,包下新建AdminController
package com.badao.hello.spring.cloud.web.admin.ribbon.controller;import com.badao.hello.spring.cloud.web.admin.ribbon.service.AdminService; 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;@RestController public class AdminController {@Autowiredprivate AdminService adminService;@RequestMapping(value = "hi", method = RequestMethod.GET)public String sayHi(@RequestParam String message) {return adminService.sayHi(message);} }然后運行當前服務消費者的啟動程序。
打開瀏覽器輸入:
http://localhost:8764/hi?message=badaoXiaofeizhe
?
代碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11860102
此時的架構
一個服務注冊中心,Eureka Server,端口號為:8761
service-admin 工程運行了兩個實例,端口號分別為:8762,8763
web-admin-ribbon 工程端口號為:8764
web-admin-ribbon 通過 RestTemplate 調用 service-admin 接口時因為啟用了負載均衡功能故會輪流調用它的 8762 和 8763 端口
總結
以上是生活随笔為你收集整理的SpringCloud-创建服务消费者-Ribbon方式(附代码下载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringCloud-服务注册与实现-
- 下一篇: gradle idea java ssm