目录
Ribbon实现服务通信
创建工程product-basic-provider(提供者)
创建工程product-img-provider(提供者)
创建工程product-detail-api(消费者)
OpenFeign实现服务通信
创建工程product-detail-api-feign(消费者)
Ribbon实现服务通信
服务提供者:product-basic-provider、product-img-provider
服务消费者:product-detail-api
创建工程product-basic-provider(提供者)
controller
@RestController
@RequestMapping("/product-basic")
public class ProductBasicController {
@GetMapping("/get")
public Product get(String productId){
Product product = new Product(productId,"娃哈哈","好喝不贵!");
return product;
}
@DeleteMapping("/delete/{id}")
public Product delete(@PathVariable("id") String productId){
return product;
}
@PostMapping("/add")
public Product add(@RequestBody Product product){
return product;
}
@PostMapping("/check")
public User check(@RequestBody User user){
System.out.println(user.getUsername());
System.out.println(user.getPassword());
return user;
}
//测试feign get请求传参
@GetMapping("/test")
public Product test(Product product){
System.out.println(product);
return product;
}
}
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
private String productId;
private String productName;
private String productDesc;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String username;
private String password;
}
启动类
@SpringBootApplication
@EnableDiscoveryClient//开启服务注册与发现
public class ProductBasicProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProductBasicProviderApplication.class, args);
}
}
application.yml
server:
port: 9001
spring:
application:
name: product-basic-provider #指定当前服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务器地址
username: nacos
password: nacos
创建工程product-img-provider(提供者)
controller
@RestController
@RequestMapping("/product-img")
public class ProductImgController {
@GetMapping("/getImg")
public List<ProductImg> listImgByProductId(String productId){
List<ProductImg> imgList = new ArrayList<>();
//假数据
imgList.add(new ProductImg("1",productId,"http:ip//img/a.jpg"));
imgList.add(new ProductImg("2",productId,"http:ip//img/b.jpg"));
imgList.add(new ProductImg("3",productId,"http:ip//img/c.jpg"));
imgList.add(new ProductImg("4",productId,"http:ip//img/d.jpg"));
return imgList;
}
}
实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductImg {
private String imgId;
private String productId;
private String imgUrl;
}
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ProductImgProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProductImgProviderApplication.class, args);
}
}
application.yml
server:
port: 9002
spring:
application:
name: product-img-provider #指定当前服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务器地址
username: nacos
password: nacos
创建工程product-detail-api(消费者)
Ribbon配置
@Configuration
public class RibbonClientConfig {
@Bean
//RestTemplate发送请求都要通过ribbon去(nacos注册中心)获取服务实例列表
@LoadBalanced //启动ribbon组件(负载均衡组件)
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
controller
@RestController
@RequestMapping("/product-api")
@CrossOrigin//开启允许跨域
public class ProductDetailController {
@Autowired
private ProductDetailService productDetailService;
@GetMapping("/get/{id}")
public ResultVo getProductDetail(@PathVariable("id") String productId){
return productDetailService.getProduct(productId);
}
}
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultVo<T> {
private Integer code;
private String msg;
private T data;
}
service接口
public interface ProductDetailService {
public ResultVo getProduct(String productId);
}
实现类
@Service
public class ProductDetailServiceImpl implements ProductDetailService {
@Autowired
private RestTemplate restTemplate;
@Override
public ResultVo getProduct(String productId) {
//1、调用product-basic-provider查询商品基本信息
String url = "http://product-basic-provider/product-basic/get?productId=" + productId;
// Product product = restTemplate.getForObject(url, Product.class);
String url3 = "http://product-basic-provider/product-basic/delete/" + productId;
// restTemplate.delete(url3);
String url4 = "http://product-basic-provider/product-basic/add";
// ResponseEntity<Product> product1 = restTemplate.postForEntity(url4, "obj", Product.class);
String url5 = "http://product-basic-provider/product-basic/check";
User user= restTemplate.patchForObject(url5, new User("admin", "123456"), User.class);
// 2、调用product-img-provider查询商品图片信息
String url2 = "http://product-img-provider/product-img/getImg?productId=" + productId;
// List<ProductImg> productImgs= restTemplate.getForObject(url2,List.class);
return new ResultVo(0,"success",user);
}
}
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ProductDetailApiApplication {
public static void main(String[] args) {
SpringApplication.run(ProductDetailApiApplication.class, args);
}
}
application.yml
server:
port: 8001
spring:
application:
name: product-detail-api #指定当前服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务器地址
username: nacos
password: nacos
pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.green</groupId>
<artifactId>product-detail-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>product-detail-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.green.productdetailapi.ProductDetailApiApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
启动服务
使用postma测试
http://localhost:8001/product-api/get/1
返回结果:
{
"code": 0,
"msg": "success",
"data": {
"username": "admin",
"password": "123456"
}
}
OpenFeign实现服务通信
服务提供者:product-basic-provider、product-img-provider
服务消费者:product-detail-api-feign
创建工程product-detail-api-feign(消费者)
ProductBasicClient
@FeignClient("product-basic-provider")
public interface ProductBasicClient {
@GetMapping("/product-basic/get")
public Product get(@RequestParam("productId") String productId);
@DeleteMapping("/product-basic/delete/{id}")
public Product delete(@PathVariable("id") String productId);
@PostMapping("/product-basic/add")
public Product add(@RequestBody Product product);
@PostMapping("/product-basic/check")
public User check(User user);
//测试feign get请求传参
@GetMapping("/product-basic/test")
public Product test(@RequestParam("productId") String productId,@RequestParam("productName") String productName);
}
ProductImgClient
@FeignClient("product-img-provider")
public interface ProductImgClient {
@GetMapping("/product-img/getImg")
public List<ProductImg> listImgByProductId(@RequestParam("productId") String productId);
}
ProductDetailServiceImpl实现类
@Service
public class ProductDetailServiceImpl implements ProductDetailService {
@Autowired
private ProductBasicClient productBasicClient;
@Autowired
private ProductImgClient productImgClient;
@Override
public ResultVo getProduct(String productId) {
//1、调用product-basic-provider查询商品基本信息
Product product = productBasicClient.get(productId);
// 2、调用product-img-provider查询商品图片信息
List<ProductImg> productImgs = productImgClient.listImgByProductId(productId);
product.setProductImgList(productImgs);
return new ResultVo(0,"success",product);
}
}
application.yml
server:
port: 8002
spring:
application:
name: product-detail-api-feign #指定当前服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务器地址
username: nacos
password: nacos
使用postma测试
http://localhost:8002/product-api/get/1
测试结果:
{
"code": 0,
"msg": "success",
"data": {
"productId": "1",
"productName": "娃哈哈",
"productDesc": "好喝不贵!",
"productImgList": [
{
"imgId": "1",
"productId": "1",
"imgUrl": "http:ip//img/a.jpg"
},
{
"imgId": "2",
"productId": "1",
"imgUrl": "http:ip//img/b.jpg"
},
{
"imgId": "3",
"productId": "1",
"imgUrl": "http:ip//img/c.jpg"
},
{
"imgId": "4",
"productId": "1",
"imgUrl": "http:ip//img/d.jpg"
}
]
}
}