03 nacos-リモートコール
1838 ワード
前節ではnacosにサービスを登録しましたが、この節ではサービスを呼び出してみました.
1、前提制約 nacosに登録されているサービス 2、操作手順 springbootプロジェクトを作成し、次の依存関係を追加します:
注意:筆者が使用するspring-bootバージョンは2.3.7.RELEASE,spring-cloud-alibabaバージョンは2.2.2.RELEASEアプリケーションを修正する.properties: メイン起動クラスの兄弟ディレクトリの下に構成クラスを作成します: メイン起動クラスの兄弟ディレクトリの下にエントリクラスを作成する: プロジェクトの開始、アクセスhttp://localhost:10002/testを選択すると、効果が表示されます.以上がnacosリモートコールのプロセスです.
1、前提制約
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
注意:筆者が使用するspring-bootバージョンは2.3.7.RELEASE,spring-cloud-alibabaバージョンは2.2.2.RELEASE
spring.application.name=nacos-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
server.port=10002
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 NacosConfig {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class TestController {
@Resource
private RestTemplate restTemplate;
@GetMapping("/test")
public String test(){
return restTemplate.getForObject("http://nacos-provider/get", String.class);
}
}