Nacos注册中心通过FeginAB跨服务调用

2021-01-29  本文已影响0人  久伴我还是酒伴我

简介

Nacos 已经作为注册中心了,现在咱们实现A 服务通过注册中心调用B服务,本文通过Fegin实现。

B服务

application.properties

server.port=8081
spring.application.name=sharding-demo
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Pom文件

 <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.0.RELEASE</version>
   </dependency>

启动入口

package com.sharding.test;

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

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

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

}

package com.sharding.test.biz.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author leijie.gao
 * @version 1.0.0
 * @ClassName JasptEncryptorController.java
 * @Description TODO
 * @createTime 2020年12月01日 16:19:00
 */
@Slf4j
@RestController
@RequestMapping("/sharding/jaspt")
public class JasptEncryptorController {
    @ApiOperation(value = "B服务")
    @PostMapping(value = "/b-service")
    public String bservice(@RequestParam("content") String content) {
        log.info("接收到A服务内容:"+content);
        return "my is b service";
    }

}

A 服务

application.properties

spring.application.name=webservice
server.port=8080
swagger2.enable=true
#IP+端口
nacos.discovery.server-addr=127.0.0.1:8848

Pom文件

 <dependency>
       <groupId>com.alibaba.cloud</groupId>
       <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
       <version>2.2.0.RELEASE</version>
   </dependency>
 <!--fegin-->
 <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-openfeign</artifactId>
       <version>2.2.3.RELEASE</version>
  </dependency>

启动入口

package com.glj.webservice.demo;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class WebserviceDemoApplication {

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

Client

package com.glj.webservice.demo.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author leijie.gao
 * @version 1.0.0
 * @ClassName JasptEncryptorClient.java
 * @Description TODO
 * @createTime 2021年01月28日 14:50:00
 */
@FeignClient(name = "sharding-demo",path = "/sharding/jaspt")
@Component
public interface JasptEncryptorClient {

    @RequestMapping(value = "/b-service",method = RequestMethod.POST)
    String jasyptEncrypt(@RequestParam("content") String content);
}

开始调用

Nacos注册中心管理列表

A服务调用

入参 出参 A服务打印日志

B服务

B服务日志打印
上一篇下一篇

猜你喜欢

热点阅读