springboot 与分布式

在分布式系统中,国内常用的zookeeper +dobbo 组合,而springboot推荐使用全栈的Spring,

Springboot,springcloud 

分布式系统

ZooKeeper 

zookeeper 是一个分布式的,开源的分布式应用程序调用服务,他是一个为分布式应用提供一致服务的软件,提供的功能包括有配置维护,域名服务,分布式同步,组服务

Dubbo

dubbo是alibaba 开源的分布式框架,他是最大的特点是按照分层的方式来架构,使用这种方式可以使各层之间的解耦&,或者是最大限度的松耦合,从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是从提供方提供服务,要么是哦那个消费放消费服务,所以基于这一点可以抽象出服务提供方provider 和服务消费方 Consumer 角色

docker 安装zookeeper 

docker pull zookeeper 

docker -d -p 2181:2181 --name zookeeper  -d imageid 

springboot 整合dubbo RPC 远程调用

发布服务

1.引入依赖

<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.1.0</version>
		</dependency>

		<!--引入zookeeper的客户端工具-->
		<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>

2.配置properties

dubbo.application.name=provider-ticket

dubbo.registry.address=zookeeper://192.168.24.136:2181

dubbo.scan.base-packages=com.atguigu.ticket.service

这里的dubbo.scan.base-packages 可以使用注解替换

@DubboComponentScan({"com.atguigu.ticket.service"})
@EnableDubbo
@SpringBootApplication
public class ProviderTicketApplication {

	public static void main(String[] args) {
		SpringApplication.run(ProviderTicketApplication.class, args);
	}
}

3.编写接口



@Component
@Service //将服务发布出去
public class TicketServiceImpl implements TicketService {
    @Override
    public String getTicket() {
        return "《厉害了,我的国》";
    }
}

用@Service 接口标注 暴漏这个接口给外部调用

注意: springboot 1.5 不需要标注@EnableDubbo  springboot2.0x 需要

调用dubbo服务

1.配置pom

2.配置properties 

3.引入接口的依赖

package com.atguigu.user.service;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.ticket.service.TicketService;
import org.springframework.stereotype.Service;

@Service
public class UserTicket {

    @Reference
    TicketService ticketService ;

    public void hello(){
        System.out.println(ticketService.getTicket());
    }

}

使用@reference 标识rpc 调用

Springboot 整合springCloud 

dubbo 是一个rpc 远程服务调用的框架, 但是springcloud 是一个着眼于分布式的整体解决方案,springcloud 使用基于http 的resultful 的架构风格调用 ,

springcloud 为开发者提供了在分布式系统,配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局锁,leader选举,分布式session ,集群状态 中构建的工具,使用springcloud的开发者可以快速的启动或者构建应用,同时能够快速和云平台资源对接,

springcloud 分布式开发常用的五大组件

服务的发现  Netflix Eureka 

服务端负载均衡  Netfix Ribbon

断路器   Netflix Hystrix 

服务网关  Netflix Zuul 

服务配置  Spring Cloud Config

配置Eureka 注册中心

1. 引入相关的依赖

服务端引用

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

2.配置properties

server:
  port: 8761

eureka:
  instance:
    hostname: eureka-server-8761 #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #单机 #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
#      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/



3. 标注@EnableEurekaServer


@EnableEurekaServer
@SpringBootApplication
public class CustomerApplication {

	public static void main(String[] args) {
		SpringApplication.run(CustomerApplication.class, args);
	}
}

4.run 

然后创建服务的发布者

1.引入依赖

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

2.配置properties

server:
  port: 8080
eureka:
  client: #客户端注册进eureka服务列表内
    service-url:
      defaultZone: http://localhost:8761/eureka
#       defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: provide-8080
    prefer-ip-address: true     #访问路径可以显示IP地址

 #微服务信息
info:
  app.name: provide-8080
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
spring:
  application:
    name: provide-8080

3.标注springboot 启动@EnableEurekaClient

@EnableEurekaClient
@SpringBootApplication
public class ProvideApplication {

	public static void main(String[] args) {
		SpringApplication.run(ProvideApplication.class, args);
	}
}

创建调用者

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

2.配置properties

server:
  port: 8686
eureka:
  client: #客户端注册进eureka服务列表内
    service-url:
      defaultZone: http://localhost:8761/eureka
#       defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: customer-8080
    prefer-ip-address: true     #访问路径可以显示IP地址

 #微服务信息
info:
  app.name: customer-8888
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
spring:
  application:
    name: customer-8888

3.开启eureka 注解

package com.zzq.customer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class CustomerApplication {

	public static void main(String[] args) {
		SpringApplication.run(CustomerApplication.class, args);
	}
}

4,调用服务
 

package com.zzq.customer.controller;

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;

@RestController
public class HelloController {

    private final String REST_URL_PREFIX="http://localhost:8080";

    @Autowired
    private RestTemplate restTemplate ;

    @RequestMapping("/hello")
    public String hello(){
        return  restTemplate.getForObject(REST_URL_PREFIX+"/hello",String.class);
    }

}

OK

 

 

 

 

 

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值