天天看点

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

0. 开发环境

  • IDE:IntelliJ IDEA 2017.1 x64
  • jdk:1.8.0_91
  • Spring Boot:2.0.9.RELEASE
  • Spring Cloud:Finchley.RELEASE

1. 新建父Maven工程

1.1 新建Maven工程

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)
ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)
ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

1.2 删除src文件夹

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

1.3 引入依赖

父Maven工程pom文件定义为父pom文件,并引入依赖

<?xml version="1.0" encoding="UTF-8"?>
           

2. 新建Spring Boot服务提供者

2.1 新建Spring Boot服务

右键SpringCloudDemo-->New-->Module,选择Maven项目

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)
ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

2.2 引入依赖

<?xml version="1.0" encoding="UTF-8"?>
           

2.3 新建Spring Boot入口类

package 
           

2.4 新建application.yml

resources 文件夹下新建application.yml

server:
  port: 8081
  servlet:
    context-path: /springbootprovider

spring:
  application:
    name: spring-boot-provider
           

2.5 新建控制器类

package 
           

2.6 目录结构

Spring Boot 服务目录结构如下

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

3. 新建Spring Boot服务消费者

3.1 新建Spring Boot消费者

与创建Spring Boot服务提供者类似,新建Spring Boot服务消费者

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

3.2 引入依赖

与Spring Boot服务提供者依赖类似

<?xml version="1.0" encoding="UTF-8"?>
           

3.3 新建Spring Boot入口类

这里要注意,与Spring Boot服务提供者入口类不同,服务消费者入口类新增了一段代码。

RestTemplate是一个对HTTP请求进行了封装的类,借助RestTemplate,Spring应用能够方便地使用REST资源。

package 
           

3.4 新建application.yml

server:
  port: 8082
  servlet:
    context-path: /springbootconsumer

spring:
  application:
    name: spring-boot-consumer
           

3.5 新建控制器类

这里的gateway()方法没有自己的实现,而是去调用了服务提供者的gateway()方法。

package 
           

4. 测试

我们先启动服务提供者,再启动服务消费者,然后浏览器访问 http://127.0.0.1:8082/springbootconsumer/gateway ,获取到正确结果。到这,一个简单Spring Cloud 项目的服务提供者和服务消费者就完成了。

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

GitHub:

dkbnull/SpringCloudDemo​github.com

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

CSDN:

https://blog.csdn.net/dkbnull/article/details/89223691​blog.csdn.net

微信:

Spring Boot整合Spring Cloud​mp.weixin.qq.com

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

微博:

Spring Cloud整合Spring Boot(服务提供者和服务消费者)​weibo.com

ideal新建springboot工程_Spring Cloud整合Spring Boot(服务提供者和服务消费者)

继续阅读