微服务学习笔记五 Spring Cloud 服务消费者及服务网关

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 微服务学习笔记五 Spring Cloud 服务消费者及服务网关

## 服务消费者 consumer

创建maven工程,pom.xml


```java

<dependencies>

   <dependency>

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

       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>

</dependencies>

```


创建配置文件,application.yml


```java

server:

 port: 8020

spring:

 application:

   name: consumer

eureka:

 client:

   service-url:

     defaultZone: http://localhost:8761/eureka/

 instance:

   prefer-ip-address: true

```


创建启动类


```java

package com.shuang;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication

public class ConsumerApplication {

   public static void main(String[] args) {

       SpringApplication.run(ConsumerApplication.class,args);

   }

   @Bean

public RestTemplate restTemplate(){

   return new RestTemplate();

}

}

```


Handler


```java

package com.shuang.controller;


import com.shuang.entity.Student;

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

import org.springframework.web.bind.annotation.*;

import org.springframework.web.client.RestTemplate;


import java.util.Collection;


@RestController

@RequestMapping("/consumer")

public class ConsumerHandler {

   @Autowired

   private RestTemplate restTemplate;


   @GetMapping("/findAll")

   public Collection<Student> findAll(){

       return restTemplate.getForEntity("http://localhost:8010/student/findAll",Collection.class).getBody();

   }

   @GetMapping("/findAll2")

   public Collection<Student> findAll2(){

       return restTemplate.getForObject("http://localhost:8010/student/findAll",Collection.class);

   }


   @GetMapping("/findById/{id}")

   public Student findById(@PathVariable("id") long id){

       return restTemplate.getForEntity("http://localhost:8010/student/findById/{id}",Student.class,id).getBody();

   }


   @GetMapping("/findById2/{id}")

   public Student findById2(@PathVariable("id") long id){

       return restTemplate.getForObject("http://localhost:8010/student/findById/{id}",Student.class,id);

   }


   @PostMapping("save")

   public void save(@RequestBody Student student){

       restTemplate.postForEntity("http://localhost:8010/student/save",student,null).getBody();

   }

   @PostMapping("/save2")

   public void save2(@RequestBody Student student){

       restTemplate.postForObject("http://localhost:8010/student/save",student,null);


   }


   @PutMapping("/update")

   public void update(@RequestBody Student student){

       restTemplate.put("http://localhost:8010/student/update",student);

   }


   @DeleteMapping("/deleteById/{id}")

   public void deleteById(@PathVariable("id") long id){

       restTemplate.delete("http://localhost:8010/student/deleteById/{id}",id);

   }


}

```


## 服务网关:

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200624123233579.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

Spring Cloud 集成了 Zuul组件,实现服务网关

Zuul是Netflix提供的一个开源的API网关服务器,是客户端和网站后端所有请求

的中间层,对外开放一个API,将所有请求导入统一的入口,屏蔽了服务端的具体

实现逻辑,Zuul可以实现反向代理的功能,在网关内部实现动态路由、身份认证、

IP过滤、数据监控等。

创建Maven工程,pom.xml


```java

<dependencies>

   <dependency>

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

       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>


   <dependency>

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

       <artifactId>spring-cloud-starter-netflix-zuul</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>

</dependencies>

```


创建配置文件。application.yml


```java

server:

 port: 8030

spring:

 application:

   name: gateway

eureka:

 client:

   service-url:

     defaultZone: http://localhost:8761/eureka/

zuul:

 routes:

   provider: /p/**

```


属性说明:

zuul.routers.provider:给服务提供者provider设置映射


创建启动类


```java

package com.shuang;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.cloud.netflix.zuul.EnableZuulProxy;


@EnableZuulProxy

@EnableAutoConfiguration

public class ZuulApplication {

   public static void main(String[] args) {

       SpringApplication.run(ZuulApplication.class,args);

   }

}

```


注解说明:

EnableZuulProxy:包含了EnableZuulServer,设置该类是网关的启动类。

EnableAutoConfiguretion:可以帮助Spring Boot应用将所有符合条件的

Configuration配置加载到当前Spring Boot创建并使用的IOC容器中。


Zuul自带了负载均衡功能,修改provider的代码


```java

package com.shuang.controller;



import com.shuang.entity.Student;

import com.shuang.repository.StudentRepository;

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

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.*;


import java.util.Collection;


@RestController

@RequestMapping("/student")

public class StudentHandler {

   @Autowired

   private StudentRepository studentRepository;


   @Value("${server.port}")

   private String port;


   @GetMapping("/findAll")

   public Collection<Student> findAll(){

       return studentRepository.findAll();

   }

   @GetMapping("/findById/{id}")

   public Student findById(@PathVariable("id") long id){

       return studentRepository.findById(id);

   }

   @PostMapping("/save")

   public void save(@RequestBody Student student){

       studentRepository.saveOrUpdate(student);

   }

   @PutMapping("/update")

   public void update(@RequestBody Student student){

       studentRepository.saveOrUpdate(student);

   }

   @DeleteMapping("/deleteById/{id}")

   public void deleteById(@PathVariable("id") long id){

       studentRepository.deleteById(id);

   }


   @GetMapping("/index")

   public String index(){

       return "当前的端口: "+this.port;

   }

}

```


启动eurekaclient的启动类。修改端口,增加另外一个启动类,启动(相当于两个相同服务提供者)

在Zuul中访问:两个服务提供者轮流提供服务

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200624123408289.png)

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200624123423690.png)






目录
相关文章
|
6天前
|
算法 NoSQL API
SpringCloud&Gateway网关限流
SpringCloud&Gateway网关限流
42 7
|
6天前
|
缓存
SpringCloud Gateway 网关的请求体body的读取和修改
SpringCloud Gateway 框架中,为了处理请求体body,实现多次读取与修改,创建了一个名为`RequestParamGlobalFilter`的全局过滤器。这个过滤器使用`@Component`和`@Slf4j`注解,实现了`GlobalFilter`和`Ordered`接口,设置最高优先级以首先读取body。它通过缓存请求体并创建装饰过的`ServerHttpRequest`来实现body的动态获取。
88 4
|
6天前
SpringCloud服务已经关但是Eureka还是显示up
SpringCloud服务已经关但是Eureka还是显示up
27 0
|
6天前
|
存储 SpringCloudAlibaba Java
【微服务 SpringCloud】实用篇 · 服务拆分和远程调用
【微服务 SpringCloud】实用篇 · 服务拆分和远程调用
21 2
|
6天前
|
负载均衡 Java 网络架构
在SpringCloud2023中快速集成SpringCloudGateway网关
本文主要简单介绍SpringCloud2023实战中SpringCoudGateway的搭建。后续的文章将会介绍在微服务中使用熔断Sentinel、鉴权OAuth2、SSO等技术。
42 2
在SpringCloud2023中快速集成SpringCloudGateway网关
|
6天前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
129 0
|
6天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
6天前
|
Java Maven 微服务
使用Spring Cloud Gateway构建微服务网关
以上简要示例介绍了使用Spring Cloud Gateway构建微服务网关的基本步骤,包括添加依赖、配置路由、编写过滤器等。通过配置和自定义过滤器,可以实现请求转发、过滤、鉴权等功能。
51 5
|
6天前
|
SpringCloudAlibaba 负载均衡 Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
77 1
|
6天前
|
Java Nacos Sentinel
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
249 0