溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringCloud之服務(wù)注冊與發(fā)現(xiàn)Eureka+客戶端Feign

發(fā)布時(shí)間:2020-07-28 14:03:03 來源:網(wǎng)絡(luò) 閱讀:326 作者:nineteens 欄目:編程語言

  前言

  SpringCloud 是微服務(wù)中的翹楚,最佳的落地方案。

  Eureka 作為注冊中心,是 SpringCloud 體系中最重要最核心的組件之一。

  Feign 使用接口加注解的方式調(diào)用服務(wù),配合 Eureka 還能實(shí)現(xiàn)負(fù)載均衡

  源碼

  GitHub

  環(huán)境

  JDK 1.8.0 +

  Maven 3.0 +

  SpringBoot 2.0.3

  SpringCloud Finchley.RELEASE

  開發(fā)工具

  IntelliJ IDEA

  正文

  commons 工程

  commons 工程 - POM 文件

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4.0.0

  com.zwc

  springcloud-eureka-commons

  1.0

  springcloud-eureka-commons

  公用工程

  jar

  UTF-8

  1.8

  Cairo-SR3

  Finchley.RELEASE

  io.spring.platform

  platform-bom

  ${platform-bom.version}

  pom

  import

  org.springframework.cloud

  spring-cloud-dependencies

  ${spring-cloud-dependencies.version}

  pom

  import

  org.springframework.boot

  spring-boot-maven-plugin

  配置一些共用依賴

  commons 工程 - 項(xiàng)目結(jié)構(gòu)

  service 工程

  此工程下有四個(gè)模塊:一個(gè)注冊中心,兩個(gè)提供者以及一個(gè)消費(fèi)者

  registry-service(注冊中心)

  registry-service - POM 文件

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4.0.0

  com.zwc

  springcloud-eureka-service

  1.0

  com.zwc

  springcloud-eureka-registry-service

  0.0.1-SNAPSHOT

  springcloud-eureka-registry-service

  注冊中心

  jar

  com.zwc

  springcloud-eureka-commons

  1.0

  org.springframework.cloud

  spring-cloud-starter-netflix-eureka-server

  org.springframework.boot

  spring-boot-maven-plugin

  主要是加入 spring-cloud-starter-netflix-eureka-server 依賴

  registry-service - application.yml 配置文件

  # 端口

  server:

  port: 8761

  # 應(yīng)用名稱

  spring:

  application:

  name: eurka-server

  eureka:

  instance:

  # 使用 ip 代替實(shí)例名

  prefer-ip-address: true

  # 實(shí)例的主機(jī)名

  hostname: ${spring.cloud.client.ip-address}

  # 實(shí)例的 ID 規(guī)則

  instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

  client:

  # 是否向注冊中心注冊自己

  registerWithEureka: false

  # 是否向注冊中心獲取注冊信息

  fetchRegistry: false

  serviceUrl:

  # 注冊中心地址

  defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

  這里使用了默認(rèn)的 8761 端口,當(dāng)然也可以更改,不過在發(fā)現(xiàn)調(diào)用服務(wù)端的注冊中心地址端口要與它一致

  registry-service - 啟動(dòng)類

  package com.zwc;

  import org.springframework.boot.SpringApplication;

  import org.springframework.boot.autoconfigure.SpringBootApplication;

  import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

  @SpringBootApplication

  @EnableEurekaServer

  public class SpringcloudEurekaRegistryServiceApplication {

  public static void main(String[] args) {

  SpringApplication.run(SpringcloudEurekaRegistryServiceApplication.class, args);

  }

  }

  在啟動(dòng)類中添加 @EnableEurekaServer 注解表示此工程是注冊中心

  registry-service - 啟動(dòng)項(xiàng)目

  1. 項(xiàng)目啟動(dòng)成功后訪問 http://localhost:8761/ 即可看到 eureka-server 主頁面

  Provider(提供者)

  Provider - POM 文件

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4.0.0

  com.zwc

  springcloud-eureka-providerfirst-service

  1.0

  com.zwc

  springcloud-eureka-providerfirst-service-core

  1.0

  springcloud-eureka-providerfirst-service-core

  提供者一號服務(wù)工程 - 核心

  jar

  com.zwc

  springcloud-eureka-commons

  1.0

  com.zwc

  springcloud-eureka-providerfirst-service-api

  1.0

  org.springframework.cloud

  spring-cloud-starter-netflix-eureka-client

  org.springframework.boot

  spring-boot-maven-plugin

  主要是加入 spring-cloud-starter-netflix-eureka-client 依賴

  Provider - application.yml 配置文件

  # 端口

  server:

  port: 8090

  # 應(yīng)用名稱

  spring:

  application:

  name: say-hello

  eureka:

  instance:

  # 使用 ip 代替實(shí)例名

  prefer-ip-address: true

  # 實(shí)例的主機(jī)名

  hostname: ${spring.cloud.client.ip-address}

  # 實(shí)例的 ID 規(guī)則

  instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

  client:

  serviceUrl:

  # 注冊中心地址

  defaultZone: http://${eureka.instance.hostname}:8761/eureka/

  注意此處配置注冊中心地址的端口為 8761 也就是上面注冊中心工程配置的端口

  有兩個(gè)消費(fèi)者工程,只有此處的端口不一致,此處端口為 8090,另一個(gè)端口為 8091。就不再贅述

  兩個(gè)消費(fèi)者工程作用是為了達(dá)到負(fù)載均衡的效果

  spring.application.name:應(yīng)用名稱,被消費(fèi)者調(diào)用時(shí)需要用到

  Provider - controller 前端控制器

  package com.zwc.providerfirst.controller;

  import org.springframework.beans.factory.annotation.Value;

  import org.springframework.web.bind.annotation.RequestMapping;

  import org.springframework.web.bind.annotation.RestController;

  /**

  * @ClassName SayHelloController

  * @Desc TODO Say Hello

  * @Date 2019/5/15 15:28

  * @Version 1.0

  */

  @RestController

  public class SayHelloController {

  /*

  * @ClassName SayHelloController

  * @Desc TODO 讀取配置文件中的端口

  * @Date 2019/5/15 15:49

  * @Version 1.0

  */

  @Value("${server.port}")

  private String port;

  /*

  * @ClassName SayHelloController

  * @Desc TODO Say Hello

  * @Date 2019/5/15 15:30

  * @Version 1.0

  */

  @RequestMapping("/hello")

  public String hello(){

  return "Hello Spring Cloud!!!port:" + port;

  }

  }

  提供一個(gè)服務(wù):輸出 Hello 和端口

  Provider - 啟動(dòng)類

  package com.zwc;

  import org.springframework.boot.SpringApplication;

  import org.springframework.boot.autoconfigure.SpringBootApplication;

  import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

  @SpringBootApplication

  @EnableEurekaClient

  public class SpringcloudEurekaProviderfirstServiceCoreApplication {

  public static void main(String[] args) {

  SpringApplication.run(SpringcloudEurekaProviderfirstServiceCoreApplication.class, args);

  }

  }

  添加 @EnableEurekaClient 注解表示此工程可以向注冊中心提供服務(wù)

  Provider - 啟動(dòng)項(xiàng)目

  1. 項(xiàng)目啟動(dòng)成功后訪問 http://localhost:8090/hello 看到輸出內(nèi)容 'Hello Spring Cloud!!!port:8090'

  2. 刷新 http://localhost:8761/(注冊中心)可以看到服務(wù)已經(jīng)被注冊進(jìn)來了

  3. 同理,還有一個(gè)提供者工程只是端口不一致,也啟動(dòng)起來

  4. 項(xiàng)目啟動(dòng)成功后訪問 http://localhost:8091/hello 看到輸出內(nèi)容 'Hello Spring Cloud!!!port:8091'

  5. 再次刷新 http://localhost:8761/(注冊中心)可以看到相同的服務(wù)有兩個(gè)提供者

  Consumer(消費(fèi)者)

  Consumer - POM 文件

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4.0.0

  com.zwc

  springcloud-eureka-consumer-service

  1.0

  com.zwc

  springcloud-eureka-consumer-service-core

  1.0

  springcloud-eureka-consumer-service-core

  消費(fèi)者服務(wù)工程 - 核心

  jar

  com.zwc

  springcloud-eureka-commons

  1.0

  com.zwc

  springcloud-eureka-consumer-service-api

  1.0

  org.springframework.cloud

  spring-cloud-starter-netflix-eureka-client

  org.springframework.cloud

  spring-cloud-starter-openfeign

  org.springframework.boot

  spring-boot-maven-plugin

  與提供者一致,需要加入 spring-cloud-starter-netflix-eureka-client 依賴

  還需要加入 Feign 的起步依賴 spring-cloud-starter-openfeign

  Consumer - application.yml 配置文件

  # 端口

  server:

  port: 8080

  # 應(yīng)用名稱

  spring:

  application:

  name: service-feign

  eureka:

  instance:

  # 使用 ip 代替實(shí)例名

  prefer-ip-address: true

  # 實(shí)例的主機(jī)名

  hostname: ${spring.cloud.client.ip-address}

  # 實(shí)例的 ID 規(guī)則

  instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

  client:無錫看婦科的醫(yī)院 http://www.ytsg120.cn/

  serviceUrl:

  # 注冊中心地址

  defaultZone: http://${eureka.instance.hostname}:8761/eureka/

  注意此處配置注冊中心地址的端口為 8761 也就是上面注冊中心工程配置的端口

  spring.application.name:應(yīng)用名稱,被消費(fèi)者調(diào)用時(shí)需要用到,它在消費(fèi)的同時(shí)也可以被消費(fèi)

  Consumer - 服務(wù)調(diào)用

  package com.zwc.consumer.api.feign;

  import org.springframework.cloud.openfeign.FeignClient;

  import org.springframework.web.bind.annotation.RequestMapping;

  /**

  * @ClassName FeignApi

  * @Desc TODO 使用 Feign 調(diào)用 Api - 接口

  * @Date 2019/5/15 16:11

  * @Version 1.0

  */

  @FeignClient("say-hello")

  public interface FeignApi {

  /*

  * @ClassName FeignApi

  * @Desc TODO 通過 say-hello 服務(wù)名調(diào)用 /hello 方法

  * @Date 2019/5/15 16:17

  * @Version 1.0

  */

  @RequestMapping("/hello")

  String hello();

  }

  通過 @FeignClient("say-hello") 注解來指定調(diào)用哪個(gè)服務(wù)

  say-hello 就是提供者的 spring.application.name:應(yīng)用名稱

  String hello();:可以發(fā)現(xiàn),此方法就是提供者 SayHelloController 中的方法,只不過這里要定義成接口

  注意要與提供者具有相同返回值,相同方法名以及相同參數(shù)

  Consumer - controller 前端控制器

  package com.zwc.consumer.controller;

  import com.zwc.consumer.api.feign.FeignApi;

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.web.bind.annotation.RequestMapping;

  import org.springframework.web.bind.annotation.RestController;

  import javax.annotation.Resource;

  /**

  * @ClassName FeignController

  * @Desc TODO 使用 Feign 調(diào)用 Api - 前端控制器

  * @Date 2019/5/15 16:18

  * @Version 1.0

  */

  @RestController

  public class FeignController {

  @Autowired(required = false)

  private FeignApi feignApi;

  /*

  * @ClassName FeignController

  * @Desc TODO 調(diào)用 Say Hello 方法

  * @Date 2019/5/15 16:20

  * @Version 1.0

  */

  @RequestMapping("/feign")

  public String feign(){

  return feignApi.hello();

  }

  }

  使用 @Autowired 注解裝配 Bean,通過此 Bean 中的方法調(diào)用服務(wù)

  此類對外暴露接口,調(diào)用的實(shí)則是提供者的服務(wù)

  Consumer - 啟動(dòng)類

  package com.zwc;

  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 SpringcloudEurekaConsumerServiceCoreApplication {

  public static void main(String[] args) {

  SpringApplication.run(SpringcloudEurekaConsumerServiceCoreApplication.class, args);

  }

  }

  添加 @EnableEurekaClient 注解表示此工程可以向注冊中心提供服務(wù)

  添加 @EnableFeignClients 注解表示開啟 Feign 功能進(jìn)行遠(yuǎn)程調(diào)用

  Consumer - 啟動(dòng)項(xiàng)目

  1. 項(xiàng)目啟動(dòng)成功后多次訪問 http://localhost:8080/feign

  2. 可以發(fā)現(xiàn)輪流輸出 'Hello Spring Cloud!!!port:8090' 和 'Hello Spring Cloud!!!port:8091'

  3. 此時(shí)已經(jīng)達(dá)到了負(fù)載均衡的效果

  4. 再次刷新 http://localhost:8761/(注冊中心)可以看到此時(shí)多了一個(gè)消費(fèi)者

  service 工程 - 項(xiàng)目結(jié)構(gòu)

  把多工程項(xiàng)目使用 IntelliJ IDEA 打開

  把項(xiàng)目從 GitHub 中下載到你的本地

  打開 IntelliJ IDEA

  點(diǎn)擊 File -> Open

  打開你下載到本地的項(xiàng)目目錄

  springcloud-eureka -> springcloud-eureka-service(選擇打開此工程)

  打開 service 工程后

  再次點(diǎn)擊 File -> Project Structrue

  選擇 Modules,點(diǎn)擊 '+' 符號

  點(diǎn)擊 Import Module

  還是打開你下載到本地的項(xiàng)目目錄

  springcloud-eureka -> springcloud-eureka-commons -> pom.xml

  點(diǎn)擊 OK

  點(diǎn)擊 Next,F(xiàn)inish

  點(diǎn)擊 Apply,OK

  擴(kuò)展

  CentOS7中使用Docker簡單部署SpringCloud項(xiàng)目


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI