网站首页 > 博客文章 正文
spring cloud项目构建入门
技术选型
- spring cloud Hoxton.SR3
- spring boot 2.2.6.RELEASE
- eureka
- open fegin
- jdk 8
- maven
版本根据 spring cloud官网确定
1、构建maven父工程
idea 新建maven project
project 命名
修改pom属性删除src文件夹
2、构建eureka注册中心
maven搭建eurekaserver
新建工程,选择Spring Initializr,默认地址,或者修改为http://start.spring.io
配置工程信息
选择Cloud Discovery,勾选EurekaServer,注意spring boot 版本选择为2.2.6
配置appcation.properties
server.port=8848
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
主启动类添加@EnableEurekaServer注解
启动eureka
浏览器打开localhost:8848,显示如下网页,则eureka server 搭建成功
3、创建用户服务user-server
步骤如上
maven 构建
选择web 和eureka client
配置application.properties
spring.application.name=USER-SERVER
server.port=8080
eureka.client.serviceUrl.defaultZone=http://localhost:8848/eureka/
主启动添加注解 @EnableEurekaClient
先启动eureka server
再启动user server
进入 发现user-server 已经成功注册
新建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;
@RestController
public 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 结果如下
4、构建服务消费端 cloud-consumer
maven module 构建
配置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;
@RestController
public class UserController {
@Resource
private UserServiceClient userServiceClient;
@RequestMapping(value = "/consumer/getUser")
public String getUser(@RequestParam String userId){
return userServiceClient.getUser(userId);
}
}
启动cloud-consumer
访问
至此spring-cloud 通过open fegin 调用user server 服务成功
猜你喜欢
- 2024-09-18 阿里p7大佬首次分享Spring Cloud学习笔记,带你从0搭建微服务
- 2024-09-18 如何使用 SpringCloud 搭建服务注册中心?
- 2024-09-18 springcloud-microservice 快速构建分布式系统
- 2024-09-18 Eureka搭建分布式SpringCloud项目
- 2024-09-18 Spring Cloud 微服务实战——nacos 服务注册中心搭建(附源码)
- 2024-09-18 Spring cloud + vue + oAuth2.0搭建企业级微服务项目源码分享
- 2024-09-18 Nacos + Spring Cloud Gateway动态路由配置
- 2024-09-18 快速创建 Spring Cloud 应用的 Spring Initializr 使用及原理
- 2024-09-18 Spring Cloud 整合 SkyWalking(spring cloud 整合kafka设置日志级别)
- 2024-09-18 记录Spring Cloud应用在阿里云架构的一次部署
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)