国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件

這篇具有很好參考價(jià)值的文章主要介紹了Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、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)

Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件,eureka,spring cloud

  • 服務(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è)中心)

Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件,eureka,spring cloud

(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

Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件,eureka,spring cloud

3、創(chuàng)建服務(wù)提供者

在spring-cloue-demo創(chuàng)建SpringBoot Moudle:demo-server-provider(服務(wù)提供者)

Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件,eureka,spring cloud

(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)用者)

Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件,eureka,spring cloud

(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

Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件,eureka,spring cloud

(5)Controller層

DeptController.java

Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件,eureka,spring cloud

(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/

Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件,eureka,spring cloud

(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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【spring cloud學(xué)習(xí)】2、Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)

    【spring cloud學(xué)習(xí)】2、Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)

    一套微服務(wù)架構(gòu)的系統(tǒng)由很多單一職責(zé)的服務(wù)單元組成,而每個(gè)服務(wù)單元又有眾多運(yùn)行實(shí)例。由于各服務(wù)單元顆粒度較小、數(shù)量眾多,相互之間呈現(xiàn)網(wǎng)狀依賴關(guān)系,因此需要服務(wù)注冊(cè)中心來(lái)統(tǒng)一管理微服務(wù)實(shí)例,維護(hù)各服務(wù)實(shí)例的健康狀態(tài)。 從宏觀角度,微服務(wù)架構(gòu)下的系統(tǒng)

    2024年02月10日
    瀏覽(230)
  • 【Spring Cloud】深入理解 Eureka 注冊(cè)中心的原理、服務(wù)的注冊(cè)與發(fā)現(xiàn)

    【Spring Cloud】深入理解 Eureka 注冊(cè)中心的原理、服務(wù)的注冊(cè)與發(fā)現(xiàn)

    在微服務(wù)架構(gòu)中,服務(wù)的注冊(cè)與發(fā)現(xiàn)是至關(guān)重要的一環(huán)。為了實(shí)現(xiàn)這一目標(biāo),Eureka 注冊(cè)中心應(yīng)運(yùn)而生。在本篇文章中,我們將深入理解 Eureka 注冊(cè)中心的原理,以及探討服務(wù)的注冊(cè)與發(fā)現(xiàn)機(jī)制。 在微服務(wù)的協(xié)作中,服務(wù)之間的遠(yuǎn)程調(diào)用是常見(jiàn)的需求。然而,使用傳統(tǒng)的 Rest

    2024年02月08日
    瀏覽(85)
  • Spring Cloud(Finchley版本)系列教程(一) 服務(wù)注冊(cè)與發(fā)現(xiàn)(eureka)

    Spring Cloud(Finchley版本)系列教程(一) 服務(wù)注冊(cè)與發(fā)現(xiàn)(eureka)

    Spring Cloud(Finchley版本)系列教程(一) 服務(wù)注冊(cè)與發(fā)現(xiàn)(eureka) 為了更好的瀏覽體驗(yàn),歡迎光顧勤奮的凱爾森同學(xué)個(gè)人博客http://www.huerpu.cc:7000 如有錯(cuò)誤懇請(qǐng)大家批評(píng)指正,與大家共同學(xué)習(xí)、一起成長(zhǎng),萬(wàn)分感謝。 一、構(gòu)建環(huán)境 Spring Cloud 的構(gòu)建工具可以使用 Maven 或 Gradle ,但 Ma

    2024年02月09日
    瀏覽(100)
  • Spring Cloud Eureka 服務(wù)注冊(cè)和服務(wù)發(fā)現(xiàn)超詳細(xì)(附加--源碼實(shí)現(xiàn)案例--及實(shí)現(xiàn)邏輯圖)

    Spring Cloud Eureka 服務(wù)注冊(cè)和服務(wù)發(fā)現(xiàn)超詳細(xì)(附加--源碼實(shí)現(xiàn)案例--及實(shí)現(xiàn)邏輯圖)

    這篇文章先講述一下Eureka的應(yīng)用場(chǎng)景、代碼實(shí)現(xiàn)案例,多個(gè)服務(wù)模塊注冊(cè)到Euraka中,服務(wù)之間的調(diào)用實(shí)現(xiàn)我會(huì)再下一篇文章中進(jìn)行講解! Eureka主要是做: 注冊(cè)發(fā)現(xiàn)中心 服務(wù)注冊(cè)與發(fā)現(xiàn)的組件 說(shuō)到Eureka不得不提到了CAP,那么什么是CAP原則呢,下面一起來(lái)看下! CAP 原則: 又稱

    2024年02月15日
    瀏覽(437)
  • Spring Cloud之一:注冊(cè)與發(fā)現(xiàn)-Eureka工程的創(chuàng)建

    Spring Cloud之一:注冊(cè)與發(fā)現(xiàn)-Eureka工程的創(chuàng)建

    ??系列目錄(持續(xù)更新。。。) Spring Cloud:什么是微服務(wù) Spring Cloud之一:注冊(cè)與發(fā)現(xiàn)-Eureka工程的創(chuàng)建 Spring Cloud之二:服務(wù)提供者注冊(cè)到Eureka Server Spring Cloud之三:Eureka Server添加認(rèn)證 Spring Cloud 之四:使用Feign實(shí)現(xiàn)微服務(wù)間的交互 Spring Cloud 之五:Feign使用Hystrix 目錄 環(huán)境 E

    2023年04月09日
    瀏覽(92)
  • 在Spring Cloud中使用組件Zuul網(wǎng)關(guān),并注冊(cè)到Eureka中去

    在Spring Cloud中使用組件Zuul網(wǎng)關(guān),并注冊(cè)到Eureka中去

    在上一篇中,我們搭建了Spring Cloud的父子模塊,并實(shí)現(xiàn)了一個(gè)Eureka子模塊的啟動(dòng),可以通過(guò)瀏覽器地址去訪問(wèn)Eureka主頁(yè)了,相信了解過(guò)的童鞋應(yīng)該看到,主頁(yè)上并未有任何服務(wù)去注冊(cè),那么我們就在這篇,使用zuul網(wǎng)關(guān)作為第一個(gè)注冊(cè)到Eureka注冊(cè)中心的服務(wù)吧。 上一篇博文地

    2024年02月05日
    瀏覽(98)
  • Spring Cloud Eureka Service Registry 服務(wù)注冊(cè)中心實(shí)踐

    作者:禪與計(jì)算機(jī)程序設(shè)計(jì)藝術(shù) 在分布式微服務(wù)架構(gòu)下,服務(wù)發(fā)現(xiàn)是保證應(yīng)用可用的關(guān)鍵組件之一。在Spring Cloud體系中,服務(wù)發(fā)現(xiàn)中心通過(guò)Netflix Eureka實(shí)現(xiàn)。 本文將介紹Spring Cloud Eureka服務(wù)注冊(cè)中心的機(jī)制、配置及使用方法,并通過(guò)實(shí)例對(duì)Eureka的功能及其局限性進(jìn)行詳細(xì)闡述

    2024年02月11日
    瀏覽(228)
  • 微服務(wù)系列-基于Spring Cloud Eureka進(jìn)行服務(wù)的注冊(cè)與消費(fèi)

    公眾號(hào)「架構(gòu)成長(zhǎng)指南」,專注于生產(chǎn)實(shí)踐、云原生、分布式系統(tǒng)、大數(shù)據(jù)技術(shù)分享。 使用 RestTemplate 的 Spring Boot 微服務(wù)通信示例 使用 WebClient 的 Spring Boot 微服務(wù)通信示例 使用 Spring Cloud Open Feign 的 Spring Boot 微服務(wù)通信示例 在本教程中,我們將學(xué)習(xí)如何在Spring boot微服務(wù)項(xiàng)

    2024年02月05日
    瀏覽(163)
  • Spring Cloud Netflix微服務(wù)組件-Eureka

    Spring Cloud Netflix微服務(wù)組件-Eureka

    目錄 CAP理論 注冊(cè)中心對(duì)比 為什么注冊(cè)中心更適合用AP? 分布式系統(tǒng)AP和CP如何取舍? Eureka核心功能點(diǎn) Euraka server啟動(dòng)的主線流程 總體流程圖 @EnableEurekaServer 流程圖 EurekaServerAutoConfiguration EurekaServerInitializerConfiguration Euraka client啟動(dòng)的主線流程 總體流程圖 EurekaClientAutoConfigurat

    2024年02月01日
    瀏覽(1757)
  • 微服務(wù) - Spring Cloud - Eureka Server單機(jī)和集群搭建、單機(jī)服務(wù)注冊(cè)和集群服務(wù)注冊(cè)

    Eureka 服務(wù)管理 Eureka是Netflix開發(fā)的服務(wù)發(fā)現(xiàn)框架,本身是一個(gè)基于REST的服務(wù),主要用于定位運(yùn)行在AWS域中的中間層服務(wù),以達(dá)到負(fù)載均衡和中間層服務(wù)故障轉(zhuǎn)移的目的。 SpringCloud將它集成在其子項(xiàng)目spring-cloud-netflix中,以實(shí)現(xiàn)SpringCloud的服務(wù)發(fā)現(xiàn)功能 Eureka服務(wù)注冊(cè)與發(fā)現(xiàn) Eu

    2024年02月13日
    瀏覽(93)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包