SpringBoot整合SpringCloud

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

0,工程结构图

1,eureka 配置

①,pom.xml

 
  1. <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>1.5.12.RELEASE</version>

  5. </parent>

  6.  
  7. <properties>

  8. <spring-cloud.version>Edgware.SR3</spring-cloud.version>

  9. </properties>

  10.  
  11. <dependencies>

  12. <dependency>

  13. <groupId>org.springframework.cloud</groupId>

  14. <artifactId>spring-cloud-starter-eureka-server</artifactId>

  15. </dependency>

  16. </dependencies>

  17.  
  18. <dependencyManagement>

  19. <dependencies>

  20. <dependency>

  21. <groupId>org.springframework.cloud</groupId>

  22. <artifactId>spring-cloud-dependencies</artifactId>

  23. <version>${spring-cloud.version}</version>

  24. <type>pom</type>

  25. <scope>import</scope>

  26. </dependency>

  27. </dependencies>

  28. </dependencyManagement>

②,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. <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>1.5.12.RELEASE</version>

  5. </parent>

  6.  
  7. <properties>

  8. <spring-cloud.version>Edgware.SR3</spring-cloud.version>

  9. </properties>

  10.  
  11. <dependencies>

  12. <dependency>

  13. <groupId>org.springframework.cloud</groupId>

  14. <artifactId>spring-cloud-starter-eureka</artifactId>

  15. </dependency>

  16. <dependency>

  17. <groupId>org.springframework.boot</groupId>

  18. <artifactId>spring-boot-starter-web</artifactId>

  19. </dependency>

  20. </dependencies>

  21.  
  22. <dependencyManagement>

  23. <dependencies>

  24. <dependency>

  25. <groupId>org.springframework.cloud</groupId>

  26. <artifactId>spring-cloud-dependencies</artifactId>

  27. <version>${spring-cloud.version}</version>

  28. <type>pom</type>

  29. <scope>import</scope>

  30. </dependency>

  31. </dependencies>

  32. </dependencyManagement>

②,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. <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>1.5.12.RELEASE</version>

  5. </parent>

  6.  
  7. <properties>

  8. <spring-cloud.version>Edgware.SR3</spring-cloud.version>

  9. </properties>

  10.  
  11. <dependencies>

  12. <dependency>

  13. <groupId>org.springframework.cloud</groupId>

  14. <artifactId>spring-cloud-starter-eureka</artifactId>

  15. </dependency>

  16. </dependencies>

  17.  
  18. <dependencyManagement>

  19. <dependencies>

  20. <dependency>

  21. <groupId>org.springframework.cloud</groupId>

  22. <artifactId>spring-cloud-dependencies</artifactId>

  23. <version>${spring-cloud.version}</version>

  24. <type>pom</type>

  25. <scope>import</scope>

  26. </dependency>

  27. </dependencies>

  28. </dependencyManagement>

②,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工程

②,启动提供者工程

③,启动消费者工程

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值