日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java springcloud版b2b2c社交电商spring cloud分布式微服务-docker-feign(四)

發布時間:2024/9/21 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(四)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。