場景
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@EnableDiscoveryClientpublic class WebAdminRibbonApplication { public static void main(String[] args) { SpringApplication.run(WebAdminRibbonApplication.class, args); }}
注:
通過 @EnableDiscoveryClient 注解注冊到服務中心
然后是組態檔代碼:
spring: application: name: hello-spring-cloud-web-admin-ribbon thymeleaf: cache: false mode: LEGACYHTML5 encoding: UTF-8 servlet: 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;@Configurationpublic class RestTemplateConfiguration { @Bean @LoadBalanced public 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;@Servicepublic class AdminService { @Autowired private 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;@RestControllerpublic class AdminController { @Autowired private 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 埠
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/39958.html
標籤:設計模式
上一篇:設計模式之?代理模式
