spring-cloud - 服务之间的通信
上文中已经讲述了基本环境搭建,本文基于上文环境https://www.cnblogs.com/xxpandong/p/10485172.html。
spring-cloud中微服务之间通信主要有俩种形式:
- RestTemplate方式
- Feign方式
RestTempalte方式请求url硬编码在客户端,当有注册中心有多个服务时,注册中心无法知道服务由谁提供。
Feign方式由于是以接口的形式进行通信,更适合这种架构。
先来说RestTemplate方式,构建一个名为custom-common的maven-project。
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.custom.mg</groupId>
<artifactId>custom.root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>custom.common</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
application.yml:
server:
port: 8001 spring:
application:
name: custom-common #注册中心指向start
eureka:
instance:
instance-id: custom-common
appname: ${spring.application.name}
client:
service-url:
#url前面增加注册中心账号以及密码
defaultZone: http://admin:123@127.0.0.1:8080/eureka/ #注册中心指向end
编写controller
package custom.common.controller; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
*
*
* @Title SayController.java
* @Packge custom.common.controller
* @Description TODO(用一句话描述该类的作用)
* @Author Pandong
* @Date 2019年2月14日
*/
@RestController
@RequestMapping(value="/common")
public class SayController { @Value("${server.port}")
private String port; @GetMapping(value="/hello")
public String say() {
return "Hello!!I'm server :"+port;
} }
编写入口类启动注册到注册中心。
再构建一个名为custom-role的maven-project
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.custom.mg</groupId>
<artifactId>custom.root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>custom.role</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
application.yml:
server:
port: 8002 spring:
application:
name: custom-role #注册中心指向start
eureka:
instance:
instance-id: custom-role
appname: ${spring.application.name}
client:
service-url:
#url前面增加注册中心账号以及密码
defaultZone: http://admin:123@127.0.0.1:8888/eureka/ #注册中心指向end
注意:
这里使用了defaultZone: http://admin:123@127.0.0.1:8888/eureka/。@前面的是对应注册中心设置的账号、密码。
上篇章讲述到访问注册中心会出现红字提醒,是因为没有为注册中心配置密码,任何服务都能注册进来,这里将密码配置进来,修改注册中心的相关文件
——————————————————————————————————————————————————————————————————————————————————————
pom.xml中增加依赖:
<!-- 密码配置依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.yml完整配置:
server:
port: 8080
spring:
security:
user:
name: admin # 配置登录的账号是admin
password: 123 # 配置登录的密码是123
eureka:
instance:
hostname: localhost
client:
# 是否要注册到其他Eureka Server实例
register-with-eureka: false
# 是否要从其他Eureka Server实例获取数据
fetch-registry: false
service-url:
defaultZone: http://{spring.security.user.name}:{spring.security.user.password}@{hostname}:{server.port}/eureka/
server:
# 关闭注册中心对服务的保护措施(如果服务挂掉了,又没进行该配置,那么服务会以UP状态一直存在于注册中心。反之会在3分钟内容在注册中心下线)
enableSelfPreservation: false
增加安全策略配置:
package com.server.config; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; /**
* Spring Cloud Finchley及更高版本,必须添加如下代码,部分关闭掉Spring Security的CSRF保护功能,否则应用无法正常注册!
* {@link http://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#_securing_the_eureka_server}
*
* @title SecurityConfig.java
* @package com.server.config
* @description TODO(一句话描述该类作用)
* @author Pandong
* @date 2019年2月18日
*/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
启动注册中心,访问localhost:8080,会出现登录页面,输入配置的账号、密码即可访问,进去后会发现之前的红字提醒不见了,但是,又出现新的红字警示,这是因为application.yml中增加了enableSelfPreservation: false该项配置所引起,可以不用理会。
——————————————————————————————————————————————————————————————————————————————————————
编写新建工程入口类:
package com.user; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient
public class RoleApplication { public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
} }
编写Controller:
package custom.role.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
*
*
* @Title SayApiController.java
* @Packge custom.role.controller
* @Description TODO(用一句话描述该类的作用)
* @Author Pandong
* @Date 2019年2月14日
*/
@RestController
@RequestMapping(value="/api")
public class SayApiController { @Value("${server.port}")
private String port; @Autowired
private RestTemplate restTemplate; @Autowired
private LoadBalancerClient loadBalancerClient; /**
* 第一种方式,直接使用RestTemplate,url写死。
* 因为服务端的 api 被硬编码在客户端,因此有两个缺点:
* – 当注册中心有很多服务时,我们可能不知道我们需要的服务由谁提供、api是多少,因此就可能无法调用到服务。
* –当某个服务部署了多个,例如 api 是: “localhost:8080/hello,localhost:8081/hello “,那么此时就需要负载均衡,这种硬编码显然不符合场景。
* @return
*/
@GetMapping(value="/say")
public String say() {
RestTemplate template = new RestTemplate();
return template.getForObject("http://127.0.0.1:8001/common/hello", String.class);
} /**
* 第二种方式:客户端通过 LoadBalancerClient 来获取应用名,进而获取地址和端口,在格式化拼接地址,从而调用 product服务。
* 缺点是每次调用服务都要这样写,编码很麻烦。
*/
@GetMapping("/say2")
public String say2() { RestTemplate restTemplate = new RestTemplate();
ServiceInstance serviceInstance = loadBalancerClient.choose("custom-common"); // serviceId 为提供服务的应用名
String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort() + "/common/hello");
String response = restTemplate.getForObject( url, String.class);
return response;
} /**
* 第三种方式:通过 @LoadBalanced,可在restTemplate 直接使用应用名字。
* 这种方式需要编写配置类,{@link custom.role.config.ConfigBeans}
*/
@GetMapping("/say3")
public String say3() {
return restTemplate.getForObject("http://custom-common/common/hello", String.class);
} }
RestTemplate配置类
package custom.role.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; @Configuration
public class ConfigBeans { @Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
} }
启动服务,一次访问,你会发现最后都会调用到custom-comon的服务中,这种硬编码在代码中的方式,显然是不合适的。
下面来说使用Feign方式进行通信。
在custom-role中添加依赖:
<!-- feign组件依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
入口类上面添加注解@EnableFeignClients。
然后编写feign通信接口:
package custom.role.service; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* 定义Feign接口
* @Title ServerService.java
* @Packge custom.role.service
* @Description TODO(用一句话描述该类的作用)
* @Author Pandong
* @Date 2019年2月14日
*/
@FeignClient(name = "custom-common") // 调用服务名
public interface ServerService { @RequestMapping(method = RequestMethod.GET,value = "/common/hello") // 访问路径,要与服务中提供的一致
String hello(); }
@FeignClient(name = "custom-common"),custom-common对应的是服务提供者的服务名
编写FeignController控制器:
package custom.role.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import custom.role.service.ServerService; /**
* Feign通信控制器
* @Title FeignClientController.java
* @Packge custom.role.controller
* @Description 以Feign方式进行服务之间的通信
* @Author Pandong
* @Date 2019年2月14日
*/ @RestController
@RequestMapping(value="/feign")
public class FeignClientController { @Autowired
private ServerService service; @RequestMapping(method = RequestMethod.GET,value = "/say")
public String say() {
return service.hello();
} }
方法上只能使用RequestMapping,不能使用GetMapping之类的注解。
启动服务后访问/feign/say,你会发现同样会调用的custom-common中的服务。
相信到这里大家对于选择哪种方式就不用多说了。
最后说一句,由于在写学习日记的时候是另外一个版本,后面有重新搭建了一个更新的版本,文中都是从本地写好的日记中拷贝的,有些地方对应不上基础篇的地方就自行修改一下。
spring-cloud - 服务之间的通信的更多相关文章
- Spring Cloud 服务之间调用
微服务之多个服务间调用 现在又一个学生微服务 user 和 学校微服务 school,如果user需要访问school,我们应该怎么做? 1.使用RestTemplate方式 添加config imp ...
- Spring Cloud服务注册中心交付至kubernetes
前言 服务发现原则: 各个微服务在启动时,会将自己的网络地址等信息注册到服务发现组件中,服务发现组件会存储这些信息 服务消费者可以从服务发现组件中查询到服务提供者的网络地址,并使用该地址来远程调用服务 ...
- spring cloud 服务A调用服务B自定义token消失,记录
后端:spring cloud 前端:vue 场景:前端ajax请求,包装自定义请求头token到后台做验证,首先调用A服务,A服务通过Feign调用B服务发现自定义token没有传到B服务去; 原因 ...
- Spring Cloud 服务网关Zuul
Spring Cloud 服务网关Zuul 服务网关是分布式架构中不可缺少的组成部分,是外部网络和内部服务之间的屏障,例如权限控制之类的逻辑应该在这里实现,而不是放在每个服务单元. Spring Cl ...
- 10月9日Android学习笔记:活动与服务之间的通信
最近在照着<第一行代码>这本书来学安卓,顺便记下笔记.主要的内容是Android中服务的第二种启动方式,通过活动绑定服务来启动服务,实现活动与服务之间的通信. 一. 首先创建一个服务类 p ...
- Spring Cloud 服务端注册与客户端调用
Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务 ...
- 【spring cloud】在spring cloud服务中,打包ms-core失败,报错Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) on project
在spring cloud服务中,有一个ms-code项目,只为所有的微服务提供核心依赖和工具类,没有业务意义,作为核心依赖使用.所以没有main方法,没有启动类. 在spring cloud整体打包 ...
- spring cloud 服务注册中心eureka高可用集群搭建
spring cloud 服务注册中心eureka高可用集群搭建 一,准备工作 eureka可以类比zookeeper,本文用三台机器搭建集群,也就是说要启动三个eureka注册中心 1 本文三台eu ...
- 【spring cloud】spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient
spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient的区别
随机推荐
- 自我介绍&友链
目录 此博客 我是谁? 友链 此博客 此博客主要更新一些题解什么的...(随缘啦) 本蒟蒻太菜了,博客中出现了错误希望指出,谢谢! . . 我是谁? SD的一名蒟蒻!emm... 喜欢摸鱼. 喜欢抽卡 ...
- “帮你APP”团队冲刺8
1.整个项目预期的任务量 (任务量 = 所有工作的预期时间)和 目前已经花的时间 (所有记录的 ‘已经花费的时间’),还剩余的时间(所有工作的 ‘剩余时间’) : 所有工作的预期时间:88h 目前已经 ...
- 【精彩回顾】第二届微医前端技术沙龙(附PPT下载)
5 月 25 日,以「无界」为主题的第二届微医前端技术沙龙成功举办.本届沙龙的演讲题目涵盖了前端技术几个主要的应用场景,包括服务端.桌面端以及跨平台的开发.最近几年前端技术发展非常快,各种可以提高开发 ...
- 非常全面的vim配置文件
1.mac下vim全局配置目录 /usr/share/vim/vimrc 一般不对此文件做修改,在用户目录下创建自定义配置,目录为: /Users/xxxxx cd ~ 2自定义vim配置 配置功能: ...
- Recommender System
推荐系统我们都很熟悉,淘宝推荐用户可能感兴趣的产品,搜索引擎帮助用户发现可能感兴趣的东西,这些都是推荐系统的内容.接下来讲述一个电影推荐的项目. Netflix 电影推荐系统 这个项目是使用的Netf ...
- STL之string使用简介
声明一个C++字符串 string类的构造函数和析构函数如下: string s; //生成一个空字符串s string s(str) //拷贝构造函数 生成str的复制品 string s(str, ...
- c语言有用函数收集
1 strtok :分解字符串为一组字符串.s为要分解的字符串,delim为分隔符字符串.首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL. 2 strstr :从字符串str1中查找 ...
- 【bzoj1415】[Noi2005]聪聪和可可 期望记忆化搜索
题目描述 输入 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点的编号. 接下来E行 ...
- [ZZOJ#31]类欧几里得
[ZZOJ#31]类欧几里得 试题描述 这是一道模板题. 给出 \(a, b, c, n\),请你求出 \(\sum_{x=0}^n{\lfloor \frac{a \cdot x + b}{c} \ ...
- [bzoj4259][bzoj4503] 残缺的字符串 [FFT]
题面 传送门 bzoj上的这两题是一样的...... 正文 我看到这道题,第一想法是跑魔改过的KMP,然后很快发现不可行 于是想换个角度思考 其实,本题最大的问题就在于通配符的存在:它可以匹配任意一个 ...