当前位置: 首页 > news >正文

做网站首页尺寸大小百度精准获客平台

做网站首页尺寸大小,百度精准获客平台,动漫制作软件,开锁做网站哪个好需求:公司产品一直是nodejs的后台,采用的eggjs框架,也不是最新版本,现有有需求需求将这些应用集成到微服务的注册中心,领导要求用java。 思路:用spring cloud gateway将需要暴露的接口url转发,…

需求:公司产品一直是nodejs的后台,采用的eggjs框架,也不是最新版本,现有有需求需求将这些应用集成到微服务的注册中心,领导要求用java。

思路:用spring cloud gateway将需要暴露的接口url转发,并将这个gateway注册到注册中心

方案:

1、转发原有nodejs的后台服务

用Springboot建立一个gateway项目,引入gateway

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId><version>3.1.2</version></dependency>

配置文件

server:port: 9984 #此模块的PORTservlet:encoding:force: truecharset: UTF-8enabled: truetomcat:uri-encoding: UTF-8spring:application:name: gatewaycloud:nacos:discovery:server-addr: localhost:8848 #注册到nacosservice=test-provider: gatewaygateway:discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由routes:- id: gateway1 #路由的ID,没有固定规则但要求唯一,建议配合服务名uri: http://IP:PORT #匹配nodejs的服务IP及端口号predicates:- Path=/你的需要暴露的服务的根路径/** # 断言,路径相匹配的进行路由
app文件
@SpringBootApplication
@EnableDiscoveryClient // cloud 需要 注册中心==发现
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}

启动后在nacos中,可以看到此项,这时可以测试9984端口已经转发了原有nodejs的服务

2、消费者

需要引入loadblancer否则报错

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId><version>3.1.1</version></dependency>

(1)RestTemplate进行服务请求

新建一个MyConfiguration类对RestTemplate进行配置

package org.example;
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 MyConfiguration {@LoadBalanced@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}
}

在使用的类进行初始化

private final RestTemplate restTemplate;@Autowiredpublic ConsumerController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

使用

String url = "http://nacos中模块名称/...";HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);HttpEntity<String> request = new HttpEntity<>(pars.toJSONString(), headers);//请求String result = restTemplate.postForObject(url, request, String.class);//System.out.println(result);

这种模式可以很好的调用springboot 自己发布的URL,但是不知道为什么请求对nodejs的服务返回的为乱码,所有的都乱码不只是中文乱码。

分析认为是GZIP的原因,nodejs的服务打开了GZIP,所以这里需要处理。可以用以下进行GZIP处理,完美结局

ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(url, request, byte[].class);GZIPInputStream fi = new GZIPInputStream(new ByteArrayInputStream(responseEntity.getBody()));BufferedReader reader = new BufferedReader(new InputStreamReader(fi));StringWriter writer = new StringWriter();String line = "";while ((line = reader.readLine()) != null) {writer.write(line);}result = writer.toString();

(2)FeignClient,此种方法更加优雅

引用

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>3.1.1</version></dependency>

接口

package org.example;import com.alibaba.fastjson.JSONObject;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;/*** @author * * @date 2024/12/8*/
@FeignClient("gateway")//这里写nacos中服务名称
public interface ProviderFeignClient {//这里是post请求,json格式参数的例子@PostMapping("/.....")//这里写具体的路径String tblcodefactoryv6_GetList(@RequestBody JSONObject pars);}

使用时就很简单了

//初始化@AutowiredProviderFeignClient providerFeignClient;//方法中这样用就好了,简洁result = providerFeignClient.tblcodefactoryv6_GetList(pars);

以上就是使用gateway将nodejs的服务加入到nacos注册中心作为微服务的过程。

http://www.hkea.cn/news/534876/

相关文章:

  • dw做汽车网站seo排名工具提升流量
  • 网络培训学校排名奉化seo页面优化外包
  • vps除了做网站还能做什么晨阳seo服务
  • seo网站建设优化什么意思网络营销与直播电商专业就业前景
  • 工程建设企业网站网站关键词优化应该怎么做
  • 修复wordpress青岛网站优化
  • 敦煌网站做外贸怎样网页推广怎么做的
  • 南京网站建设优化今日头条普通版
  • 网站编辑的工作职能有哪些活动营销案例100例
  • 小程序招商加盟平台我是seo关键词
  • wordpress 发帖机镇江抖音seo
  • 网站建设的小结可以发外链的论坛有哪些
  • 网站正常打开速度网店营销与推广策划方案
  • 义乌 网站制作进入百度app
  • 做外围网站赌球红树林seo基础入门免费教程
  • 绿色风格网站seo排名赚钱
  • 南宁企业免费建站百度推广营销怎么做
  • 建立个人网站的成本短视频seo营销系统
  • 深圳公司名称大全网站结构优化的内容和方法
  • 安康市代驾公司上海网站关键词排名优化报价
  • 怎么在网站上建设投票统计在线培训系统app
  • 泰州网站建设哪家好网站seo的主要优化内容
  • 洛卡博网站谁做的seo权重查询
  • 东莞网络科技公司有哪些山东网站seo
  • 网站建设需要学什么网站模板购买
  • 用html做的游戏网站关键词推广效果分析
  • 做影视网站引流正规推广平台有哪些
  • 免费下载简历模板北京seo排名厂家
  • 西昌市做网站的百度搜索排名靠前
  • 办公室装修实景拍摄图重庆seo俱乐部联系方式