idea创建springcloud项目_idea构建 spring cloud项目入门

spring cloud项目构建入门

技术选型

  • spring cloud Hoxton.SR3
  • spring boot 2.2.6.RELEASE
  • eureka
  • open fegin
  • jdk 8
  • maven

版本根据 spring cloud官网确定

4b42cf63fed440c49680155b974e1db6

1、构建maven父工程

idea 新建maven project

0806da51633d486a96686666ca6a6078

project 命名

16b92c1c486d4ff8934cb76444ea5d85

修改pom属性删除src文件夹

92b2fbf810d6470b92c394ec630626af

2、构建eureka注册中心

maven搭建eurekaserver

新建工程,选择Spring Initializr,默认地址,或者修改为http://start.spring.io

配置工程信息

选择Cloud Discovery,勾选EurekaServer,注意spring boot 版本选择为2.2.6

6e3622bf910147e4b8c2cbe7614b677f

配置appcation.properties

server.port=8848eureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

主启动类添加@EnableEurekaServer注解

e1f51bbab5e34d4c9b42df973743f9e2

启动eureka

浏览器打开localhost:8848,显示如下网页,则eureka server 搭建成功

2f34bfec8d04488e82134cb361957756

3、创建用户服务user-server

步骤如上

maven 构建

e43f41cd6f534def8e228b4d9989185d

选择web 和eureka client

b1d0769a3f4048059118b6f262cf2009
1277d2cdcef54163b103a3325cc39984

配置application.properties

spring.application.name=USER-SERVERserver.port=8080eureka.client.serviceUrl.defaultZone=http://localhost:8848/eureka/

主启动添加注解 @EnableEurekaClient

先启动eureka server

再启动user server

进入 发现user-server 已经成功注册

d7d3915d3bdb42ea9da406e5d9d0fa2f

新建UserController

package com.cloud.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController {    @RequestMapping(value = "getUser")    public String  service(@RequestParam String userId){        String  str="hello  ,im user server  ,userId:"+userId;        return str;    }}

浏览器访问 http://localhost:8080/getUser?userId=1 结果如下

bb85d3b1ce00406eabd3e74fd519aa52

4、构建服务消费端 cloud-consumer

maven module 构建

57d1fc02d87f4e909e025ac7b9709f75
353c15932dc346f986a673c5d59cbecd

配置application.properties

#是指,这个服务的名称spring.application.name=CONSUME-SERVICE  #该服务独有的端口号server.port=80#下面这个是指向Eureka注册中心,这样就注册成功了..eureka.client.serviceUrl.defaultZone=http://localhost:8848/eureka/ 

主启动添加注解 @EnableEurekaClient @EnableFeignClients

新建 UserServiceClient UserController

package com.cloud.service;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@FeignClient(value = "USER-SERVER")public interface UserServiceClient {    @RequestMapping(value = "getUser")    public String  getUser(@RequestParam String userId);}
package com.cloud.controller;import com.cloud.service.UserServiceClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestControllerpublic class UserController {    @Resource    private UserServiceClient userServiceClient;    @RequestMapping(value = "/consumer/getUser")    public String  getUser(@RequestParam String userId){        return userServiceClient.getUser(userId);    }}

启动cloud-consumer

访问

771c1b622fcc4f61a71e80c31098defd

至此spring-cloud 通过open fegin 调用user server 服务成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值