前言:
開發工具:IntelliJ IDEA 2020版 (Ultimate Edition)
框架:spring boot 、spring cloud
搭建一套spring cloud微服務系統,實作服務之間的呼叫,
需要搭建一個父工程springcloud-test,一個服務注冊中心eureka-server,兩個微服務cloud-client,cloud-provider,
兩個微服務均注冊到服務注冊中心,
一.搭建父專案

2.

3.

(1)刪掉src目錄

(2)定義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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chen.test</groupId>
<artifactId>springcloud-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
</project>
二.搭建eureka-server注冊中心
1.

2.

3.

4.

5.

6.
(1)定義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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--引入父工程依賴-->
<parent>
<groupId>com.chen.test</groupId>
<artifactId>springcloud-test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.chen.test</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
(2)刪掉test檔案夾(自己設定,可有可無)
(3)啟動加注解@EnableEurekaServer(開啟eureka服務)
package com.chen.test.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
(4)定義組態檔
組態檔改為application.yml(可有可無,看自己喜好)
server:
port: 8080
spring:
application:
#應用名稱(在注冊中顯示的)
name: eureka-server
eureka:
client:
#此客戶端是否獲取eureka服務器注冊表上的注冊資訊,默認為true
fetch-registry: false
#實體是否在eureka服務器上注冊自己的資訊以供其他服務發現,默認為true,即自己注冊自己,
register-with-eureka: true
#與Eureka注冊服務中心的通信zone和url地址
serviceUrl:
#http://localhost:8080/eureka/eureka
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
#服務注冊中心實體的主機名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8080
server:
#設為false,關閉自我保護,即Eureka server在云心光器件會去統計心跳失敗比例在15分鐘之內是否低于85%,如果低于85%,EurekaServer
#會將這些事例保護起來,讓這些事例不會過期,但是在保護器內如果剛哈這個服務提供者非正常下線了,此時服務消費者會拿到一個無效的服務
#實體,此時呼叫會失敗,對于這個問題需要服務消費者端有一些容錯機制,如重試、斷路器等;
enable-self-preservation: false
#掃描失效服務的間隔時間(單位是毫秒,摩恩是60*1000),即60s
eviction-interval-timer-in-ms: 10000
(5)測驗

三.搭建提供者服務
1.

2.

3.

4.

5.

6.
(1)定義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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--引入父工程依賴-->
<parent>
<groupId>com.chen.test</groupId>
<artifactId>springcloud-test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.chen.test</groupId>
<artifactId>cloud-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloud-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
(2)洗掉test檔案加(可有可無)
(3)修改組態檔為application.yml(可有可無)
spring:
application:
name: cloud-provider
server:
port: 8081
eureka:
client:
#此客戶端是否獲取eureka服務器注冊表上的注冊資訊,默認為true
fetch-registry: false
#實體是否在eureka服務器上注冊自己的資訊以供其他服務發現,默認為true,即自己注冊自己,
register-with-eureka: true
service-url:
#defaultZone 這個是不會提示的,此處需要自己寫
#實際上屬性應該是service-url,這個屬性是個map(key-value)格式;當key是defaultZone的時候才能被決議;所以這里沒有提示,
#但是自己還需要寫一個defaultZone;
defaultZone: http://localhost:8080/eureka
#服務注冊中心實體的主機名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8081
(4)啟動類加注解@EnableEurekaClient
package com.chen.test.cloudprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class CloudProviderApplication {
public static void main(String[] args) {
SpringApplication.run(CloudProviderApplication.class, args);
}
}
(5)測驗

四.搭建消費者服務
1.

2.

3.

4.

5.

(1)定義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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--引入父工程依賴-->
<parent>
<groupId>com.chen.test</groupId>
<artifactId>springcloud-test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.chen.demo</groupId>
<artifactId>cloud-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloud-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
(2)洗掉test檔案夾(可有可無)
(3)修改組態檔為application.yml(可有可無)
server:
#定義埠號
port: 8082
spring:
application:
#定義應用名稱,即服務名稱
name: cloud-client
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka
#服務注冊中心實體的主機名
instance:
hostname: 127.0.0.1
prefer-ip-address: true
instance-id: 127.0.0.1:8082
(4)啟動類加注解@EnableEurekaClient
package com.chen.demo.cloudclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class CloudClientApplication {
public static void main(String[] args) {
SpringApplication.run(CloudClientApplication.class, args);
}
}
(5)測驗

(6)在父pom中加上
<modules>
<module>eureka-server</module>
<module>cloud-provider</module>
<module>cloud-client</module>
</modules>

五.實作服務之間的呼叫
1.在cloud-provider中創建controller包和service包
package com.chen.test.cloudprovider.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chen.test.cloudprovider.service.HelloService;
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/getHello")
public String getHello(){
return helloService.getHello();
}
}
package com.chen.test.cloudprovider.service;
public interface HelloService {
String getHello();
}
package com.chen.test.cloudprovider.service.impl;
import com.chen.test.cloudprovider.service.HelloService;
import org.springframework.stereotype.Service;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String getHello() {
return "你好兄弟";
}
}
(2)測驗

(3)在cloud-client中創建controller包和service包
package com.chen.demo.cloudclient.controller;
import com.chen.demo.cloudclient.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/Hello")
public class HelloClient {
@Autowired
private HelloService helloService;
@GetMapping("/getClient")
public String getClient(){
return helloService.getProduct();
}
}
package com.chen.demo.cloudclient.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
//name 為product專案中application.yml組態檔中的application.name;
//path 為product專案中application.yml組態檔中的context.path;
@FeignClient(name = "cloud-provider",path ="/hello" )
//@Componet注解最好加上,不加idea會顯示有錯誤,但是不影響系統運行;
@Component
public interface HelloService {
@RequestMapping(value = "getHello")
String getProduct();
}
特別注意
在啟動類加上注解@EnableFeignClients
package com.chen.demo.cloudclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class CloudClientApplication {
public static void main(String[] args) {
SpringApplication.run(CloudClientApplication.class, args);
}
}
(4)測驗

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/245205.html
標籤:java
