码迷,mamicode.com
首页 > 其他好文 > 详细

服务之间的通信

时间:2019-03-07 17:51:40      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:alt   OLE   log   pom   get   高版本   adb   autowired   部署   

 

 

 

 上文中已经讲述了基本环境搭建,本文基于上文环境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>
View Code

 

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 
View Code

 

编写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;
    }
    
    
}
 
View Code

 

 

编写入口类启动注册到注册中心。

 

再构建一个名为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>
View Code

 

 

  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 
View Code

 注意:

    这里使用了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
View Code

 

 增加安全策略配置:

技术图片
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);
    }
}
View Code

 

启动注册中心,访问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);
    }
    
    
}
View Code

 

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();
    }

}
View Code

 

 

启动服务,一次访问,你会发现最后都会调用到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();
    
}
View Code

@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();
    }
    
}
View Code

 

方法上只能使用RequestMapping,不能使用GetMapping之类的注解。

启动服务后访问/feign/say,你会发现同样会调用的custom-common中的服务。

 

相信到这里大家对于选择哪种方式就不用多说了。

 

最后说一句,由于在写学习日记的时候是另外一个版本,后面有重新搭建了一个更新的版本,文中都是从本地写好的日记中拷贝的,有些地方对应不上基础篇的地方就自行修改一下。

 

服务之间的通信

标签:alt   OLE   log   pom   get   高版本   adb   autowired   部署   

原文地址:https://www.cnblogs.com/xxpandong/p/10490886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!