栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot+nacos+rpc(dubbo)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot+nacos+rpc(dubbo)

 第一,nacos服务名到ip的解析(无dubbo)

controller

package com.yanghongzhou.nacos.controller;

import java.net.URI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/nacos")
public class NacosTestController {

	@Autowired
	RestTemplate restTemplate;
	@Value("${server.port}")
	String port;
	
	@RequestMapping("/test")
	public String test() throws Exception {
		System.out.println("laile," + port);
//		URI url = new URI("http://10.1.2.106:8088/cmp/nacos/testService");
		URI url = new URI("http://nacos-test-service/nacos/testService");
		String msg = restTemplate.getForObject(url, String.class);
		return "可不是直接访问:" + msg;
	}
	@RequestMapping("/testService")
	public String testService() throws Exception {
		return "hello nacos!" + port;
	}
}

如上,test通过服务名调testService,testService可以在另一个工程中

浏览器测试

第二,整合dubbo,目的是controller可以访问service

controller及service

package com.yanghongzhou.nacos.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yanghongzhou.nacos.dubbo.service.DubboTestService;

@RestController
@RequestMapping("/dubbo")
public class DubboTestController {

	@DubboReference
	private DubboTestService dubboTestService;
	
	@RequestMapping("/test")
	public String testDubbo() {
		return dubboTestService.testDubbo();
	}
}
package com.yanghongzhou.nacos.dubbo.service;

import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class DubboTestServiceImpl implements DubboTestService{

	@Override
	public String testDubbo() {
		return "这是rpc的dubbo远程service访问";
	}

}

很简单,两个注解,controller用  @DubboReference,service用@DubboService

浏览器测试

第三,依赖

1,application.yml

server:
  port: 8088
spring:
  application:
    name: nacos-test-service
  cloud:
    nacos:
      server-addr: 10.1.2.106:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
dubbo:
  application:
    name: dubbo-test-service
  registry:
    address: nacos://10.1.2.106:8848
    username: nacos
    password: nacos
  scan:
    base-packages: com.yanghongzhou.nacos.dubbo.service
  protocol:
    name: dubbo
    port: 15511

2,pom.xml  注意版本


	4.0.0
	nacos-test
	nacos-test
	0.0.1-SNAPSHOT

	
		
			org.springframework.boot
			spring-boot-starter-web
			2.4.2
		
		
			com.alibaba.cloud
			spring-cloud-starter-alibaba-nacos-discovery
			2021.1
		
		
			org.springframework.cloud
			spring-cloud-starter-loadbalancer
			3.0.1
		
		
			org.yaml
			snakeyaml
			1.30
		
		
			org.springframework
			spring-context
			5.3.3
		


		
		
			org.apache.dubbo
			dubbo-spring-boot-starter
			2.7.11
		
		
			org.apache.dubbo
			dubbo
			2.7.11
		
		
			org.apache.dubbo
			dubbo-registry-nacos
			2.7.11
		
	
 第四,说明

我为了方便,将两个功能的样例都放在一个工程中了,实际是分开的,不过这都不重要。

付nacos管理截图

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975257.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号