SpringBoot整合SpringCloud

SpringCloud 可以说是一门非常热门的技术,依赖于SpringBoot进行实现。cloud就像一个大管家,而SpringBoot 才是真正干活的人。且SpringBoot可以独自运行,不依赖于SpringCloud。本篇主要介绍SpringCloud中五大神兽里的两大神兽,eureka和rabbin,其中eureka 是重点,而rabbin只是简单使用了它的一个注解。

0,工程结构图

 

1,eureka 配置

①,pom.xml

 <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.12.RELEASEversion>
    parent>

    <properties>
        <spring-cloud.version>Edgware.SR3spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eureka-serverartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

②,application.properties

#eueka 主机名
eureka.instance.hostname=eureka-service
#不注册自己
eureka.client.register-with-eureka=false
#获取服务
eureka.client.fetch-registry=false
#提供者和消费者的注册地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

server.port=8761

③,启用注册中心

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

//启用注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

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

2,provider配置

①,pom.xml

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.12.RELEASEversion>
    parent>

    <properties>
        <spring-cloud.version>Edgware.SR3spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

②,application.properties

server.port=8002
#服务名
spring.application.name=ticket-provider
#使用ip进行注册
eureka.instance.prefer-ip-address=true
#注册地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

③,定义服务

import org.springframework.stereotype.Service;

@Service
public class TicketService {

    public String buyTicket(){
        System.out.println("我是8002");
        return "《疯狂的石头》";
    }
}

④,提供服务

import com.example.provider.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TicketController {

    @Autowired
    private TicketService ticketService;

    @RequestMapping("/")
    public String index(){
        return ticketService.buyTicket();
    }
}

3,customer配置

①,pom.xml

 <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.12.RELEASEversion>
    parent>

    <properties>
        <spring-cloud.version>Edgware.SR3spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

②,application.properties

server.port=8200

spring.application.name=ticket-customer
eureka.instance.prefer-ip-address=true
#注册地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

③,消费者配置

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 CustomerApplication {

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

//    启用负载均衡,默认算法是轮询
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

④,消费者消费方法

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 CustomerController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/")
    public String index(){
        String result = restTemplate.getForObject("http://ticket-provider/", String.class);

        return result;
    }
}

4,测试

①,启动eureka工程

②,启动提供者工程

③,启动消费者工程

④,最后会看到消费者和提供者都注册到了eureka中,并可以通过,消费接口访问服务者提供的服务,

并且使用了轮询的负载均衡策略

你可能感兴趣的:(SpringBoot整合SpringCloud)