一 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》 下載nacos https://github.com/alibaba/nacos nacos-server-2.0.3.zip Windows 版 解壓後,資料庫新建nacos庫,將 X:\nacos\ ...
一 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
下載nacos https://github.com/alibaba/nacos nacos-server-2.0.3.zip Windows 版
解壓後,資料庫新建nacos庫,將 X:\nacos\conf 目錄下的 nacos-mysql.sql 文件執行 導入數據表與數據
修改文件 X:\nacos\conf 目錄下 application.properties
1 #*************** Config Module Related Configurations ***************# 2 ### If use MySQL as datasource: 3 spring.datasource.platform=mysql 4 5 ### Count of DB: 6 db.num=1 7 8 ### Connect URL of DB: 9 db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC 10 db.user.0=root 11 db.password.0=Root!234
修改文件 X:\nacos\bin 目錄下的 startup.cmd 標紅部分 為單機模式
set BASE_DIR="%BASE_DIR:~0,-5%"
set CUSTOM_SEARCH_LOCATIONS=file:%BASE_DIR%/conf/
set MODE="standalone" rem “cluster”
set FUNCTION_MODE="all"
set SERVER=nacos-server
set MODE_INDEX=-1
set FUNCTION_MODE_INDEX=-1
set SERVER_INDEX=-1
set EMBEDDED_STORAGE_INDEX=-1
set EMBEDDED_STORAGE=""
雙擊啟動 startup.cmd 第一行顯示 "nacos is starting with standalone" 為單機模式
"nacos is starting with standalone"
,--.
,--.'|
,--,: : | Nacos 2.0.3
,`--.'`| ' : ,---. Running in stand alone mode, All function modules
| : : | | ' ,'\ .--.--. Port: 8848
: | \ | : ,--.--. ,---. / / | / / ' Pid: 7892
| : ' '; | / \ / \. ; ,. :| : /`./ Console: http://127.0.0.1:8848/nacos/index.html
' ' ;. ;.--. .-. | / / '' | |: :| : ;_
| | | \ | \__\/: . .. ' / ' | .; : \ \ `. https://nacos.io
' : | ; .' ," .--.; |' ; :__| : | `----. \
| | '`--' / / ,. |' | '.'|\ \ / / /`--' /
' : | ; : .' \ : : `----' '--'. /
; |.' | , .-./\ \ / `--'---'
'---' `--`---' `----'
2023-04-02 08:56:24,132 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@2392212b' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2023-04-02 08:56:24,138 INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2023-04-02 08:56:24,746 INFO Tomcat initialized with port(s): 8848 (http)
2023-04-02 08:56:25,248 INFO Root WebApplicationContext: initialization completed in 7616 ms
2023-04-02 08:56:30,342 INFO Initializing ExecutorService 'applicationTaskExecutor'
2023-04-02 08:56:30,512 INFO Adding welcome page: class path resource [static/index.html]
2023-04-02 08:56:31,244 INFO Creating filter chain: Ant [pattern='/**'], []
2023-04-02 08:56:31,299 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@29f85fe1, org.springframework.security.web.context.SecurityContextPersistenceFilter@73d3e555, org.springframework.security.web.header.HeaderWriterFilter@bbf9e07, org.springframework.security.web.csrf.CsrfFilter@2af46afd, org.springframework.security.web.authentication.logout.LogoutFilter@3a08078c, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@44924587, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4487c0c2, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@43a0a32d, org.springframework.security.web.session.SessionManagementFilter@2b289ac9, org.springframework.security.web.access.ExceptionTranslationFilter@31ceba99]
2023-04-02 08:56:31,483 INFO Initializing ExecutorService 'taskScheduler'
2023-04-02 08:56:31,501 INFO Exposing 16 endpoint(s) beneath base path '/actuator'
2023-04-02 08:56:31,638 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'
2023-04-02 08:56:31,641 INFO Nacos started successfully in stand alone mode. use external storage
2023-04-02 08:56:45,175 INFO Initializing Servlet 'dispatcherServlet'
2023-04-02 08:56:45,189 INFO Completed initialization in 13 ms
訪問 http://localhost:8848/nacos/index.html 賬號密碼為 nacos
二 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
1.idea新建springboot項目《cloud》,保留 .idea pom.xml 兩個 ,其餘全部刪除。
修改pom.xml 文件 註意jdk版本對應springboot版本,springboot版本對應的springcloud版本
- 2022.x 分支對應的是 Spring Cloud 2022 與 Spring Boot 3.0.x,最低支持 JDK 17。
- 2021.x 分支對應的是 Spring Cloud 2021 與 Spring Boot 2.6.x,最低支持 JDK 1.8。
- 2020.0 分支對應的是 Spring Cloud 2020 與 Spring Boot 2.4.x,最低支持 JDK 1.8。
- 2.2.x 分支對應的是 Spring Cloud Hoxton 與 Spring Boot 2.2.x,最低支持 JDK 1.8。
- greenwich 分支對應的是 Spring Cloud Greenwich 與 Spring Boot 2.1.x,最低支持 JDK 1.8。
- finchley 分支對應的是 Spring Cloud Finchley 與 Spring Boot 2.0.x,最低支持 JDK 1.8。
- 1.x 分支對應的是 Spring Cloud Edgware 與 Spring Boot 1.x,最低支持 JDK 1.7。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.6.10</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.example</groupId> 12 <artifactId>cloud</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>cloud</name> 15 <description>cloud</description> 16 17 <packaging>pom</packaging> 18 19 <properties> 20 <java.version>1.8</java.version> 21 <spring-cloud-alibaba-dependencies-version>2021.0.5.0</spring-cloud-alibaba-dependencies-version> 22 </properties> 23 24 <dependencies> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter</artifactId> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-test</artifactId> 36 <scope>test</scope> 37 <exclusions> 38 <exclusion> 39 <groupId>org.junit.vintage</groupId> 40 <artifactId>junit-vintage-engine</artifactId> 41 </exclusion> 42 </exclusions> 43 </dependency> 44 </dependencies> 45 46 <build> 47 <plugins> 48 <plugin> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-maven-plugin</artifactId> 51 </plugin> 52 </plugins> 53 </build> 54 55 <dependencyManagement> 56 <dependencies> 57 <dependency> 58 <groupId>com.alibaba.cloud</groupId> 59 <artifactId>spring-cloud-alibaba-dependencies</artifactId> 60 <version>${spring-cloud-alibaba-dependencies-version}</version> 61 <type>pom</type> 62 <scope>import</scope> 63 </dependency> 64 </dependencies> 65 </dependencyManagement> 66 </project>
2.新建在cloud項目下 新建 maven model 《provider》 註意是maven項目
修改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.example</groupId> <artifactId>cloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.com.provider</groupId> <artifactId>provider</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>
新建 java項目目錄與application啟動類
application.yam文件內容如下
server: port: 8001 spring: application: name: provider cloud: nacos: discovery: server-addr: 127.0.0.1:8848
ProviderApplication.class 文件內容如下 (註意標紅部分)
package cn.com.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
創建一個TestController.class (ResultUtil.result()我自己創建的返回封裝,不重要,寫個字元串就“provider success” 就好!)
package cn.com.provider.controller; import cn.com.kity.common.result.ResultUtil; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/test") public class TestController { @GetMapping(value = "/test01") public Object eg01(){
//provider success return ResultUtil.result(); } }
啟動provider 訪問 localhost:8001/test/test01 返回 provider success
nacos服務管理下服務列表出現provider服務
3.同上創建consumer服務【maven項目】 consumer服務需要調用provider服務 則使用Feign交互
目錄結構為:
pom.xml如下: 多了三個 openfeign; loadbalancer; sentinel;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>com.example</groupId> 8 <artifactId>cloud</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 </parent> 11 12 <artifactId>consumer</artifactId> 13 14 <properties> 15 <maven.compiler.source>8</maven.compiler.source> 16 <maven.compiler.target>8</maven.compiler.target> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 </properties> 19 <dependencies> 20 <dependency> 21 <groupId>com.alibaba.cloud</groupId> 22 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> 23 </dependency> 24 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign --> 25 <dependency> 26 <groupId>org.springframework.cloud</groupId> 27 <artifactId>spring-cloud-starter-openfeign</artifactId> 28 <version>3.1.6</version> 29 </dependency> 30 <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-loadbalancer --> 31 <dependency> 32 <groupId>org.springframework.cloud</groupId> 33 <artifactId>spring-cloud-loadbalancer</artifactId> 34 <version>3.1.6</version> 35 </dependency> 36 <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel --> 37 <dependency> 38 <groupId>com.alibaba.cloud</groupId> 39 <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> 40 <exclusions> <!--除去該依賴,controller返回的結果才是json,不去除就xml--> 41 <exclusion> 42 <groupId>com.fasterxml.jackson.dataformat</groupId> 43 <artifactId>jackson-dataformat-xml</artifactId> 44 </exclusion> 45 </exclusions> 46 </dependency> 47 </dependencies> 48 </project>
application.yml內容如下:
1 server: 2 port: 8002 3 spring: 4 application: 5 name: consumer 6 cloud: 7 nacos: 8 discovery: 9 server-addr: 127.0.0.1:8848 10 feign: 11 sentinel: 12 enabled: true #開啟feign對sentinel的支持 服務降級
創建ConsumerApplication.class 內容如下:【註意標紅部分】
package cn.com.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
創建FeignFallBack.class 內容如下:
package cn.com.consumer.feign; import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.stereotype.Component; @Component public class FeignFallBack implements FallbackFactory<ConsumerFeignToProvider> { @Override public ConsumerFeignToProvider create(Throwable cause) { return () -> "provider 服務提供者掛了!!!!!!"; } }
創建ConsumerFeignToProvider.class內容如下:
標紅地方為provider服務名稱,
application.yml中
spring: application: name: provider
package cn.com.consumer.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "provider",fallbackFactory = FeignFallBack.class)
public interface ConsumerFeignToProvider {
@GetMapping("/test/test01")
String test01();
}
創建TestController.class內容如下
1 package cn.com.consumer.controller; 2 3 import cn.com.consumer.feign.ConsumerFeignToProvider; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 import javax.annotation.Resource; 9 10 @RestController 11 @RequestMapping(value = "/consumer") 12 public class TestController { 13 14 @Resource 15 ConsumerFeignToProvider cftp; 16 17 @GetMapping(value = "/eg01") 18 public Object eg01(){ 19 return "consumer01 掉用 ===" + cftp.test01() ; 20 } 21 22 }
啟動consumer服務:
nacos服務管理下服務列表出現consumer服務
訪問http://localhost:8002/consumer/eg01 返回
consumer01 掉用 ===provider success
停止provider服務 訪問http://localhost:8002/consumer/eg01 返回
consumer01 掉用 ===provider 服務提供者掛了!!!!!!
====================================================結束===================================================gateway+++conifg======================================================