eureka hostname作用_SpringCloud之服务注册与发现Eureka+客户端Feign

前言

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

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

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

源码

GitHub地址:https://github.com/intomylife/SpringCloud

环境

  • JDK 1.8.0 +
  • Maven 3.0 +
  • SpringBoot 2.0.3
  • SpringCloud Finchley.RELEASE

开发工具

  • IntelliJ IDEA

正文

commons 工程

commons 工程 - POM 文件

<?xml version="1.0" encoding="UTF-8"?>4.0.0com.zwc springcloud-eureka-commons 1.0springcloud-eureka-commons公用工程jarUTF-81.8Cairo-SR3Finchley.RELEASEio.spring.platform platform-bom ${platform-bom.version}pomimportorg.springframework.cloud spring-cloud-dependencies ${spring-cloud-dependencies.version}pomimportorg.springframework.boot spring-boot-maven-plugin 

配置一些共用依赖

commons 工程 - 项目结构

a492e04fb8784448e1e28ca496ca4c53.png

service 工程

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

registry-server(注册中心)

registry-server - POM 文件

<?xml version="1.0" encoding="UTF-8"?>4.0.0com.zwc springcloud-eureka-service 1.0com.zwc springcloud-eureka-registry-service 0.0.1-SNAPSHOTspringcloud-eureka-registry-service注册中心jarcom.zwc springcloud-eureka-commons 1.0org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-maven-plugin 

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

registry-server - 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-server - 启动类

package com.zwc; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication@EnableEurekaServerpublic class SpringcloudEurekaRegistryServiceApplication {  public static void main(String[] args) { SpringApplication.run(SpringcloudEurekaRegistryServiceApplication.class, args); } }

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

registry-server - 启动项目

1. 项目启动成功后访问 http://localhost:8761/ 即可看到 eureka-server 主页面

7615b176f2e81d609a2b70acacffc221.png

Provider(提供者)

Provider - POM 文件

<?xml version="1.0" encoding="UTF-8"?>4.0.0com.zwc springcloud-eureka-providerfirst-service 1.0com.zwc springcloud-eureka-providerfirst-service-core 1.0springcloud-eureka-providerfirst-service-core提供者一号服务工程 - 核心jarcom.zwc springcloud-eureka-commons 1.0com.zwc springcloud-eureka-providerfirst-service-api 1.0org.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 */@RestControllerpublic 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@EnableEurekaClientpublic 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/(注册中心)可以看到服务已经被注册进来了

e0439b30534d4483034766e9a5599dc9.png

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

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

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

5dbe586ff7ae0aec4b6372ab354b0c72.png

Consumer(消费者)

Consumer - POM 文件

<?xml version="1.0" encoding="UTF-8"?>4.0.0com.zwc springcloud-eureka-consumer-service 1.0com.zwc springcloud-eureka-consumer-service-core 1.0springcloud-eureka-consumer-service-core消费者服务工程 - 核心jarcom.zwc springcloud-eureka-commons 1.0com.zwc springcloud-eureka-consumer-service-api 1.0org.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: 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 */@RestControllerpublic 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@EnableFeignClientspublic class SpringcloudEurekaConsumerServiceCoreApplication {  public static void main(String[] args) { SpringApplication.run(SpringcloudEurekaConsumerServiceCoreApplication.class, args); } }
  • 添加 @EnableEurekaClient 注解表示此工程可以提供服务或调用服务
  • 添加 @EnableFeignClients 注解表示开启 Feign 功能进行远程调用

Consumer - 启动项目

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

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

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

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

844a0f1747e9da86e6c76e2eef70e38d.png
f67984edb4cf887136ced64499857893.png

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

  1. 把项目从 GitHub 中下载到你的本地
  2. 打开 IntelliJ IDEA
  3. 点击 File -> Open
  4. 打开你下载到本地的项目目录
  5. springcloud-eureka -> springcloud-eureka-service(选择打开此工程)
  6. 打开 service 工程后
  7. 再次点击 File -> Project Structrue
  8. 选择 Modules,点击 '+' 符号
  9. 点击 Import Module
  10. 还是打开你下载到本地的项目目录
  11. springcloud-eureka -> springcloud-eureka-commons -> pom.xml
  12. 点击 OK
  13. 点击 Next,Finish
  14. 点击 Apply,OK

竟然都看到最后了,给小编点个关注吧,小编还会持续更新的,只收藏不点关注的都是在耍流氓!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值