1.打开IntelliJ Idea,新创建一个工程,选择Spring Initializr,JDK1.8,如下图所示:
2.点击Next,进入下一步,Group命名为com.hzb(自己命名包名即可),Artifact命名为eureka_server(服务器端最好命名如此),其他默认即可,如下图所示:
3.进入下一界面,选择Spring Cloud Discovery---Eureka Server---Spring Boot 2.1.6(我的IntelliJ Idea是2019.1版本,Spring Boot版本较高,后续因为版本原因需要加一个类的配置),如下图所示:
4.进入下一界面,一切默认即可,点击Finish,第一步环境搭建完成,如下图所示:
5.将application.properties文件改为application.yml,内容如下所示:
server:
port: 8888
spring:
security:
user:
password: 123456
name: hzb
eureka:
instance:
hostname: localhost
client:
#声明自己是个服务端
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
6.打开启动类EurekaServerApplication.java,加入@EnableEurekaServer,内容如下所示:
package net.xdclass.eureka_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
//如果这个启动类所在的包路径隐藏的很深,则需要指定扫描包。否则默认扫描启动类所在的子包路径下
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
7.现在可以将服务器端启动了,由于设置了用户名密码,在浏览器输入localhost:8888时,会进行验证,用户名密码在application.yml文件中,进入后,如下图所示:
至此,服务器端配置启动完成
8.搭建客户端,步骤与之前类似,项目名为product_service,Spring Cloud Discovery---Eureka Discovery Client,其他配置与之前相同,如下图所示,
9.将application.properites改为application.yml,内容如下:
(注意:要与服务器端的yml内容相对应,否则无法产生进入服务器端)
server:
port: 8888
spring:
security:
user:
password: 123456
name: hzb
eureka:
instance:
hostname: localhost
client:
#声明自己是个服务端
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
server:
#自我保护模式(注册中心控制台的红色提醒)被禁止,不建议禁止此模式
#默认开启状态true
enable-self-preservation: false
10.创建如下图所示的MVC结构:
ProductController:
package net.xdclass.product_service.controller;
import net.xdclass.product_service.domain.Product;
import net.xdclass.product_service.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("api/v1/product")
public class ProductController {
@Value("${server.port}")
private String port;
@Autowired
private ProductService productService;
/**
* 获取所有商品列表
*
* @return
*/
@RequestMapping("list")
public Object list() {
return productService.listProduct();
}
/**
* 根据id查找商品详情
*
* @param id
* @return
*/
@RequestMapping("find")
public Object findById(@RequestParam("id") int id) {
Product product = productService.findById(id);
Product result = new Product();
//复制属性
BeanUtils.copyProperties(product, result);
result.setName(result.getName() + " data from port=" + port);
return result;
}
}
Product:
package net.xdclass.product_service.domain;
import java.io.Serializable;
public class