Closed wangyjx closed 5 years ago
@wangyjx 请问您使用的是哪个版本的 sofa-rpc 呢?
如果是 5.5.x,可以尝试加入以下依赖:
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
<version>1.3.0</version>
</dependency>
如果是 5.6.x,也同样加入 1.4.2 版本的依赖:
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
<version>1.4.2</version>
</dependency>
@ScienJus 多谢 我的是5.5.5的RPC,加入上述依赖后不报错了,算是解决了初步的问题。
不过,consul上显示的service名很奇怪,叫做: consul-default ID也很奇怪 叫做 192.168.2.122:12200-null-50505
看来是没有正确传递spring.application.name 或者说还有其他设置需要做才可以正确展示service 吗?
我在application.properties中设置了
spring.application.name=xxxx spring.cloud.consul.discovery.health-check-path=/ spring.cloud.consul.discovery.health-check-interval=10s spring.cloud.consul.discovery.instance-id=${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
看起来不生效
@wangyjx
首先是 spring.application.name
和 spring.cloud.consul.*
等配置是在 spring-cloud-consul-discovery 以及其周边组件中所使用的,sofa-rpc 的 consul-registry 并不会读取该配置以及注册对应的信息。
设想一下,sofa-rpc 的注册是以 interface 为粒度的,一个应用可以有多个 interface,难道他们注册时都是同一个 app name 么?这是不合理的。
其次,我看你的配置是希望配置 consul 的 service name 和自定义 http health check,这个在 5.5.x 是没有提供相关配置项的,不过在 5.6.0 版本之后,可以通过一系列配置项进行配置,稍后我尝试提供一个 demo,其大概是:
配置 HTTP 探活:
com.alipay.sofa.rpc.registry.address=consul://127.0.0.1:8500?healthCheck.type=http&healthCheck.path=/&healthCheck.interval=10s
配置服务别名(默认是 interfaceId):
// xml 方式
<sofa:service ref="helloSyncServiceImpl" interface="com.alipay.sofa.rpc.samples.invoke.HelloSyncService">
<sofa:binding.bolt/>
</sofa:service>
<sofa:reference id="helloSyncServiceReference" interface="com.alipay.sofa.rpc.samples.invoke.HelloSyncService">
<sofa:binding.bolt>
<sofa:parameter key="consulServiceName" value="${spring.application.name}"/>
</sofa:binding.bolt>
</sofa:reference>
// 注解方式
@SofaReference(
binding = @SofaReferenceBinding(
bindingType = "bolt",
parameters = @SofaParameter(key = "consulServiceName", value = "${spring.application.name}")))
@SofaService(
bindings = @SofaServiceBinding(
bindingType = "bolt",
parameters = @SofaParameter(key = "consulServiceName", value = "${spring.application.name}")))
@ScienJus 非常感谢解答,我看见5.6刚发布了,稍后试试5.6
@wangyjx 我编写了一个基于 5.6.0 版本 consul registry 的 demo,希望能对你有帮助。
已经使用了5.6,加上这个demo本身运行都是没有问题的。 不过有个问题,还请指点。
rpc服务注册是在一个项目,称为项目 A,使用consul注册完全没有问题(和demo一样是注解方式)。
spring.application.name=imp.user.rpc com.alipay.sofa.rpc.bolt.port=12200 com.alipay.sofa.rpc.registry.address=consul://127.0.0.1:8500
服务消费在另外一个项目,项目B,这个项目是sofaboot 3.1.5的项目,加入了 sofarpc 5.6的引用
在application.properties里面加了 com.alipay.sofa.rpc.registry.address=consul://127.0.0.1:8500
@SpringBootApplication
@EnableDiscoveryClient
public class UserClientApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(UserClientApplication.class);
springApplication.run(args);
}
}
----- controller -----
@RestController
@Slf4j
public class UserController {
@SofaReference(
binding = @SofaReferenceBinding(
bindingType = "bolt",
parameters = @SofaParameter(key = ConsulConstants.CONSUL_SERVICE_NAME_KEY, value = "imp.user.rpc-user-service") //这个名字是从consul UI上复制下来的
),
jvmFirst = false
)
private UserService userService;
@RequestMapping("/")
public String home() {
return "Hello world";
}
@RequestMapping("/test")
public String test() {
return userService.getUser(1L).getData().getUserId().toString(); //userService一直是null
}
}
然后又试了一下RegistryConfig 和ConsumerConfig, 结果ProviderConfig不支持 bolt协议, 直接报错了
RegistryConfig registryConfig = new RegistryConfig() .setProtocol("bolt") .setAddress("127.0.0.1:12200");
ConsumerConfig<UserService> consumerConfig = new ConsumerConfig<UserService>()
.setInterfaceId(UserService.class.getName())
.setRegistry(registryConfig)
.setProtocol("bolt")
.setConnectTimeout(10 * 1000);
userService = consumerConfig.refer();
@ScienJus 不知道怎么做了,请指点
Your question
如何正确的在sofarpc启用consul注册
Your scenes
在测试sofa-rpc使用consul注册的功能,不在application.properties中加com.alipay.sofa.rpc.registry.address=consul://127.0.0.1:8500 的时候,一切正常。 一旦加上启动就报错 java.lang.NoClassDefFoundError: com/ecwid/consul/v1/ConsulClient
当前项目的maven里面相关dependency:
父级项目中: `
`
Your advice
Environment
java -version
): JDK11.02uname -a
): MacOS 10.14.1