springCloud微服务搭建实例

springcloud微服务搭建实例

项目目录结构

eureka-server是服务注册中心,parententity是公共实体类,providerclient和employeeprovider是两个服务,注册到服务注册中心。 ## 创建项目 创建一个空工程项目:

在这里插入图片描述

Eureka服务注册中心的创建

在空工程项目中创建一个Module
在这里插入图片描述
选择Spring Initializr,下一步
在这里插入图片描述
如下图,继续下一步,完成。
在这里插入图片描述
注册中心代码以及配置

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

需要在启动类中添加@EnableEurekaServer注解。

appication.yml配置文件

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${
   eureka.instance.hostname}:${
   server.port}/eureka

pom.xml文件

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

服务注册中心已经配置完成,访问 http://localhost:8761/,如下图:

在这里插入图片描述

创建公共实体类的子项目

创建的时候也是勾选Spring Initializr项目,其他的话勾选Lombok就行,新建完成之后可以删除启动类和配置文件。
把所有实体类都放到这个项目中统一管理,其他子项目不需要再重复创建实体类,只需要再pom文件中添加该项目的相关依赖就可以,如下:

<dependency>
    <groupId>com.hejin</groupId>
    <artifactId>parententity</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值