栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(单力测试及源码),Gateway+FeignClient+Nacos通过网关远程调用微服务(一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(单力测试及源码),Gateway+FeignClient+Nacos通过网关远程调用微服务(一)

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(单力测试及源码),Gateway+FeignClient+Nacos通过网关远程调用微服务(一)

问题背景项目搭建

1 gateway网关搭建 2 service1项目搭建

3 service2项目搭建 测试步骤心得Lyric: 种麦芽糖的山坡

问题背景

使用gateway网关远程调用微服务非常常用,并配合追踪链路,本文先介绍使用gateway网关配置路由的方式访问远程微服务
注意事项:

默认JDK环境nacos服务器的搭建和外部调用微服务的方式可以参考我的另一篇文章大家可以通过本文的代码自己创建工程,也可以直接下载本文源码 项目搭建 1 gateway网关搭建

1.1 创建gateway工程,引入pom依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.1.RELEASE
         
    
    com.yg
    gateway
    0.0.1-SNAPSHOT
    gateway
    gateway
    
        1.8
    
    

        
            org.springframework.cloud
            spring-cloud-starter-gateway

        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test

        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery

        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.RELEASE
                pom
                import
            

            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.2.1.RELEASE
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    

1.2 添加application配置,- Path=/three/** #路径匹配规则,微服务控制类必须有一个统一的入口区分域,不然网关不能访问,@RequestMapping("/three"),@RequestMapping("/four")

server:
  port: 3900

spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
#      routes:
#        - id: service2  #设置路由id(理论上是可以随便写的)
#          uri: lb://service2 #设置路由的url lb://nacos服务注册名称 lb://service1 http://localhost:3901
#          predicates:
#            - Path=/three/** #路径匹配规则,微服务必须有一个统一的入口,不然网关不能访问,@RequestMapping("/three")
#        - id: service1  #设置路由id(理论上是可以随便写的)
#          uri: lb://service1 #设置路由的url lb://nacos服务注册名称 lb://service1 http://localhost:3901
#          predicates:
#            - Path=/four/** #路径匹配规则,@RequestMapping("/four")

1.3 gateway启动类

package com.yg.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

}
2 service1项目搭建

2.1 创建service1项目,引入pom公共依赖



    4.0.0
    com.yg
    sleuthTest
    0.0.1-SNAPSHOT
    pom

    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.1.RELEASE
         
    

    
        service1
        service2
    

    sleuthTest
    sleuthTest
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        


        
            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.1.0.RELEASE
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery

        

    

    
        
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.RELEASE
                pom
                import
            

            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.2.1.RELEASE
                pom
                import
            

        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


2.2 引入service1单独的pom文件



    4.0.0
    com.yg
    service1
    0.0.1-SNAPSHOT
    jar

    
        com.yg
        sleuthTest
        0.0.1-SNAPSHOT
    

    service1
    service1
    
        1.8
    
    

    


2.3 service1启动类,添加@EnableDiscoveryClient、@EnableFeignClients注解

package com.yg.service1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
public class Service1Application {

    public static void main(String[] args) {
        SpringApplication.run(Service1Application.class, args);
    }
}

2.4 添加application配置,添加nacos注册中心

server:
  port: 3901

spring:
  application:
    name: service1
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848  # 以服务名进行注册

2.5 创建controller类

package com.yg.service1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/four")
public class Service1Controller {

    @Autowired
    private Service2Controller service2Controller;

    @GetMapping("/t1")
    public String test(){
        System.out.println("Test success");
        return "I am YuanGe";
    }

   @GetMapping("/test1")
   public String test1(){
       System.out.println("test11111");
       System.out.println(service2Controller.getTest2());
       return "I am test11111";
   }
}

2.6 创建feignclient远程调用接口类

package com.yg.service1.controller;

import com.yg.service1.fallback.Service2ControllerFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "service2",fallbackFactory = Service2ControllerFallbackFactory.class)
public interface Service2Controller {

    @GetMapping("/three/test2")
    String getTest2();
}

2.7 实现FallbackFactory

package com.yg.service1.fallback;

import com.yg.service1.controller.Service2Controller;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

@Component
public class Service2ControllerFallbackFactory implements FallbackFactory {
    @Override
    public Service2Controller create(Throwable throwable) {
        return null;
    }
}

3 service2项目搭建

3.1 引入pom依赖



    4.0.0
    com.yg
    service2
    0.0.1-SNAPSHOT
    jar

    
        com.yg
        sleuthTest
        0.0.1-SNAPSHOT
    

    service2
    service2
    
        1.8
    
    

    

3.2 添加application配置

server:
  port: 3902

spring:
  application:
    name: service2
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

3.3 service2启动类

package com.yg.service2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class Service2Application {

    public static void main(String[] args) {
        SpringApplication.run(Service2Application.class, args);
    }
}

3.4 service2控制类

package com.yg.service2.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/three")
public class Service2Controller {

    @GetMapping("/t2")
    public String test(){
        System.out.println("Received");
        return "I am YuanGe";
    }

    @GetMapping("/test2")
    public String test2(){
        System.out.println("test22222");
        return "I am test22222";
    }

}

3.5 整体项目架构目录

测试步骤

1 启动nacos服务端,gateway,service1,service2



2 查看注册中心页面http://localhost:8848/nacos/,账号密码都为:nacos

3 使用service1微服务名远程调用service2微服务


心得

在测试的途中都遇到了几个小问题,写了一个错误的配置如下,- Path=/** #这样写没有问题,但是控制类需要属于不同的域,如果没有在控制类上写上@RequestMapping注解,微服务过多就找不到API了

spring:
  application:
    name: gateway
  cloud:
    gateway:
      routes:
        - id: service2  
          uri: lb://service2 
          predicates:
            - Path=/** #这样写单服务是没有问题,但是微服务有两个及以上,控制类需要属于不同的域,如果没有在控制类上写上@RequestMapping注解,微服务过多就找不到API了
        - id: service1  
          uri: lb://service1 
          predicates:
            - Path=/** 

service2和service1分别添加@RequestMapping("/three"),@RequestMapping("/four")注解,更改写法:

spring:
  application:
    name: gateway
  cloud:
    gateway:
      routes:
        - id: service2  #设置路由id(理论上是可以随便写的)
          uri: lb://service2 #设置路由的url lb://nacos服务注册名称 lb://service1 http://localhost:3901
          predicates:
            - Path=/three/** #路径匹配规则,微服务必须有一个统一的入口,不然网关不能访问,@RequestMapping("/three")
        - id: service1  #设置路由id(理论上是可以随便写的)
          uri: lb://service1 #设置路由的url lb://nacos服务注册名称 lb://service1 http://localhost:3901
          predicates:
            - Path=/four/** #路径匹配规则,@RequestMapping("/four")





作为程序员第 18 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 种麦芽糖的山坡
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704082.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号