SpringBoot整合SpringCloud

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

0,工程结构图

SpringBoot整合SpringCloud_第1张图片

1,eureka 配置

①,pom.xml

 
  1. org.springframework.boot

  2. spring-boot-starter-parent

  3. 1.5.12.RELEASE

  4.  
  5. Edgware.SR3

  6.  
  7. org.springframework.cloud

  8. spring-cloud-starter-eureka-server

  9.  
  10. org.springframework.cloud

  11. spring-cloud-dependencies

  12. ${spring-cloud.version}

  13. pom

  14. import

②,application.properties

 
  1. #eueka 主机名

  2. eureka.instance.hostname=eureka-service

  3. #不注册自己

  4. eureka.client.register-with-eureka=false

  5. #获取服务

  6. eureka.client.fetch-registry=false

  7. #提供者和消费者的注册地址

  8. eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

  9.  
  10. server.port=8761

③,启用注册中心

 
  1. import org.springframework.boot.SpringApplication;

  2. import org.springframework.boot.autoconfigure.SpringBootApplication;

  3. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

  4.  
  5. //启用注册中心

  6. @EnableEurekaServer

  7. @SpringBootApplication

  8. public class EurekaApplication {

  9.  
  10. public static void main(String[] args) {

  11. SpringApplication.run(EurekaApplication.class, args);

  12. }

  13. }

2,provider配置

①,pom.xml

 
  1. org.springframework.boot

  2. spring-boot-starter-parent

  3. 1.5.12.RELEASE

  4.  
  5. Edgware.SR3

  6.  
  7. org.springframework.cloud

  8. spring-cloud-starter-eureka

  9. org.springframework.boot

  10. spring-boot-starter-web

  11.  
  12. org.springframework.cloud

  13. spring-cloud-dependencies

  14. ${spring-cloud.version}

  15. pom

  16. import

②,application.properties

 
  1. server.port=8002

  2. #服务名

  3. spring.application.name=ticket-provider

  4. #使用ip进行注册

  5. eureka.instance.prefer-ip-address=true

  6. #注册地址

  7. eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

③,定义服务

 
  1. import org.springframework.stereotype.Service;

  2.  
  3. @Service

  4. public class TicketService {

  5.  
  6. public String buyTicket(){

  7. System.out.println("我是8002");

  8. return "《疯狂的石头》";

  9. }

  10. }

④,提供服务

 
  1. import com.example.provider.service.TicketService;

  2. import org.springframework.beans.factory.annotation.Autowired;

  3. import org.springframework.web.bind.annotation.RequestMapping;

  4. import org.springframework.web.bind.annotation.RestController;

  5.  
  6. @RestController

  7. public class TicketController {

  8.  
  9. @Autowired

  10. private TicketService ticketService;

  11.  
  12. @RequestMapping("/")

  13. public String index(){

  14. return ticketService.buyTicket();

  15. }

  16. }

3,customer配置

①,pom.xml

 
  1. org.springframework.boot

  2. spring-boot-starter-parent

  3. 1.5.12.RELEASE

  4.  
  5. Edgware.SR3

  6.  
  7. org.springframework.cloud

  8. spring-cloud-starter-eureka

  9.  
  10. org.springframework.cloud

  11. spring-cloud-dependencies

  12. ${spring-cloud.version}

  13. pom

  14. import

②,application.properties

 
  1. server.port=8200

  2.  
  3. spring.application.name=ticket-customer

  4. eureka.instance.prefer-ip-address=true

  5. #注册地址

  6. eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

③,消费者配置

 
  1. import org.springframework.boot.SpringApplication;

  2. import org.springframework.boot.autoconfigure.SpringBootApplication;

  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

  4. import org.springframework.cloud.client.loadbalancer.LoadBalanced;

  5. import org.springframework.context.annotation.Bean;

  6. import org.springframework.web.client.RestTemplate;

  7.  
  8. //开启发现服务

  9. @EnableDiscoveryClient

  10. @SpringBootApplication

  11. public class CustomerApplication {

  12.  
  13. public static void main(String[] args) {

  14. SpringApplication.run(CustomerApplication.class, args);

  15. }

  16.  
  17. // 启用负载均衡,默认算法是轮询

  18. @LoadBalanced

  19. @Bean

  20. public RestTemplate restTemplate(){

  21. return new RestTemplate();

  22. }

  23. }

④,消费者消费方法

 
  1. import org.springframework.beans.factory.annotation.Autowired;

  2. import org.springframework.web.bind.annotation.RequestMapping;

  3. import org.springframework.web.bind.annotation.RestController;

  4. import org.springframework.web.client.RestTemplate;

  5.  
  6. @RestController

  7. public class CustomerController {

  8.  
  9. @Autowired

  10. private RestTemplate restTemplate;

  11.  
  12. @RequestMapping("/")

  13. public String index(){

  14. String result = restTemplate.getForObject("http://ticket-provider/", String.class);

  15.  
  16. return result;

  17. }

  18. }

4,测试

①,启动eureka工程

SpringBoot整合SpringCloud_第2张图片

②,启动提供者工程

③,启动消费者工程

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

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

你可能感兴趣的:(Spring,Boot,spring,boot)