Eureka Client的搭建跟Service搭建过程一样同样是用idea快速搭建,这次就不再重复说明了,不过在构建的时候跟Service不同的是要添加依赖的组件不同。首先是web组件和服务发现注册组件。
然后下一步,下一步项目构建好之后建一下包,结构如下图
controller层代码
package com.example.product_service.controller;
import com.example.product_service.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("api/v1/producct")
public class ProductController {
@Autowired
ProductService productService;
/**
* 获取所有商品列表
* @return
*/
@RequestMapping("/list")
public Object list(){
return productService.listPoduct();
}
/**
* 根据id查找商品详情
* @param id
* @return
*/
@RequestMapping("/find")
public Object findById(@RequestParam("id") int id){
return productService.findById(id);
}
}
domain层代码
package com.example.product_service.domain;
import java.io.Serializable;
public class Product implements Serializable {
public Product(){};
public Product(int id, String name, int price, int store) {
this.id = id;
this.name = name;
this.price = price;
this.store = store;
}
private int id;
/**
* 商品名称
*/
private String name;
/**
* 价格
*/
private int price;
/**
* 库存
*/
private int store;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getStore() {
return store;
}
public void setStore(int store) {
this.store = store;
}
}
service层
package com.example.product_service.service;
import com.example.product_service.domain.Product;
import java.util.List;
public interface ProductService {
List<Product> listPoduct();
Product findById(int id);
}
service实现层,没有连接数据库这里用map存的假数据
package com.example.product_service.service.impl;
import com.example.product_service.domain.Product;
import com.example.product_service.service.ProductService;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class ProductSreivceImpl implements ProductService {
private static final Map<Integer,Product> daoMap = new HashMap<>();
static {
Product p1 = new Product(1,"手机",10,10);
Product p2 = new Product(2,"冰箱",5,12);
Product p3 = new Product(3,"洗衣机",6,78);
Product p4 = new Product(4,"汽车",858,542);
Product p5 = new Product(5,"椅子",585,242);
Product p6 = new Product(6,"java",424,246);
Product p7 = new Product(7,"犯病病",10000,10);
daoMap.put(p1.getId(),p1);
daoMap.put(p2.getId(),p2);
daoMap.put(p3.getId(),p3);
daoMap.put(p4.getId(),p4);
daoMap.put(p5.getId(),p5);
daoMap.put(p6.getId(),p6);
daoMap.put(p7.getId(),p7);
}
@Override
public List<Product> listPoduct() {
Collection<Product> collection = daoMap.values();
List<Product> list = new ArrayList<>(collection);
return list;
}
@Override
public Product findById(int id) {
return daoMap.get(id);
}
}
启动类
package com.example.product_service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
}
}
application.yml 配置文件
server:
port: 8771
#指定注册中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
#服务的名称
spring:
application:
name: product-service
接下启动项目,注意要先启动之前搭建的Eureka Srever在启动Eureka Client不然会报错,因为配置文件里面已经配置了注册中心了。此时在打开注册中心就可以看到刚才搭建的客户端了
上图Application 一栏就是咋们刚刚搭建的客户端名为product-service,现在可以在启动一个客户端看看,idea启动两个springboot方式如下图
在 VM options一栏中输入-Dserver.port=8772(8772是端口号)应用之后就行了
再次启动会发现有个2的显示,就说明启动了两个了
再次打开注册中心就可以看到有两个product-service服务注册了
刚才在Client写的接口现在访问以下看看
看到数据返回了,到此Eureka Client搭建就算完成了