博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java springcloud版b2b2c社交电商spring cloud分布式微服务-docker-feign(四)
阅读量:6991 次
发布时间:2019-06-27

本文共 6618 字,大约阅读时间需要 22 分钟。

简介

Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六。上一节,我们讨论了怎么通过,restTemlate调用cloud的生产者,实现起来还是比较复杂的,尤其是在消费复杂的Restful服务的时候,还需要进行一系列的转换,编解码等,使用Feign就完全不用考虑这个问题.。

一、feinn介绍

Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求,这整个调用过程和Dubbo的RPC非常类似。开发起来非常的优雅。

二、创建模块(microservice-consumer-movie-feign)

项目结构如下:

三、pom.xml

microservice-spring-cloud
com.jacky
1.0-SNAPSHOT
4.0.0
microservice-consumer-movie-feign
jar
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-feign
com.spotify
docker-maven-plugin
build-image
install
build
http://192.168.6.130:5678
true
${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}
java:openjdk-8-jdk-alpine
["java", "-jar", "/${project.build.finalName}.jar"]
/
${project.build.directory}
${project.build.finalName}.jar

四、配置文件application.yml

spring:  application:    name: microservice-consumer-movie-feignserver:  port: 7901eureka:  client:    healthcheck:      enabled: true    serviceUrl:      defaultZone: http://jacky:admin@peer1:8761/eureka/,http://jacky:admin@peer2:8762/eureka/,http://jacky:admin@peer3:8763/eureka/  instance:    prefer-ip-address: true    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

五、MovieController.java

package com.jacky.cloud.controller;import com.jacky.cloud.entity.User;import com.jacky.cloud.feign.UserFeignClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;/** * Created by jacky on 2017/7/14. */@RestControllerpublic class MovieController {    @Autowired    private UserFeignClient userFeignClient;    @GetMapping("/movie/{id}")    public User findById(@PathVariable Long id) {        return this.userFeignClient.findById(id);    }    @GetMapping("/test")    public User testPost(User user) {        return this.userFeignClient.postUser(user);    }    @GetMapping("/test-get")    public User testGet(User user) {        return this.userFeignClient.getUser(user);    }}

六、实体类User.java

package com.jacky.cloud.entity;import java.math.BigDecimal;public class User {  private Long id;  private String username;  private String name;  private Short age;  private BigDecimal balance;  public Long getId() {    return this.id;  }  public void setId(Long id) {    this.id = id;  }  public String getUsername() {    return this.username;  }  public void setUsername(String username) {    this.username = username;  }  public String getName() {    return this.name;  }  public void setName(String name) {    this.name = name;  }  public Short getAge() {    return this.age;  }  public void setAge(Short age) {    this.age = age;  }  public BigDecimal getBalance() {    return this.balance;  }  public void setBalance(BigDecimal balance) {    this.balance = balance;  }}

七、UserFeignClient.java

package com.jacky.cloud.feign;import com.jacky.cloud.entity.User;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;/** * Created by jacky on 2017/7/14. */@FeignClient("microservice-provider-user")public interface UserFeignClient {    /**     * 根据Id获得User     * 两个坑:1. @GetMapping不支持   2. @PathVariable得设置value     * @param id     * @return     */    @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)    public User findById(@PathVariable("id") Long id);    @RequestMapping(value = "/user", method = RequestMethod.POST)    public User postUser(@RequestBody User user);    // 该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。可能是我没找到相应的注解或使用方法错误。    // 也就是说复杂对象,feign一定要post的请求方式    @RequestMapping(value = "/get-user", method = RequestMethod.GET)    public User getUser(User user);}

八、启动类(MicroserviceSimpleConsumerMovieApplication.java)

package com.jacky.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableEurekaClient@EnableFeignClientspublic class MicroserviceSimpleConsumerMovieApplication {  public static void main(String[] args) {    SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args);  }}

需要JAVASpring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六

标签:springcloud,spring cloud,springcloud微服务,b2b2c,o2o电子商务,java多用户商城系统

转载于:https://www.cnblogs.com/sunnysunny/p/10836932.html

你可能感兴趣的文章
044 HIVE中的几种排序
查看>>
Javascript 运行上下文和作用域链
查看>>
Tomcat服务器安装
查看>>
Atcoder训练计划
查看>>
新一日三省吾身
查看>>
ABP框架 - 验证数据传输对象
查看>>
是否值得付费?Oracle,Open JDK等四大JVM性能全面对比
查看>>
49岁程序员:如何靠编程实现财务、精神双自由?
查看>>
get荣耀YOYO智能音箱打电话的新技能 感觉赚了1个亿
查看>>
明明长得一样,怎么就不认识你了呢
查看>>
支付宝风控闹“乌龙”,网友却纷纷点赞叫好
查看>>
2017年度华为感动之星巅峰之旅活动完美收官
查看>>
高瓴资本大幅减持虎牙:半年时间闪进闪出
查看>>
亚马逊CEO贝索斯离婚:拥有身家1369亿美元 已结婚25年
查看>>
这一次升级等了七年 PCIe 4.0终于发布了
查看>>
发改委:以稳汽车消费来托住商品消费“大头”
查看>>
华夏幸福前锋拉维奇旧伤复发 已顺利完成手术
查看>>
Python入门:Python 2与Python3有什么区别?
查看>>
淘宝天猫购物再不要这样子了我刚亏了8块钱
查看>>
如何跟踪信息流广告转化数据?
查看>>