https://blog.youkuaiyun.com/qq_28764557/article/details/89423072
一、Spring Boot和Spring Cloud
Spring Cloud
Spring Cloud是一个分布式的整体解决方案。Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局琐,leader选举,分布式session,集群状态)中快速构建的工具,使用Spring Cloud的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接。
SpringCloud分布式开发五大常用组件
- 服务发现——Netflix Eureka
- 客服端负载均衡——Netflix Ribbon
- 断路器——Netflix Hystrix
- 服务网关——Netflix Zuul
- 分布式配置——Spring Cloud Config
二、Spring Cloud 入门
1、创建provider
2、创建consumer
3、引入Spring Cloud
4、引入Eureka注册中心
5、引入Ribbon进行客户端负载均衡
dubbo解决的是rpc。
springcloud是整体的解决方案。
Eureka:注册中心
Ribbon:负载均衡
Hystrix:断路器,A-B-C,中间出现问题用断路器,响应。
zuul:网管,过滤。
注册中心:
服务的提供者:
消费者:
启动注册中心:
配置文件:
-
server:
-
port:
8761
-
eureka:
-
instance:
-
hostname: eureka-server
# eureka实例的主机名
-
client:
-
register-with-eureka:
false
#不把自己注册到eureka上
-
fetch-registry:
false
#不从eureka上来获取服务的注册信息
-
service-url:
-
defaultZone: http:
//localhost:8761/eureka/
-
-
-
-
-
地址默认值:
启动要加注解:
-
package com.atguigu.eurekaserver;
-
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
-
-
/**
-
* 注册中心
-
* 1、配置Eureka信息
-
* 2、@EnableEurekaServer
-
*/
-
@EnableEurekaServer
//加入这个注解
-
@SpringBootApplication
-
public
class EurekaServerApplication {
-
-
public static void main(String[] args) {
-
SpringApplication.run(EurekaServerApplication.class, args);
-
}
-
}
启动:
访问:
34---------------------------------------------------------------------------------------
提供者的功能。
注册进来:
-
server:
-
port:
8001
-
spring:
-
application:
-
name: provider-ticket
-
-
-
eureka:
-
instance:
-
prefer-ip-address:
true
# 注册服务的时候使用服务的ip地址
-
client:
-
service-url:
-
defaultZone: http:
//localhost:8761/eureka/
启动。
注册中心有了应用。
可以注册多个应用。
8001 8002
35---------------------------------------------------------------------------------------
创建消费者:
-
spring:
-
application:
-
name: consumer-user
-
server:
-
port:
8200
-
-
eureka:
-
instance:
-
prefer-ip-address:
true
# 注册服务的时候使用服务的ip地址
-
client:
-
service-url:
-
defaultZone: http:
//localhost:8761/eureka/
-
发现服务的注解:
-
package com.atguigu.consumeruser;
-
-
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 ConsumerUserApplication {
-
-
public static void main(String[] args) {
-
SpringApplication.run(ConsumerUserApplication.class, args);
-
}
-
-
@LoadBalanced
//使用负载均衡机制
-
@Bean
-
public RestTemplate restTemplate(){
-
return
new RestTemplate();
-
}
-
}
-
package com.atguigu.consumeruser;
-
-
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 ConsumerUserApplication {
-
-
public static void main(String[] args) {
-
SpringApplication.run(ConsumerUserApplication.class, args);
-
}
-
-
@LoadBalanced
//使用负载均衡机制
-
@Bean
-
public RestTemplate restTemplate(){
//帮我们发送http请求的。
-
return
new RestTemplate();
-
}
-
}
消费服务:
-
package com.atguigu.consumeruser.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 {
-
-
@Autowired
-
RestTemplate restTemplate;
-
-
@GetMapping("/buy")
-
public String buyTicket(String name){
-
String s = restTemplate.getForObject(
"http://PROVIDER-TICKET/ticket", String.
class);
-
return name+
"购买了"+s;
-
}
-
}
36--------------------------------------------------------------------------------------