上文中已经讲述了基本环境搭建,本文基于上文环境https://www.cnblogs.com/xxpandong/p/10485172.html

  spring-cloud中微服务之间通信主要有俩种形式:

  1. RestTemplate方式
  2. 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 - 服务之间的通信的更多相关文章

  1. Spring Cloud 服务之间调用

    微服务之多个服务间调用 现在又一个学生微服务 user 和 学校微服务 school,如果user需要访问school,我们应该怎么做? 1.使用RestTemplate方式 添加config imp ...

  2. Spring Cloud服务注册中心交付至kubernetes

    前言 服务发现原则: 各个微服务在启动时,会将自己的网络地址等信息注册到服务发现组件中,服务发现组件会存储这些信息 服务消费者可以从服务发现组件中查询到服务提供者的网络地址,并使用该地址来远程调用服务 ...

  3. spring cloud 服务A调用服务B自定义token消失,记录

    后端:spring cloud 前端:vue 场景:前端ajax请求,包装自定义请求头token到后台做验证,首先调用A服务,A服务通过Feign调用B服务发现自定义token没有传到B服务去; 原因 ...

  4. Spring Cloud 服务网关Zuul

    Spring Cloud 服务网关Zuul 服务网关是分布式架构中不可缺少的组成部分,是外部网络和内部服务之间的屏障,例如权限控制之类的逻辑应该在这里实现,而不是放在每个服务单元. Spring Cl ...

  5. 10月9日Android学习笔记:活动与服务之间的通信

    最近在照着<第一行代码>这本书来学安卓,顺便记下笔记.主要的内容是Android中服务的第二种启动方式,通过活动绑定服务来启动服务,实现活动与服务之间的通信. 一. 首先创建一个服务类 p ...

  6. Spring Cloud 服务端注册与客户端调用

    Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务 ...

  7. 【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整体打包 ...

  8. spring cloud 服务注册中心eureka高可用集群搭建

    spring cloud 服务注册中心eureka高可用集群搭建 一,准备工作 eureka可以类比zookeeper,本文用三台机器搭建集群,也就是说要启动三个eureka注册中心 1 本文三台eu ...

  9. 【spring cloud】spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient

    spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient的区别

随机推荐

  1. Python框架之Django学习笔记(十四)

    Django站点管理(续·完) 本想昨天更新的,谁曾想昨天竟然是工作日!我就不吐槽昨天加班到十一点多了,需求增加无疑让我等蛋疼不已,忽而想起一首打油诗: 明月几时有,把酒问群友.不知这次版本,今晚能出 ...

  2. 3、CSS基础 part-1

    1.给body设置颜色 <html> <body text="red"> <p> hello world</p> <p> ...

  3. IOS开发学习笔记041-UITableView总结1

    一.UITableView的常用属性 1.分割线 // 分割线 self.tableView.separatorColor = [UIColorredColor]; // 隐藏分割线 self.tab ...

  4. Leetcode 526.优美的排列

    优美的排列 假设有从 1 到 N 的 N 个整数,如果从这 N 个数字中成功构造出一个数组,使得数组的第 i 位 (1 <= i <= N) 满足如下两个条件中的一个,我们就称这个数组为一 ...

  5. [python][django 1.10中文文档]

    https://docs.djangoproject.com/en/1.10/  官方文档,点我下载 推荐一个翻译django 1.8.2的网址: 推荐一个翻译django 1.10的博客:(着重推荐 ...

  6. 位图 c++ 位图排序

    什么是位图?来自http://www.cnblogs.com/dolphin0520/archive/2011/10/19/2217369.html 位图就是用一个bit来标记某个元素对应的值,键值就 ...

  7. [转]Docker容器内不能联网的6种解决方案

    注: 下面的方法是在容器内能ping通公网IP的解决方案,如果连公网IP都ping不通,那主机可能也上不了网(尝试ping 8.8.8.8) 1.使用--net:host选项 sudo docker ...

  8. Android之Bitmap 高效加载

    一张图片(BitMap)占用的内存=图片长度*图片宽度*单位像素占用的字节数 图片格式(Bitmap.Config) 一张100*100的图片占用内存的大小 ALPHA_8 图片长度*图片宽度 100 ...

  9. HTML表单与文件

    <!DOCTYPE html> <html> <head lang="en"> <title></title> < ...

  10. nginx通过spawn-fcgi调用C++写的cgi程序

    通过apt-get install 安装nginx和spawn-fcgi /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9002 -C 25 -f  ...