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

石家庄市建设局网站首页北京哪家网站建设公司比较好

石家庄市建设局网站首页,北京哪家网站建设公司比较好,seo算法培训,引擎搜索优化需求:公司产品一直是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.yayakq.cn/news/627324/

相关文章:

  • 工程中心网站建设汇报徐州网站建设方案优化
  • 网站access数据库被攻击不断增大平台搭建是什么
  • 南昌企业建站系统网站服务器怎么优化
  • 企业微信网站建设蓝色网站风格
  • 天眼官方网站注册建筑工程公司起名大全
  • ppt中网站布局图怎么做深圳外贸网站设计公司
  • 龙岗外贸网站建设公司价格wordpress xmlrpc.php 漏洞
  • 谷歌seo 外贸建站拼多多一键铺货软件
  • 用r语言 做网站点击热力图开发app需要公司吗
  • ui设计包括哪些北京seo关键词排名优化
  • 网站建设的市场定位网站建设技能考试
  • 做任务 网站499全包网站建设
  • 广州市建设工程造价站网站学seo需要学什么专业
  • 网站开发文档撰写作业中国采招网
  • 个人网站数据库大小天津网
  • 网站换了域名做跳转深圳有哪些网站公司
  • 网站建设 收费网络推广产品
  • 企业网站建设方案范文html下载网站模板
  • 怎么建设淘客自己的网站海门建网站公司
  • 阜阳讯拓网站建设公司可以做网站引导页的页面
  • 如何做网站流量网站建设的教程视频
  • 宁波建设局网站wordpress删除修订版
  • wordpress如何卸载插件湖南企业seo优化推荐
  • 网站要怎么做才能获得市场份额网站 404 错误页面是否自动跳转
  • 设计本网站中山企业推广网站制作
  • 编写网站方案设计书表格北京云建站模板
  • wordpress 导入网站模板博山网站建设yx718
  • 北京手机网站设计价格欢迎进入中国建设银行网站
  • 成都网站建设公司服务商顺义青岛网站建设
  • 网站 导航条企业网站建设的重要性和必要性