一、Eureka是什么
Eureka 一詞來(lái)源于古希臘詞匯,是“發(fā)現(xiàn)了”的意思。在軟件領(lǐng)域,Eureka 是 Netflix 公司開發(fā)的一款開源的服務(wù)注冊(cè)與發(fā)現(xiàn)組件。
Spring Cloud 將 Eureka 與 Netflix 中的其他開源服務(wù)組件(例如 Ribbon、Feign 以及 Hystrix 等)一起整合進(jìn) Spring Cloud Netflix 模塊中,整合后的組件全稱為 Spring Cloud Netflix Eureka。
Eureka 是 Spring Cloud Netflix 模塊的子模塊,它是 Spring Cloud 對(duì) Netflix Eureka 的二次封裝,主要負(fù)責(zé) Spring Cloud 的服務(wù)注冊(cè)與發(fā)現(xiàn)功能。
Spring Cloud 使用 Spring Boot 思想為 Eureka 增加了自動(dòng)化配置,開發(fā)人員只需要引入相關(guān)依賴和注解,就能將 Spring Boot 構(gòu)建的微服務(wù)輕松地與 Eureka 進(jìn)行整合。
二、Eureka 兩大組件
Eureka 采用 CS(Client/Server,客戶端/服務(wù)器) 架構(gòu),它包括以下兩大組件:
-
Eureka Server:Eureka 服務(wù)注冊(cè)中心,主要用于提供服務(wù)注冊(cè)功能。當(dāng)微服務(wù)啟動(dòng)時(shí),會(huì)將自己的服務(wù)注冊(cè)到 Eureka Server。Eureka Server 維護(hù)了一個(gè)可用服務(wù)列表,存儲(chǔ)了所有注冊(cè)到 Eureka Server 的可用服務(wù)的信息,這些可用服務(wù)可以在 Eureka Server 的管理界面中直觀看到。
-
Eureka Client:Eureka 客戶端,通常指的是微服務(wù)系統(tǒng)中各個(gè)微服務(wù),主要用于和 Eureka Server 進(jìn)行交互。在微服務(wù)應(yīng)用啟動(dòng)后,Eureka Client 會(huì)向 Eureka Server 發(fā)送心跳(默認(rèn)周期為 30 秒)。若 Eureka Server 在多個(gè)心跳周期內(nèi)沒(méi)有接收到某個(gè) Eureka Client 的心跳,Eureka Server 將它從可用服務(wù)列表中移除(默認(rèn) 90 秒)。
注:“心跳”指的是一段定時(shí)發(fā)送的自定義信息,讓對(duì)方知道自己“存活”,以確保連接的有效性。大部分 CS 架構(gòu)的
應(yīng)用程序都采用了心跳機(jī)制,服務(wù)端和客戶端都可以發(fā)心跳。通常情況下是客戶端向服務(wù)器端發(fā)送心跳包,服務(wù)端
用于判斷客戶端是否在線。
三、Eureka 服務(wù)注冊(cè)與發(fā)現(xiàn)
-
服務(wù)注冊(cè)中心(Register Service):它是一個(gè) Eureka Server,用于提供服務(wù)注冊(cè)和發(fā)現(xiàn)功能。
-
服務(wù)提供者(Provider Service):它是一個(gè) Eureka Client,用于提供服務(wù)。它將自己提供的服務(wù)注冊(cè)到服務(wù)注冊(cè)中心,以供服務(wù)消費(fèi)者發(fā)現(xiàn)。
-
服務(wù)消費(fèi)者(Consumer Service):它是一個(gè) Eureka Client,用于消費(fèi)服務(wù)。它可以從服務(wù)注冊(cè)中心獲取服務(wù)列表,調(diào)用所需的服務(wù)。
Eureka 實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)的流程如下:
(1)搭建一個(gè) Eureka Server 作為服務(wù)注冊(cè)中心;
(2)服務(wù)提供者 Eureka Client 啟動(dòng)時(shí),會(huì)把當(dāng)前服務(wù)器的信息以服務(wù)名(spring.application.name)的方式注冊(cè)
到服務(wù)注冊(cè)中心;
(3)服務(wù)消費(fèi)者 Eureka Client 啟動(dòng)時(shí),也會(huì)向服務(wù)注冊(cè)中心注冊(cè);
(4)服務(wù)消費(fèi)者還會(huì)獲取一份可用服務(wù)列表,該列表中包含了所有注冊(cè)到服務(wù)注冊(cè)中心的服務(wù)信息(包括服務(wù)提供者和自身的信息);
(5)在獲得了可用服務(wù)列表后,服務(wù)消費(fèi)者通過(guò) HTTP 或消息中間件遠(yuǎn)程調(diào)用服務(wù)提供者提供的服務(wù)。
四、示例 1
1、創(chuàng)建Maven項(xiàng)目spring-cloue-demo
刪除src目錄,保留pom.xml文件
2、創(chuàng)建注冊(cè)中心
在spring-cloue-demo創(chuàng)建SpringBoot Moudle:eureka-server(注冊(cè)中心)
(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>
? ? ?<groupId>com.cloud</groupId>
? ? ?<artifactId>eureka-server</artifactId>
? ? ?<version>0.0.1-SNAPSHOT</version>
? ? ?<packaging>war</packaging>
? ? ?<name>eureka-server</name>
? ? ?<description>eureka-server</description>
? ? ?<properties>
? ? ? ? ?<java.version>11</java.version>
? ? ? ? ?<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
? ? ? ? ?<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
? ? ? ? ?<spring-boot.version>2.6.13</spring-boot.version>
? ? ? ? ?<spring-cloud.version>2021.0.5</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-tomcat</artifactId>
? ? ? ? ? ? ?<scope>provided</scope>
? ? ? ? ?</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>
? ? ? ? ? ? ?<dependency>
? ? ? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? ?<artifactId>spring-boot-dependencies</artifactId>
? ? ? ? ? ? ? ? ?<version>${spring-boot.version}</version>
? ? ? ? ? ? ? ? ?<type>pom</type>
? ? ? ? ? ? ? ? ?<scope>import</scope>
? ? ? ? ? ? ?</dependency>
? ? ? ? ?</dependencies>
? ? ?</dependencyManagement>
??
? ? ?<build>
? ? ? ? ?<plugins>
? ? ? ? ? ? ?<plugin>
? ? ? ? ? ? ? ? ?<groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? ? ? ?<artifactId>maven-compiler-plugin</artifactId>
? ? ? ? ? ? ? ? ?<version>3.8.1</version>
? ? ? ? ? ? ? ? ?<configuration>
? ? ? ? ? ? ? ? ? ? ?<source>1.8</source>
? ? ? ? ? ? ? ? ? ? ?<target>1.8</target>
? ? ? ? ? ? ? ? ? ? ?<encoding>UTF-8</encoding>
? ? ? ? ? ? ? ? ?</configuration>
? ? ? ? ? ? ?</plugin>
? ? ? ? ? ? ?<plugin>
? ? ? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? ?<artifactId>spring-boot-maven-plugin</artifactId>
? ? ? ? ? ? ? ? ?<version>${spring-boot.version}</version>
? ? ? ? ? ? ? ? ?<configuration>
? ? ? ? ? ? ? ? ? ? ?<mainClass>com.cloud.eurekaserver.EurekaServerApplication</mainClass>
? ? ? ? ? ? ? ? ? ? ?<skip>true</skip>
? ? ? ? ? ? ? ? ?</configuration>
? ? ? ? ? ? ? ? ?<executions>
? ? ? ? ? ? ? ? ? ? ?<execution>
? ? ? ? ? ? ? ? ? ? ? ? ?<id>repackage</id>
? ? ? ? ? ? ? ? ? ? ? ? ?<goals>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<goal>repackage</goal>
? ? ? ? ? ? ? ? ? ? ? ? ?</goals>
? ? ? ? ? ? ? ? ? ? ?</execution>
? ? ? ? ? ? ? ? ?</executions>
? ? ? ? ? ? ?</plugin>
? ? ? ? ?</plugins>
? ? ?</build>
?</project>
(2)application.yml
server:
? port: 8089 #服務(wù)端口
??
?spring:
? application:
? ? name: eurekaServer #服務(wù)名稱
??
?eureka:
? instance:
? ? hostname: localhost
? client:
? ? register-with-eureka: false #false表示不向注冊(cè)中心注冊(cè)自己
? ? fetch-registry: false #false表示自己就是注冊(cè)中心,我的職責(zé)就是維護(hù)服務(wù)實(shí)例,并不負(fù)責(zé)數(shù)據(jù)同步
? ? service-url:
? ? ? defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #注冊(cè)中心地址
(3)啟動(dòng)類
?@SpringBootApplication
?@EnableEurekaServer ?//EurekaServer注解,表示這是Eureka注冊(cè)中心
?public class EurekaServerApplication {
??
? ? ?public static void main(String[] args) {
? ? ? ? ?SpringApplication.run(EurekaServerApplication.class, args);
? ? }
?}
(4)運(yùn)行注冊(cè)中心:
http://localhost:8089
3、創(chuàng)建服務(wù)提供者
在spring-cloue-demo創(chuàng)建SpringBoot Moudle:demo-server-provider(服務(wù)提供者)
(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>
? ? ?<groupId>com.cloud</groupId>
? ? ?<artifactId>demo-server-provider</artifactId>
? ? ?<version>0.0.1-SNAPSHOT</version>
? ? ?<packaging>war</packaging>
? ? ?<name>demo-server-provider</name>
? ? ?<description>demo-server-provider</description>
? ? ?<properties>
? ? ? ? ?<java.version>11</java.version>
? ? ? ? ?<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
? ? ? ? ?<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
? ? ? ? ?<spring-boot.version>2.6.13</spring-boot.version>
? ? ? ? ?<spring-cloud.version>2021.0.5</spring-cloud.version>
? ? ?</properties>
? ? ?<dependencies>
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ?<artifactId>spring-boot-starter-web</artifactId>
? ? ? ? ?</dependency>
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>org.mybatis.spring.boot</groupId>
? ? ? ? ? ? ?<artifactId>mybatis-spring-boot-starter</artifactId>
? ? ? ? ? ? ?<version>2.2.2</version>
? ? ? ? ?</dependency>
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? ?<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
? ? ? ? ? ? ?<version>3.1.3</version>
? ? ? ? ?</dependency>
??
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>mysql</groupId>
? ? ? ? ? ? ?<artifactId>mysql-connector-java</artifactId>
? ? ? ? ? ? ?<scope>runtime</scope>
? ? ? ? ?</dependency>
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>org.projectlombok</groupId>
? ? ? ? ? ? ?<artifactId>lombok</artifactId>
? ? ? ? ? ? ?<optional>true</optional>
? ? ? ? ?</dependency>
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ?<artifactId>spring-boot-starter-tomcat</artifactId>
? ? ? ? ? ? ?<scope>provided</scope>
? ? ? ? ?</dependency>
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ?<artifactId>spring-boot-starter-test</artifactId>
? ? ? ? ? ? ?<scope>test</scope>
? ? ? ? ?</dependency>
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>com.baomidou</groupId>
? ? ? ? ? ? ?<artifactId>mybatis-plus-core</artifactId>
? ? ? ? ? ? ?<version>3.5.3.1</version>
? ? ? ? ?</dependency>
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>com.baomidou</groupId>
? ? ? ? ? ? ?<artifactId>mybatis-plus-extension</artifactId>
? ? ? ? ? ? ?<version>3.5.3.1</version>
? ? ? ? ?</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>
? ? ? ? ? ? ?<dependency>
? ? ? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? ?<artifactId>spring-boot-dependencies</artifactId>
? ? ? ? ? ? ? ? ?<version>${spring-boot.version}</version>
? ? ? ? ? ? ? ? ?<type>pom</type>
? ? ? ? ? ? ? ? ?<scope>import</scope>
? ? ? ? ? ? ?</dependency>
? ? ? ? ?</dependencies>
? ? ?</dependencyManagement>
??
? ? ?<build>
? ? ? ? ?<plugins>
? ? ? ? ? ? ?<plugin>
? ? ? ? ? ? ? ? ?<groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? ? ? ?<artifactId>maven-compiler-plugin</artifactId>
? ? ? ? ? ? ? ? ?<version>3.8.1</version>
? ? ? ? ? ? ? ? ?<configuration>
? ? ? ? ? ? ? ? ? ? ?<source>1.8</source>
? ? ? ? ? ? ? ? ? ? ?<target>1.8</target>
? ? ? ? ? ? ? ? ? ? ?<encoding>UTF-8</encoding>
? ? ? ? ? ? ? ? ?</configuration>
? ? ? ? ? ? ?</plugin>
? ? ? ? ? ? ?<plugin>
? ? ? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? ?<artifactId>spring-boot-maven-plugin</artifactId>
? ? ? ? ? ? ? ? ?<version>${spring-boot.version}</version>
? ? ? ? ? ? ? ? ?<configuration>
? ? ? ? ? ? ? ? ? ? ?<mainClass>com.cloud.demoserverprovider.DemoServerProviderApplication</mainClass>
? ? ? ? ? ? ? ? ? ? ?<skip>true</skip>
? ? ? ? ? ? ? ? ?</configuration>
? ? ? ? ? ? ? ? ?<executions>
? ? ? ? ? ? ? ? ? ? ?<execution>
? ? ? ? ? ? ? ? ? ? ? ? ?<id>repackage</id>
? ? ? ? ? ? ? ? ? ? ? ? ?<goals>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<goal>repackage</goal>
? ? ? ? ? ? ? ? ? ? ? ? ?</goals>
? ? ? ? ? ? ? ? ? ? ?</execution>
? ? ? ? ? ? ? ? ?</executions>
? ? ? ? ? ? ?</plugin>
? ? ? ? ?</plugins>
? ? ?</build>
?</project>
(2)application.yml
?
server:
? port: 8763 #服務(wù)端口號(hào)
??
?spring:
? application:
? ? name: payProvider #RestTemplate進(jìn)行服務(wù)調(diào)用的名稱地址
??
? datasource: #數(shù)據(jù)庫(kù)連接配置
? ? driver-class-name: com.mysql.cj.jdbc.Driver
? ? url: ?jdbc:mysql://127.0.0.1:3306/dbms?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
? ? username: root
? ? password: 123456
??
?eureka:
? client:
? ? register-with-eureka: true #注冊(cè)到注冊(cè)中心
? ? fetch-registry: true #獲取服務(wù)列表
? ? service-url:
? ? ? defaultZone: http://localhost:8089/eureka/ #注冊(cè)中心地址
? instance: #服務(wù)實(shí)例名稱
? ? instance-id: server-provider
? ? prefer-ip-address: true
??
?mybatis:
? mapper-locations: classpath:mapper/*.xml #mapper映射文件位置
(3)實(shí)體類
Dept.java
@TableName(value ="dept")
?@Data
?@AllArgsConstructor
?@NoArgsConstructor
?public class Dept implements Serializable {
? ?
? ? ?@TableId(type = IdType.AUTO)
? ? ?private Integer deptNo;
? ? ?private String deptName;
? ? ?private String dbSource;
??
? ? ?@TableField(exist = false)
? ? ?private static final long serialVersionUID = 1L;
?}
(4)Mapper接口
DeptMapper.java
@Mapper
?public interface DeptMapper {
? ? ?List<Dept> findAll();
?}
(5)Mapper接口的映射文件
resources/mapper/DeptMapper.xml
<mapper namespace="com.cloud.demoserverprovider.mapper.DeptMapper">
? ? ?<resultMap id="BaseResultMap" type="com.cloud.demoserverprovider.entity.Dept">
? ? ? ? ? ? ?<id property="deptNo" column="dept_no" jdbcType="INTEGER"/>
? ? ? ? ? ? ?<result property="deptName" column="dept_name" jdbcType="VARCHAR"/>
? ? ? ? ? ? ?<result property="dbSource" column="db_source" jdbcType="VARCHAR"/>
? ? ?</resultMap>
??
? ? ?<sql id="Base_Column_List">
? ? ? ? dept_no,dept_name,db_source
? ? ?</sql>
??
? ? ?<select id="findAll" resultMap="BaseResultMap">
? ? ? ? select * from dept;
? ? ?</select>
?</mapper>
(6)Service層
DeptService.java
public interface DeptService{
? ? ?List<Dept> selectAll();
?}
(6)服務(wù)層實(shí)現(xiàn)類
DeptServiceImpl.java
@Service
?public class DeptServiceImpl implements DeptService{
??
? ? ?@Autowired
? ? ?private DeptMapper deptMapper;
??
? ? ?@Override
? ? ?public List<Dept> selectAll() {
? ? ? ? ?return deptMapper.findAll();
? ? }
?}
(7)Controller層
DeptController.java
@RestController
?@Slf4j
?public class DeptController {
? ? ?@Autowired
? ? ?private DeptService deptService;
??
? ? ?@Value("${server.port}")
? ? ?private String serverPort;
??
? ? ?@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
? ? ?public List<Dept> list(){
? ? ? ? ?return deptService.selectAll();
? ? }
?}
(8)啟動(dòng)類
@SpringBootApplication
?@EnableEurekaClient
?@EnableDiscoveryClient
?public class DemoServerProviderApplication {
??
? ? ?public static void main(String[] args) {
? ? ? ? ?SpringApplication.run(DemoServerProviderApplication.class, args);
? ? ? ? ?System.out.println("服務(wù)提供者啟動(dòng)成功");
? ? }
?}
4、創(chuàng)建服務(wù)調(diào)用者
在spring-cloue-demo創(chuàng)建SpringBoot Moudle:demo-cloud-consumer(服務(wù)調(diào)用者)
(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>
? ? ?<groupId>com.cloud</groupId>
? ? ?<artifactId>demo-cloud-consumer</artifactId>
? ? ?<version>0.0.1-SNAPSHOT</version>
? ? ?<packaging>war</packaging>
? ? ?<name>demo-cloud-consumer</name>
? ? ?<description>demo-cloud-consumer</description>
? ? ?<properties>
? ? ? ? ?<java.version>11</java.version>
? ? ? ? ?<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
? ? ? ? ?<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
? ? ? ? ?<spring-boot.version>2.6.13</spring-boot.version>
? ? ? ? ?<spring-cloud.version>2021.0.5</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>
? ? ? ? ? ? ?<version>3.1.3</version>
? ? ? ? ?</dependency>
??
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ?<artifactId>spring-boot-starter-tomcat</artifactId>
? ? ? ? ? ? ?<scope>provided</scope>
? ? ? ? ?</dependency>
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ?<artifactId>spring-boot-starter-test</artifactId>
? ? ? ? ? ? ?<scope>test</scope>
? ? ? ? ?</dependency>
? ? ? ? ?<dependency>
? ? ? ? ? ? ?<groupId>com.cloud</groupId>
? ? ? ? ? ? ?<artifactId>demo-server-provider</artifactId>
? ? ? ? ? ? ?<version>0.0.1-SNAPSHOT</version>
? ? ? ? ? ? ?<scope>compile</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>
? ? ? ? ? ? ?<dependency>
? ? ? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? ?<artifactId>spring-boot-dependencies</artifactId>
? ? ? ? ? ? ? ? ?<version>${spring-boot.version}</version>
? ? ? ? ? ? ? ? ?<type>pom</type>
? ? ? ? ? ? ? ? ?<scope>import</scope>
? ? ? ? ? ? ?</dependency>
? ? ? ? ?</dependencies>
? ? ?</dependencyManagement>
??
? ? ?<build>
? ? ? ? ?<plugins>
? ? ? ? ? ? ?<plugin>
? ? ? ? ? ? ? ? ?<groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? ? ? ?<artifactId>maven-compiler-plugin</artifactId>
? ? ? ? ? ? ? ? ?<version>3.8.1</version>
? ? ? ? ? ? ? ? ?<configuration>
? ? ? ? ? ? ? ? ? ? ?<source>1.8</source>
? ? ? ? ? ? ? ? ? ? ?<target>1.8</target>
? ? ? ? ? ? ? ? ? ? ?<encoding>UTF-8</encoding>
? ? ? ? ? ? ? ? ?</configuration>
? ? ? ? ? ? ?</plugin>
? ? ? ? ? ? ?<plugin>
? ? ? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? ?<artifactId>spring-boot-maven-plugin</artifactId>
? ? ? ? ? ? ? ? ?<version>${spring-boot.version}</version>
? ? ? ? ? ? ? ? ?<configuration>
? ? ? ? ? ? ? ? ? ? ?<mainClass>com.cloud.democloudconsumer.DemoCloudConsumerApplication</mainClass>
? ? ? ? ? ? ? ? ? ? ?<skip>true</skip>
? ? ? ? ? ? ? ? ?</configuration>
? ? ? ? ? ? ? ? ?<executions>
? ? ? ? ? ? ? ? ? ? ?<execution>
? ? ? ? ? ? ? ? ? ? ? ? ?<id>repackage</id>
? ? ? ? ? ? ? ? ? ? ? ? ?<goals>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<goal>repackage</goal>
? ? ? ? ? ? ? ? ? ? ? ? ?</goals>
? ? ? ? ? ? ? ? ? ? ?</execution>
? ? ? ? ? ? ? ? ?</executions>
? ? ? ? ? ? ?</plugin>
? ? ? ? ?</plugins>
? ? ?</build>
?</project>
(2)application.yml
server:
? port: 8764 # 服務(wù)端口
??
?spring:
? application:
? ? name: demo-service-consumer # 服務(wù)名
? mvc:
? ? servlet:
? ? ? path: /deptInfo #servlet請(qǐng)求路徑
? datasource:
? ? driver-class-name: com.mysql.cj.jdbc.Driver
? ? url: jdbc:mysql://localhost:3306/dbms?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
? ? username: root
? ? password: 123456
??
?eureka:
? client:
? ? register-with-eureka: true # 是否將自己注冊(cè)到Eureka服務(wù)中,默認(rèn)為true
? ? fetch-registry: true # 是否從Eureka中獲取注冊(cè)信息,默認(rèn)為true
? ? service-url:
? ? ? defaultZone: http://localhost:8089/eureka/ # Eureka服務(wù)端的地址
(3)負(fù)載均衡配置類
utils/ConfigBean.java
@Configuration
?public class ConfigBean {
? ? ?@Bean
? ? ?@LoadBalanced
? ? ?RestTemplate restTemplate(RestTemplateBuilder builder){
? ? ? ? ?return builder.build();
? ? }
?}
注:新版本的Eureka繼承了Ribbon,不需要再導(dǎo)入Ribbon,否則會(huì)發(fā)生沖突
(4)Service層
服務(wù)層接口DeptService.java
public interface DeptService {
? ? ?public List<Dept> getDeptList();
?}
服務(wù)層實(shí)現(xiàn)類DeptServiceImpl.java
(5)Controller層
DeptController.java
(6)啟動(dòng)類
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class DemoCloudConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DemoCloudConsumerApplication.class, args);
System.out.println("服務(wù)調(diào)用者啟動(dòng)成功");
}
}
5、運(yùn)行項(xiàng)目
(1)依次啟動(dòng)eureka-server(注冊(cè)中心)、demo-server-provider(服務(wù)提供者)、demo-cloud-consumer(服
務(wù)調(diào)用者)
(2)在瀏覽器中訪問(wèn)Eureka注冊(cè)中心:http://localhost:8089/
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-768045.html
(3)在瀏覽器中訪問(wèn)服務(wù)調(diào)用者的請(qǐng)求:http://localhost:8764/deptInfo/con/all文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-768045.html
到了這里,關(guān)于Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!