Spring Boot与分布式

Updated on with 0 views and 0 comments

Spring Boot与dubbo

ZooKeeper
ZooKeeper 是一个分布式的,开放源码的分布式应用程序协调服务。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
Dubbo
Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。

image.png

  1. docker下载zookeeper
  2. 启动zookeeper

docker run --name some-zookeeper -p 2181:2181 --restart always -d zookeeper

  1. 导入依赖
<!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.8.0</version>
        </dependency>
  1. 配置文件、提供者
dubbo.application.name=provider
dubbo.registry.address=zookeeper://10.211.55.6:2181
dubbo.scan.base-packages=top.wu.providerticket.service
@EnableDubbo
@SpringBootApplication
public class ProviderTicketApplication {
@Component
@DubboService   //发布服务
public class TicketServiceImpl implements TicketService {
    @Override
    public String getTicket() {
        return "《2077》";
    }
}
  1. 配置文件、消费者
dubbo.application.name=consumer
dubbo.registry.address=zookeeper://10.211.55.6:2181
@Service
public class UserServiceImpl implements UserService {

    @DubboReference
    TicketService ticketService;

    @Override
    public void hello() {
        String ticket = ticketService.getTicket();
        System.out.println(ticket);
    }
}

Spring Boot与Spring Cloud

pring Cloud是一个分布式的整体解决方案。Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局琐,leader选举,分布式session,集群状态)中快速构建的工具,使用Spring Cloud的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接。

SpringCloud分布式开发五大常用组件

  • 服务发现——Netflix Eureka
  • 客服端负载均衡——Netflix Ribbon
  • 断路器——Netflix Hystrix
  • 服务网关——Netflix Zuul
  • 分布式配置——Spring Cloud Config

注册中心

image.png

注意SpringBoot和SpringCloud之间的版本对应

<properties>
        <spring-cloud.version>2020.0.0-M6</spring-cloud.version>
    </properties>
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server  #eureka实例的主机名

  client:
    register-with-eureka: false  #不把自己本身也注册在注册中心中,不使用高可用的话就设置为false
    fetch-registry: false        #不从eureka上来获取服务的注册信息
    service-url:
      defaultZone: http://localhost:8761/eureka

启动注解方式的eureka

@EnableEurekaServer
@SpringBootApplication
public class EurakeServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurakeServerApplication.class, args);
    }

}

http://localhost:8761/

image.png

提供者

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
server:
  port: 8001

spring:
  application:
    name: provider-ticket
eureka:
  instance:
    prefer-ip-address: true    #注册服务的时候使用服务的ip地址
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
@RestController
public class TicketController {

    @Autowired
    TicketService ticketService;

    @GetMapping("/ticket")
    public String getTicket() {
        String ticket = ticketService.getTicket();
        return ticket;
    }
}
@Service   //正常的web注解
public class TicketService {

    public String getTicket() {
        return "《南方车站》";
    }

}

服务消费和发现

依赖跟提供者一致

配置

server:
  port: 8002

spring:
  application:
    name: consumer-user
eureka:
  instance:
    prefer-ip-address: true    #注册服务的时候使用服务的ip地址
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

开启发现服务功能

@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();
    }
}
@RestController
public class UserController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/buy")
    public String buyTicket(String name) {
        String forObject = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);  //进行远程调用,第二个参数表示返回类型
        return name+"购买了"+forObject;
    }
}