33、springboot整合springcloud
Spring Cloud

此时需要引入:

配置文件
server.port=
#主机名
eureka.instance.hostname=server
#不做高可用不进行设置
#不把本身注册在注册中心
eureka.client.register-with-eureka=false
#不从eureka上获取服务的注册信息
eureka.client.fetch-registry=false
#服务中心地址
eureka.client.service-url.DEFAULT_ZONE=http://localhost:8761/eureka/
开启服务:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
} }
访问网页:

此时的服务是开启的!!!
服务提供者:


TicketService.java
package com.cr.provider.service;
import org.springframework.stereotype.Service;
@Service
public class TicketService {
public String buyTicket(){
return "战狼2";
}
}
TicketController.java
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TicketController {
@Autowired
TicketService ticketService; //通过http协议进行发送的
@GetMapping("/buy")
public String getTicket(){
return ticketService.buyTicket();
}
}
配置文件:
server.port=8081
#应用起名字spring.application.name=provider
#注册服务时使用服务的ip地址
eureka.instance.prefer-ip-address=true
#服务中心地址
eureka.client.service-url.DEFAULT_ZONE=http://localhost:8761/eureka/
@SpringBootApplication
public class ProviderApplication { public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
} }

此时查看注册证中心:

此时打包两个jar文件分别未8081、8082端口,分别进行多个服务的注册

此时:同一个应用的两个实例

服务消费者:


UserController.java
package com.cr.consumer.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class UserController { //用于获取http请求的信息
@Autowired
RestTemplate restTemplate; @GetMapping("/buyTicket")
public String buyTicket(String name){ String ticket = restTemplate.getForObject("http://PROVIDER/buy", String.class);
return name + "购买了" + ticket ;
} }
配置文件:
server.port=8088spring.application.name=consumer
#注册服务时使用服务的ip地址
eureka.instance.prefer-ip-address=true
#服务中心地址
eureka.client.service-url.DEFAULT_ZONE=http://localhost:8761/eureka/
主类:
package com.cr.consumer; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; //开启发现服务功能
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication { public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
} //http请求
@LoadBalanced//使用负载均衡机制
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
启动服务:



33、springboot整合springcloud的更多相关文章
- SpringBoot整合SpringCloud搭建分布式应用
什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...
- SpringCloud:SpringBoot整合SpringCloud项目
划分模块 这里我划分了四个模块 Common: 存放bean和Dao模块 Consumer: 消费者模块,提供对外暴露接口服务 EurekaServer: Eureka注册中心模块,主要用于启动注册中 ...
- Java-SpringBoot整合SpringCloud
SpringBoot整合SpringCloud 1. SpringCloud特点 SpringCloud专注于为典型的用例和扩展机制提供良好的开箱即用体验,以涵盖其他情况: 分布式/版本化配置 服务注 ...
- Rabbitmq基本使用 SpringBoot整合Rabbit SpringCloud Stream+Rabbit
https://blog.csdn.net/m0_37867405/article/details/80793601 四.docker中使用rabbitmq 1. 搭建和启动 使用地址:rabbitm ...
- SpringBoot分布式篇Ⅷ --- 整合SpringCloud
SpringCloud是一个分布式的整体解决方案.Spring Cloud为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局锁,leader选举.分布 ...
- SpringBoot系列十二:SpringBoot整合 Shiro
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Shiro 2.具体内容 Shiro 是现在最为流行的权限认证开发框架,与它起名的只有最初 ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
- SpringBoot整合RabbitMQ-整合演示
本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...
- SpringBoot整合Netty并使用Protobuf进行数据传输(附工程)
前言 本篇文章主要介绍的是SpringBoot整合Netty以及使用Protobuf进行数据传输的相关内容.Protobuf会简单的介绍下用法,至于Netty在之前的文章中已经简单的介绍过了,这里就不 ...
随机推荐
- Apache shiro的简单介绍与使用(与spring整合使用,并加入ehcache缓存权限数据)
apache shiro框架简介 Apache Shiro是一个强大而灵活的开源安全框架,它能够干净利落地处理身份认证,授权,企业会话管理和加密.现在,使用Apache Shiro的人越来越多,因为它 ...
- java工厂模式个人体会
上一边文章主要对单例模式做了一个总结,这篇文章主要对工厂模式也写一写个人的体会. 工厂模式是设计模式的一种,它主要是把实现产品对象的过程封装起来,然后提供给客户端相应的接口.工厂模式也是有3种,分别为 ...
- 判断是手机端还是电脑端 isMobile()
1.在PublicController控制器中写好判断手机端方法. <?php namespace Home\Controller; use Think\Controller; class Pu ...
- JavaScript初学
今天学习了js的基础知识,自我归纳如下: 第一部分:js变量的声明和引入 js声明1-直接声明js代码块,使用<script></script> 2-引入外部声明,即创建一个 ...
- MongoDB Limit/限制记录
Limit() 方法 要限制 MongoDB 中的记录,需要使用 limit() 方法. limit() 方法接受一个数字型的参数,这是要显示的文档数. 语法: limit() 方法的基本语法如下 & ...
- Monitorix:一款面向Linux的轻型系统和网络监测工具
Monitorix是一款功能非常强大的免费开源轻型工具,目的在于监测Linux中的系统和网络资源.它可以定期收集系统和网络数据,并使用自己的Web界面,通过图形显示相关信息.Monitorix让用户可 ...
- HiJson工具 && 火狐浏览器中的jsonHandle插件(以及乱码问题的解决)-->来转换json串的格式
原文:http://blog.csdn.net/cjm2484836553/article/details/72453907 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] ...
- Maximum Product Subarray 最大连续乘积子集
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- android studio新建项目时出现Error:Execution failed for task ':app:preDebugAndroidTestBuild'.
android studio更新后创建新项目时出现以下错误 可以用Build->Rebuild Project解决,但这个方法只是临时的,重新打开项目还是会报错 所以用另一种方法: 在app下的 ...
- 新建虚拟机,每次都提示无法连接虚拟设备 ide1:0
处理方式:看到了这个老哥http://www.cnblogs.com/dean-du/p/6888513.html的博客,发现问题是一样的,所以记录一下. 将虚拟机设置中的CD/DVD选项中的连接更改 ...