使用IntelliJ Idea搭建Eureka服务中心Server端并用客户端调用

本文详细介绍了如何使用IntelliJ Idea搭建Eureka服务中心Server端,并配置客户端连接。从新建Spring Initializr工程开始,一步步配置Eureka Server,包括设置Group和Artifact,选择Spring Cloud Discovery的Eureka Server。接着,介绍了如何修改application.yml文件,启动服务器并解决登录验证问题。然后,搭建商品服务客户端,配置Eureka Discovery Client,并创建相关MVC结构。最后,讨论了常见的错误处理,如客户端注册失败、多端口启动和负载均衡的实现。
摘要由CSDN通过智能技术生成

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
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值