spring boot整合spring cloud

本项目还是通过卖票和买票模块来介绍spring cloud。

1.新建服务注册模块:eureka-server

1.1 配置pom



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.19.RELEASE
         
    
    com.laoxu
    eureka-server
    0.0.1-SNAPSHOT
    eureka-server
    Demo project for Spring Boot

    
        1.8
        Edgware.SR5
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


1.2 配置application.yml

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka/

 

2.新建卖票模块:provider-ticket

2.1 配置pom



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.19.RELEASE
         
    
    com.laoxu
    provider-ticket
    0.0.1-SNAPSHOT
    provider-ticket
    Demo project for Spring Boot

    
        1.8
        Edgware.SR5
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.2 配置application.yml

server:
  port: 8002
spring:
  application:
    name: provider-ticket
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

2.3 编写service

package com.laoxu.providerticket.service;

import org.springframework.stereotype.Service;

@Service
public class TicketService {
    public String getTicket(){
        System.out.println("8002"); //自定义输出
        return "《厉害了,我的国》";
    }
}

2.4 编写controller

package com.laoxu.providerticket.controller;

import com.laoxu.providerticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TicketController {
    @Autowired
    TicketService ticketService;

    @GetMapping("/ticket")
    public String getTicket(){
        return ticketService.getTicket();
    }
}

2.5 打包模块

1)修改getTicket方法中输出端口和yml文件端口为8001,并用maven打包,重命名文件为:

provider-ticket-0.0.1-SNAPSHOT-8001.jar

2)重复1)中步骤端口改为8002再打包为

provider-ticket-0.0.1-SNAPSHOT-8002.jar

 

如何打包?

spring boot整合spring cloud_第1张图片

 

3.新建买票模块:consumer-user

3.1 配置pom



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.19.RELEASE
         
    
    com.laoxu
    consumer-user
    0.0.1-SNAPSHOT
    consumer-user
    Demo project for Spring Boot

    
        1.8
        Edgware.SR5
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


3.2 配置application.yml

spring:
  application:
    name: consumer-user
server:
  port: 8200

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

3.3 给启动类添加bean

package com.laoxu.consumeruser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient //开启发现服务功能
@SpringBootApplication
public class ConsumerUserApplication {

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

    @LoadBalanced //开启负载均衡
    @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }

}

3.4 编写controller

package com.laoxu.consumeruser.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
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 ticket = restTemplate.getForObject("http://provider-ticket/ticket", String.class);
        return name+"购买了:"+ticket;
    }
}

 

 

4.依次启动模块

4.1 eureka-server

spring boot整合spring cloud_第2张图片

4.2 命令行启动provider-ticket

java -jar provider-ticket-0.0.1-SNAPSHOT-8001.jar

java -jar provider-ticket-0.0.1-SNAPSHOT-8002.jar

4.3 consumer-user

 

刷新注册中心,可以看到provider-ticket和consumer-user两个Application

 

5.测试买票流程

 

spring boot整合spring cloud_第3张图片

多次刷新,观察provider-ticket的两个应用控制台日志。

可以看到4次访问时平均分配到2个应用上的,因此实现了请求负载均衡。

spring boot整合spring cloud_第4张图片

 

 

 

 

你可能感兴趣的:(#,Spring-Boot,spring,cloud)