springboot + nacos + sofarpc 整合后报错403
springboot版本 2.2.2
nacos 版本 1.4.2
rpc-sofa-boot 版本 3.2.0
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version></parent><!-- 这里写你自己的module名称 --><properties><sofa.boot.version>3.2.0</sofa.boot.version><spring.boot.version>2.2.2.RELEASE</spring.boot.version><nacos.version>1.4.2</nacos.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring.boot.version}</version><scope>import</scope><type>pom</type></dependency><dependency><groupId>com.alipay.sofa</groupId><artifactId>sofaboot-dependencies</artifactId><version>${sofa.boot.version}</version><scope>import</scope><type>pom</type></dependency></dependencies></dependencyManagement><dependencies>
<!-- <dependency>-->
<!-- <groupId>com.alipay.sofa</groupId>-->
<!-- <artifactId>healthcheck-sofa-boot-starter</artifactId>-->
<!-- </dependency>--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alipay.sofa</groupId><artifactId>rpc-sofa-boot-starter</artifactId></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>${nacos.version}</version></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-api</artifactId><version>${nacos.version}</version></dependency></dependencies>
配置文件
#配置nacos 的ip 端口 和命名空间
com.alipay.sofa.rpc.registry-address=nacos://ip:port/nacos-3fepopkd
编写服务接口和服务实现
public interface HelloService {String sayHello(String name);
}
import club.throwable.contract.HelloService;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import org.springframework.stereotype.Service;@Service
@SofaService(interfaceType = HelloService.class, bindings = {@SofaServiceBinding(bindingType = "bolt")
})
public class DefaultHelloService implements HelloService {@Overridepublic String sayHello(String name) {System.out.println(name + " -------------------------");return String.format("%s say hello!", name);}
}
启动类
@SpringBootApplication(scanBasePackages = {"club.throwable.server", "club.throwable.contract"})
public class ServerApplication {public static void main(String[] args) {SpringApplication.run(ServerApplication.class, args);}
}
client 实现
@SpringBootApplication(scanBasePackages = {"club.throwable.client", "club.throwable.contract"})
public class ClientApplication implements CommandLineRunner {@SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt"))private HelloService boltHelloService;public static void main(String[] args) {SpringApplication.run(ClientApplication.class, args);}@Overridepublic void run(String... args) throws Exception {System.out.println(boltHelloService.sayHello("zf-test"));}
}
如果你的nacos没有配置用户密码,那么以上代码就不用看下面的了
但是我的nacos是用用户名和密码的,导致nacos无法访问,后面定位出来是rpc源码的问题,
我试过了最新版本6.0.4 也无法解决这个问题,那我就自己改源码了
在项目路径下重写NacosRegistry 类第139行处,加入如下代码
nacosConfig.put(PropertyKeyConst.USERNAME, "your_user");nacosConfig.put(PropertyKeyConst.PASSWORD, "your_password");