網(wǎng)站制作公司源碼北京百度競(jìng)價(jià)托管
需求:公司產(chǎn)品一直是nodejs的后臺(tái),采用的eggjs框架,也不是最新版本,現(xiàn)有有需求需求將這些應(yīng)用集成到微服務(wù)的注冊(cè)中心,領(lǐng)導(dǎo)要求用java。
思路:用spring cloud gateway將需要暴露的接口url轉(zhuǎn)發(fā),并將這個(gè)gateway注冊(cè)到注冊(cè)中心
方案:
1、轉(zhuǎn)發(fā)原有nodejs的后臺(tái)服務(wù)
用Springboot建立一個(gè)gateway項(xiàng)目,引入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 #注冊(cè)到nacosservice=test-provider: gatewaygateway:discovery:locator:enabled: true #開啟從注冊(cè)中心動(dòng)態(tài)創(chuàng)建路由的功能,利用微服務(wù)名進(jìn)行路由routes:- id: gateway1 #路由的ID,沒有固定規(guī)則但要求唯一,建議配合服務(wù)名uri: http://IP:PORT #匹配nodejs的服務(wù)IP及端口號(hào)predicates:- Path=/你的需要暴露的服務(wù)的根路徑/** # 斷言,路徑相匹配的進(jìn)行路由
app文件
@SpringBootApplication
@EnableDiscoveryClient // cloud 需要 注冊(cè)中心==發(fā)現(xiàn)
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}
啟動(dòng)后在nacos中,可以看到此項(xiàng),這時(shí)可以測(cè)試9984端口已經(jīng)轉(zhuǎn)發(fā)了原有nodejs的服務(wù)
2、消費(fèi)者
需要引入loadblancer否則報(bào)錯(cuò)
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId><version>3.1.1</version></dependency>
(1)RestTemplate進(jìn)行服務(wù)請(qǐng)求
新建一個(gè)MyConfiguration類對(duì)RestTemplate進(jìn)行配置
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();}
}
在使用的類進(jìn)行初始化
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);//請(qǐng)求String result = restTemplate.postForObject(url, request, String.class);//System.out.println(result);
這種模式可以很好的調(diào)用springboot 自己發(fā)布的URL,但是不知道為什么請(qǐng)求對(duì)nodejs的服務(wù)返回的為亂碼,所有的都亂碼不只是中文亂碼。
分析認(rèn)為是GZIP的原因,nodejs的服務(wù)打開了GZIP,所以這里需要處理。可以用以下進(jìn)行GZIP處理,完美結(jié)局
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,此種方法更加優(yōu)雅
引用
<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中服務(wù)名稱
public interface ProviderFeignClient {//這里是post請(qǐng)求,json格式參數(shù)的例子@PostMapping("/.....")//這里寫具體的路徑String tblcodefactoryv6_GetList(@RequestBody JSONObject pars);}
使用時(shí)就很簡(jiǎn)單了
//初始化@AutowiredProviderFeignClient providerFeignClient;//方法中這樣用就好了,簡(jiǎn)潔result = providerFeignClient.tblcodefactoryv6_GetList(pars);
以上就是使用gateway將nodejs的服務(wù)加入到nacos注冊(cè)中心作為微服務(wù)的過(guò)程。