springcloud eureka服务器搭建(SpringCloud微服务之Eureka集成OpenFeign)

springcloud eureka服务器搭建(SpringCloud微服务之Eureka集成OpenFeign)(1)

版本说明

  • spring-cloud-starter-openfeign : 3.0.3
  • spring-cloud-starter-netflix-eureka-client :2.2.9.RELEASE
  • spring-cloud-starter-netflix-ribbon : 2.2.9.RELEASE
  • Spring-boot-starter-parent :2.3.12.RELEASE
  • spring-cloud-dependencies :Hoxton.SR12

这里需要注意SpringBoot 与 SpringCloud的版本匹配,请参考:https://spring.io/projects/spring-cloud

springcloud eureka服务器搭建(SpringCloud微服务之Eureka集成OpenFeign)(2)

注册中心

可搭建单机版用于 Demo 测试

创建微服务提供者Maven配置

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>

创建启动类,增加注解

这里暂时不需要数据库

@EnableeurekaClient @EnableDiscoveryClient @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class HttpFeignProvider8083 { public static void main(String[] args) { SpringApplication.run(HttpFeignProvider8083.class, args); } }

创建一个接口类型用于测试

这里很简单的两个URLMapping,简单模拟两个场景:

  1. 根据用户id查询用户名;
  2. 根据产品id查询产品名;

@RestController public class HttpFeignProviderController { @GetMapping("/user/getUserNameById") public String getUserNameById(@RequestParam("userId") String userId){ return "张学友"; } @GetMapping("/product/getProductNameById") public String getProductNameById(@RequestParam("productId") String productId){ return "iphone 13"; } }

配置文件

server.port=8083 spring.application.name=http-feign-provider8083 eureka.client.serviceUrl.defaultZone=http://host80:8080/eureka/,http://host81:8081/eureka/,http://host82:8082/eureka/ eureka.client.serviceUrl.healthcheck=true # 若不配置该项,使用 spring.application.name 显示 #eureka.instance.instance-id=http-feign-provider8083 # actuator info 配置 info.app.name=${spring.application.name} info.company.name=pengld

创建微服务消费者Maven配置

这里增加了一个依赖:spring-cloud-starter-netflix-ribbon,用于服务调用时负载均衡。

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies>

创建启动类

同样暂时不需要数据库,比服务提供者多一个注解:@EnableFeignClients

@EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class HttpFeignConsumer8084 { public static void main(String[] args) { SpringApplication.run(HttpFeignConsumer8084.class, args); } }

创建feignclient

对比到前文服务提供者的controller,这里模拟两个场景,实际开发中根据功能、模块等维度划分不同的client用于区分。

UserClient

@FeignClient(name="http://http-feign-provider8083",contextId = "UserClient",path = "/user") public interface UserClient { @GetMapping("/getUserNameById") String getUserNameById(@RequestParam("userId") String userId); }

ProductClient

@FeignClient(name="http://http-feign-provider8083",contextId = "ProductClient",path = "/product") public interface ProductClient { @GetMapping("/getProductNameById") String getProductNameById(@RequestParam("productId") String productId); }

服务消费侧创建接口模拟远程调用

@RestController public class ConsumerController { @Autowired UserClient userClient; @Autowired ProductClient productClient; @GetMapping("/getUserNameById") public String getUserNameById(@RequestParam("userId") String userId){ return userClient.getUserNameById(userId); } @GetMapping("/getProductNameById") public String getProductNameById(@RequestParam("productId") String productId){ return productClient.getProductNameById(productId); } }

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页