sofastack / sofa-rpc

SOFARPC is a high-performance, high-extensibility, production-level Java RPC framework.
https://www.sofastack.tech/sofa-rpc/docs/Home
Apache License 2.0
3.81k stars 1.17k forks source link

Consul注册无法使用的问题 #687

Closed wangyjx closed 5 years ago

wangyjx commented 5 years ago

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:

             <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>rpc-sofa-boot-starter</artifactId>
    </dependency>

父级项目中: `

com.alipay.sofa sofaboot-dependencies 3.1.5

`

Your advice

Environment

ScienJus commented 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>
wangyjx commented 5 years ago

@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}}}

看起来不生效

ScienJus commented 5 years ago

@wangyjx

首先是 spring.application.namespring.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}")))
wangyjx commented 5 years ago

@ScienJus 非常感谢解答,我看见5.6刚发布了,稍后试试5.6

ScienJus commented 5 years ago

@wangyjx 我编写了一个基于 5.6.0 版本 consul registry 的 demo,希望能对你有帮助。

https://github.com/ScienJus/sofa-rpc-consul-registry-demo

wangyjx commented 5 years ago

已经使用了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
    }
}

@ScienJus 不知道怎么做了,请指点