java springcloud版b2b2c社交电商spring cloud分布式微服务-docker-feign(四)
簡介
Spring Cloud大型企業分布式微服務云構建的B2B2C電子商務平臺源碼請加企鵝求求:一零三八七七四六二六。上一節,我們討論了怎么通過,restTemlate調用cloud的生產者,實現起來還是比較復雜的,尤其是在消費復雜的Restful服務的時候,還需要進行一系列的轉換,編解碼等,使用Feign就完全不用考慮這個問題.。
一、feinn介紹
Feign是一種聲明式、模板化的HTTP客戶端。在Spring Cloud中使用Feign, 我們可以做到使用HTTP請求遠程服務時能與調用本地方法一樣的編碼體驗,開發者完全感知不到這是遠程方法,更感知不到這是個HTTP請求,這整個調用過程和Dubbo的RPC非常類似。開發起來非常的優雅。
二、創建模塊(microservice-consumer-movie-feign)
項目結構如下:
三、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>microservice-spring-cloud</artifactId><groupId>com.jacky</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>microservice-consumer-movie-feign</artifactId><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</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-feign</artifactId></dependency><!--<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-oauth2</artifactId></dependency>--></dependencies><build><plugins><plugin><groupId>com.spotify</groupId><artifactId>docker-maven-plugin</artifactId><executions><!--設置在執行maven 的install時構建鏡像--><execution><id>build-image</id><phase>install</phase><goals><goal>build</goal></goals></execution></executions><configuration><!--安裝了docker的主機,并且打開了api remote接口設置--><dockerHost>http://192.168.6.130:5678</dockerHost><pushImage>true</pushImage><!--設置上傳鏡像到私有倉庫,需要docker設置指定私有倉庫地址--><!--鏡像名稱--><imageName>${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}</imageName><!--鏡像的基礎版本--><baseImage>java:openjdk-8-jdk-alpine</baseImage><!--鏡像啟動參數--><entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint><resources><resource><targetPath>/</targetPath><directory>${project.build.directory}</directory><include>${project.build.finalName}.jar</include></resource></resources></configuration></plugin></plugins></build> </project>四、配置文件application.yml
spring:application:name: microservice-consumer-movie-feign server:port: 7901 eureka:client:healthcheck:enabled: trueserviceUrl:defaultZone: http://jacky:admin@peer1:8761/eureka/,http://jacky:admin@peer2:8762/eureka/,http://jacky:admin@peer3:8763/eureka/ instance:prefer-ip-address: trueinstance-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.*/ @RestController public class MovieController {@Autowiredprivate 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 @EnableFeignClients public 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
總結
以上是生活随笔為你收集整理的java springcloud版b2b2c社交电商spring cloud分布式微服务-docker-feign(四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分布式架构发展
- 下一篇: 如何改变UITableViewCell的