javascript
SpringCloud实战与原理---快速入门
SpringCloud實戰與原理
第一章? 快速入門
1. 初始化工程
2. 啟動Eureka注冊中心
2.1 Eureka注冊中心啟動代碼
2.2 啟動Eureka注冊中心
3.?注冊Eureka客戶端
3.1 Eureka客戶端代碼
3.2 注冊Eureka客戶端
4. 服務之間的調用
4.1 Feign調用代碼
4.2 Feign調用結果
5. 路由網關ZUUL
5.1 路由網關ZUUL代碼
5.2 啟動路由網關ZUUL
?
第一章? 快速入門
本章主要目的:搭建一個可使用的SpringClooud工程,對各模塊的功能后續會進行一一分析
1. 初始化工程
用Idea的Spring Initializr新建一個maven工程,pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>eng.lab</groupId><artifactId>springcloudsearch</artifactId><version>0.0.1-SNAPSHOT</version><name>springcloudsearch</name><description>Demo project for Spring Boot</description><packaging>pom</packaging><modules><module>eureka-server</module></modules><properties><java.version>1.8</java.version><spring-cloud.version>Hoxton.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>2. 啟動Eureka注冊中心
2.1 Eureka注冊中心啟動代碼
在idea新建Maven子模塊eureka-server
pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>eng.lab</groupId><artifactId>springcloudsearch</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>eng.lab</groupId><artifactId>eureka-server</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-server</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement> </project>并在啟動類中用@EnableEurekaServer啟動Eureka注冊中心
package eng.lab.eurekaserver;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}在application.yml中配置相關屬性
server:port: 8888 eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/2.2 啟動Eureka注冊中心
啟動服務成功后,用瀏覽器打開http://localhost:8888/,展示如下網頁則說明Eureka注冊中心啟動成功。
3.?注冊Eureka客戶端
3.1 Eureka客戶端代碼
在idea新建Maven子模塊eureka-client-a
pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>eng.lab</groupId><artifactId>springcloudsearch</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>eng.lab</groupId><artifactId>eureka-client-a</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-client-a</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies></project>并在啟動類中用@EnableEurekaClient啟動Eureka客戶端
package eng.lab.eurekaclientaa;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient @SpringBootApplication public class EurekaClientAApplication {public static void main(String[] args) {SpringApplication.run(EurekaClientAApplication.class, args);}}新增測試接口用于調用
package eng.lab.eurekaclientaa.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;/*** @Author: Wen-Xueliang* @Date: Created in 2020/3/5 22:12* @Description:*/ @RestController public class TestController {@GetMapping("/print")public void print() {System.out.println("This is A system!");}@GetMapping("/show")public String show() {return "This is A system!";} }在application.yml中配置相關屬性
eureka:client:serviceUrl:defaultZone: http://localhost:8888/eureka/ server:port: 8889 spring:application:name: eureka-client-a3.2 注冊Eureka客戶端
在瀏覽器中刷新http://localhost:8888/頁面,此時出現被注冊的服務EUREKA-CLIENT-A
用瀏覽器打開http://localhost:8889/show,則可直接調用該服務
4. 服務之間的調用
4.1 Feign調用代碼
在idea新建Maven子模塊eureka-client-b
pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>eng.lab</groupId><artifactId>springcloudsearch</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>eng.lab</groupId><artifactId>eureka-client-b</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-client-b</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies> </project>并在啟動類中用@EnableEurekaClient啟動Eureka客戶端,@EnableFeignClients啟動Feign客戶端。
package eng.lab.eurekaclientb;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients;@EnableEurekaClient @EnableFeignClients @SpringBootApplication public class EurekaClientBApplication {public static void main(String[] args) {SpringApplication.run(EurekaClientBApplication.class, args);}}新增服務用于調用eureka-client-a
package eng.lab.eurekaclientb.service;import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping;/*** @Author: Wen-Xueliang* @Date: Created in 2020/3/6 15:00* @Description:*/ @Service @FeignClient("eureka-client-a") public interface TestService {@GetMapping("/show")public String show(); }新增測試接口用于調用
package eng.lab.eurekaclientb.controller;import eng.lab.eurekaclientb.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;/*** @Author: Wen-Xueliang* @Date: Created in 2020/3/6 15:03* @Description:*/ @RestController public class InvokeController {@Autowiredprivate TestService testService;@GetMapping("/invoke")public String invoke() {return testService.show();} }在application.yml中配置相關屬性
eureka:client:serviceUrl:defaultZone: http://localhost:8888/eureka/ server:port: 8890 spring:application:name: eureka-client-b4.2 Feign調用結果
在瀏覽器中刷新http://localhost:8888/頁面,此時出現被新注冊的服務EUREKA-CLIENT-B
用瀏覽器打開http://localhost:8890/invoke,則可調用服務EUREKA-CLIENT-B,令其調用服務EUREKA-CLIENT-A。
5. 路由網關ZUUL
5.1 路由網關ZUUL代碼
在idea新建Maven子模塊zuul
pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>eng.lab</groupId><artifactId>springcloudsearch</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>eng.lab</groupId><artifactId>zuul</artifactId><version>0.0.1-SNAPSHOT</version><name>zuul</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency></dependencies> </project>并在啟動類中用@EnableZuulProxy啟動Zuul。
package eng.lab.zuul;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy @SpringBootApplication public class ZuulApplication {public static void main(String[] args) {SpringApplication.run(ZuulApplication.class, args);}}在application.yml中配置相關屬性
eureka:client:serviceUrl:defaultZone: http://localhost:8888/eureka/ server:port: 8000 spring:application:name: zuul zuul:routes:route-name:url: http://localhost:8889/path: /a/**5.2 啟動路由網關ZUUL
用瀏覽器打開http://localhost:8000/a/show,則可直接通過網關調用eureka-client-a服務
?
總結
以上是生活随笔為你收集整理的SpringCloud实战与原理---快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux电脑合盖后卡住了,解决ubun
- 下一篇: linux 所有命令无法使用