使用idea从零编写SpringCloud项目-Ribbo
git:https://github.com/bmdcheng/product_server
git:https://github.com/bmdcheng/order_server
1.需要创建两个项目注册到eureka,然后来进行调用展示效果
2.首先创建product_server
3.选择相应的依赖
4.编写一个controller,用于提供服务
package com.example.product_server_test.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; @RestController
@RequestMapping("/api/product")
public class ProductController { @RequestMapping("findAll")
public Object findAll(){
return getData();
} /**
* 模拟数据
* @return
*/
public List<Map<String,Object>> getData(){
List<Map<String,Object>> list = new ArrayList<>(); for (int i =0;10<i;i++){
Map<String,Object> map = new HashMap<String,Object>();
map.put("id", String.valueOf(i));
map.put("Name", "商品"+i);
map.put("price", i*100);
map.put("weight", i*200);
list.add(map);
}
return list;
} }
4.修改product_server的的配置文件application.yml
server:
port: 8771
#指定注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
#服务的名称
spring:
application:
name: product-service-test
5.启动应用,然后到eureka中查看注册的produc_server,http://localhost:8761/
6.再创建一个order_server,用ribbon调用product_server
7.勾选对应的依赖,小知识点,当spring boot版本过高时,就用不了ribbon了,所以调低一点springboot版本就可以选择ribbon依赖了
8.修改order_server中的启动类增加如下代码
@Bean
//@Bean 将该对象交由spring ioc管理
@LoadBalanced
//@增加负载均衡策略
public RestTemplate restTemplate(){
return new RestTemplate();
}
9.修改application.yml配置文件
server:
port: 8781
#指定注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
#服务的名称
spring:
application:
name: order-service
10.写一个controller
package com.example.order_server_test; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import java.util.List; @RestController
@RequestMapping("/api/order")
public class controller { @Autowired
private RestTemplate restTemplate; @RequestMapping("/findAllProduct")
public Object findAllProduct(){
List list = restTemplate.getForObject("http://product-service/api/product/findAll", List.class); return list;
}
}
11.然后在页面使用连接访问
http://localhost:8781/api/order/findAllProduct
12.看到结果了,完事。
13.总结一下流程
创建服务提供方,引入web,eureka-client,修改配置文件,设置服务的端口,服务的名称,注册中心的地址。
创建服务消费方,引入web,eureka-client,ribbon依赖,修改配置文件,设置服务的端口,服务的名称,注册中心的地址,使用@LoadBalanced注解,然后使用restTemplate调连接,拿返回值
使用idea从零编写SpringCloud项目-Ribbo的更多相关文章
- 从零构建Java项目(Maven+SpringBoot+Git) #02 奥斯丁项目
前两天我说要写个项目来持续迭代,有好多小伙伴都表示支持和鼓励,项目的第一篇这不就来了么~我给项目取了个名字,英文名叫做:austin,中文名叫做:奥斯丁 名字倒没有什么特别的含义,我单纯觉得这个名字好 ...
- 从零搭建一个SpringCloud项目之Feign搭建
从零搭建一个SpringCloud项目之Feign搭建 工程简述 目的:实现trade服务通过feign调用user服务的功能.因为trade服务会用到user里的一些类和接口,所以抽出了其他服务需要 ...
- CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL
CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL +BIT祝威+悄悄在此留下版了个权的信息说: 开始 本文用step by step的方式,讲述如何使 ...
- docker初体验:Docker部署SpringCloud项目eureka-server
Docker部署SpringCloud项目eureka-server 1 创建eureka-server工程 创建父工程cloud-demo,其pom.xml如下: <?xml version= ...
- 在Android上编写模块化项目(翻译)
来源博客:Wang Jie's Blog 本文链接:http://blog.wangjiegulu.com/2018/02/13/writing_a_modular_project_on_androi ...
- idea创建springcloud项目图文教程(EurekaServer注册中心)
http://blog.csdn.net/hcmony/article/details/77854999 idea创建springcloud项目图文教程(EurekaServer注册中心)(六) 1, ...
- SpringCloud项目,接口调用返回http 500 - Internal Server Error的错误
今天上班的时候,自己正在参与的Spring Cloud项目出现了问题,原本上周五还正常的项目突然所有接口调用都是返回http 500的错误. 项目的状态是在Eureka上可以看到对应微服务是在线状态, ...
- 一起来学Spring Cloud | 第一章 :如何搭建一个多模块的springcloud项目
在spring cloud系列章节中,本来已经写了几个章节了,但是自己看起来有些东西写得比较杂,所以重构了一下springcloud的章节内容,新写了本章节,先教大家在工作中如何搭建一个多模块的spr ...
- Nacos(四):SpringCloud项目中接入Nacos作为配置中心
前言 通过前两篇文章: Nacos(二):Nacos与OpenFeign的对接使用 Nacos(三):SpringCloud项目中接入Nacos作为注册中心 相信大家已经对Nacos作为注册中心的基本 ...
- springcloud项目配置拓展从本地config目录加载
本文受阿里开源的Nacos启发,应用启动后从Nacos服务加载配置到应用中,想着本地开发的时候加载配置能否从本地存储中加载,这样也能加快开发效率 首先我们来看下SpringCloud项目应用Nacos ...
随机推荐
- Mybatis动态SQL语句大全
读完这篇文章里你能收获到 Mybatis动态SQL语句大全 Mybatis中如何定义变量 Mybatis中如何提取公共的SQL片段 1. If 语句 需求:根据作者名字和博客名字来查询博客!如果作者名 ...
- 【vscode】linux下vscode的使用
注1:vscode在查看project时,非常好用,可以导入整个project并查看其中文件,通过插件的安装还可以实现跳转到当前函数定义处的功能; 注2:可以了解下source insight; 补充 ...
- k8s centos 79,用kuboard-spray装成功。低版本的。安装docker-ce,安装epel源
安装日志 #安装epel源 yum install epel-release -y --nogpgcheck # 安装docker-ce yum install -y yum-utils device ...
- SpringBoot配置双数据源
SpringBoot配置双数据源 一.搭建springboot项目 二.添加依赖 <dependencies> <!--web服务--> <dependency> ...
- 清理缓存tc
/$SYNC 今天修改自建表的字段,换了参考字段但是SE16N显示一直没有改变,删字段,删表都尝试依旧无果,实际上只是没有清理缓存,扑街,留存
- ssh_remote_without_password
2 machines, 1 called client , 1 called server , server need static ip address. client connects serve ...
- Onur Mutlu 18-447 Lecture9 分支预测-1
=============== 第一部分:branch prediction =========== 1. 最简单的分支预测:总是预测下一条指令的地址在 PC+4 如何让这种分支预测更加有效呢? Id ...
- CSP-J入门组
setw(2) cout<<setw(2) //设置后面显示字符的宽度为2 cout<<fixed<<setprecision(6)<<变量名;//设置 ...
- 打开网页自动下载APP
整体主要使用script控制,目前Android通过,iOS未测试. a标签源码部分 <a id="Download"href="javascript:;" ...
- Feign熔断
在Feign中使用 @EnableFeignClients中已经默认打开了断路器功能,所以这里的启动类上不需要再加@EnableCircuitBreaker注解 只需要在@FeignClient中为f ...