SpringCloud之服务注册与发现Eureka+客户端Feign

  前言

  SpringCloud 是微服务中的翘楚,最佳的落地方案。

  Eureka 作为注册中心,是 SpringCloud 体系中最重要最核心的组件之一。

  Feign 使用接口加注解的方式调用服务,配合 Eureka 还能实现负载均衡。

  源码

  GitHub

  环境

  JDK 1.8.0 +

  Maven 3.0 +

  SpringBoot 2.0.3

  SpringCloud Finchley.RELEASE

  开发工具

  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 工程 - 项目结构

  service 工程

  此工程下有四个模块:一个注册中心,两个提供者以及一个消费者

  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

  # 应用名称

  spring:

  application:

  name: eurka-server

  eureka:

  instance:

  # 使用 ip 代替实例名

  prefer-ip-address: true

  # 实例的主机名

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

  # 实例的 ID 规则

  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/

  这里使用了默认的 8761 端口,当然也可以更改,不过在发现调用服务端的注册中心地址端口要与它一致

  registry-service - 启动类

  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);

  }

  }

  在启动类中添加 @EnableEurekaServer 注解表示此工程是注册中心

  registry-service - 启动项目

  1. 项目启动成功后访问 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

  提供者一号服务工程 - 核心

  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

  # 应用名称

  spring:

  application:

  name: say-hello

  eureka:

  instance:

  # 使用 ip 代替实例名

  prefer-ip-address: true

  # 实例的主机名

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

  # 实例的 ID 规则

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

  client:

  serviceUrl:

  # 注册中心地址

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

  注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口

  有两个消费者工程,只有此处的端口不一致,此处端口为 8090,另一个端口为 8091。就不再赘述

  两个消费者工程作用是为了达到负载均衡的效果

  spring.application.name:应用名称,被消费者调用时需要用到

  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;

  }

  }

  提供一个服务:输出 Hello 和端口

  Provider - 启动类

  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 注解表示此工程可以向注册中心提供服务

  Provider - 启动项目

  1. 项目启动成功后访问 http://localhost:8090/hello 看到输出内容 'Hello Spring Cloud!!!port:8090'

  2. 刷新 http://localhost:8761/(注册中心)可以看到服务已经被注册进来了

  3. 同理,还有一个提供者工程只是端口不一致,也启动起来

  4. 项目启动成功后访问 http://localhost:8091/hello 看到输出内容 'Hello Spring Cloud!!!port:8091'

  5. 再次刷新 http://localhost:8761/(注册中心)可以看到相同的服务有两个提供者

  Consumer(消费者)

  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

  消费者服务工程 - 核心

  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

  # 应用名称

  spring:

  application:

  name: service-feign

  eureka:

  instance:

  # 使用 ip 代替实例名

  prefer-ip-address: true

  # 实例的主机名

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

  # 实例的 ID 规则

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

  client:无锡看妇科的医院 http://www.ytsg120.cn/

  serviceUrl:

  # 注册中心地址

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

  注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口

  spring.application.name:应用名称,被消费者调用时需要用到,它在消费的同时也可以被消费

  Consumer - 服务调用

  package com.zwc.consumer.api.feign;

  import org.springframework.cloud.openfeign.FeignClient;

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

  /**

  * @ClassName FeignApi

  * @Desc TODO 使用 Feign 调用 Api - 接口

  * @Date 2019/5/15 16:11

  * @Version 1.0

  */

  @FeignClient("say-hello")

  public interface FeignApi {

  /*

  * @ClassName FeignApi

  * @Desc TODO 通过 say-hello 服务名调用 /hello 方法

  * @Date 2019/5/15 16:17

  * @Version 1.0

  */

  @RequestMapping("/hello")

  String hello();

  }

  通过 @FeignClient("say-hello") 注解来指定调用哪个服务

  say-hello 就是提供者的 spring.application.name:应用名称

  String hello();:可以发现,此方法就是提供者 SayHelloController 中的方法,只不过这里要定义成接口

  注意要与提供者具有相同返回值,相同方法名以及相同参数

  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 调用 Api - 前端控制器

  * @Date 2019/5/15 16:18

  * @Version 1.0

  */

  @RestController

  public class FeignController {

  @Autowired(required = false)

  private FeignApi feignApi;

  /*

  * @ClassName FeignController

  * @Desc TODO 调用 Say Hello 方法

  * @Date 2019/5/15 16:20

  * @Version 1.0

  */

  @RequestMapping("/feign")

  public String feign(){

  return feignApi.hello();

  }

  }

  使用 @Autowired 注解装配 Bean,通过此 Bean 中的方法调用服务

  此类对外暴露接口,调用的实则是提供者的服务

  Consumer - 启动类

  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 注解表示此工程可以向注册中心提供服务

  添加 @EnableFeignClients 注解表示开启 Feign 功能进行远程调用

  Consumer - 启动项目

  1. 项目启动成功后多次访问 http://localhost:8080/feign

  2. 可以发现轮流输出 'Hello Spring Cloud!!!port:8090' 和 'Hello Spring Cloud!!!port:8091'

  3. 此时已经达到了负载均衡的效果

  4. 再次刷新 http://localhost:8761/(注册中心)可以看到此时多了一个消费者

  service 工程 - 项目结构

  把多工程项目使用 IntelliJ IDEA 打开

  把项目从 GitHub 中下载到你的本地

  打开 IntelliJ IDEA

  点击 File -> Open

  打开你下载到本地的项目目录

  springcloud-eureka -> springcloud-eureka-service(选择打开此工程)

  打开 service 工程后

  再次点击 File -> Project Structrue

  选择 Modules,点击 '+' 符号

  点击 Import Module

  还是打开你下载到本地的项目目录

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

  点击 OK

  点击 Next,Finish

  点击 Apply,OK

  扩展

  CentOS7中使用Docker简单部署SpringCloud项目


猜你喜欢

转载自blog.51cto.com/14335413/2425144