mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
This commit is contained in:
@@ -16,14 +16,23 @@
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.bootstrap;
|
||||
|
||||
import com.alibaba.dubbo.config.ApplicationConfig;
|
||||
import com.alibaba.dubbo.config.RegistryConfig;
|
||||
import com.alibaba.dubbo.config.annotation.Reference;
|
||||
import com.alibaba.dubbo.config.spring.ReferenceBean;
|
||||
import com.alibaba.dubbo.config.spring.ServiceBean;
|
||||
import com.alibaba.dubbo.config.spring.context.event.ServiceBeanExportedEvent;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.EchoService;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.event.EventListener;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Dubbo Spring Cloud Bootstrap
|
||||
@@ -35,6 +44,9 @@ public class DubboSpringCloudBootstrap {
|
||||
@Reference(version = "1.0.0")
|
||||
private EchoService echoService;
|
||||
|
||||
@Reference(version = "1.0.0")
|
||||
private EchoService echoServiceForRest;
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner applicationRunner() {
|
||||
return arguments -> {
|
||||
@@ -42,6 +54,28 @@ public class DubboSpringCloudBootstrap {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Autowired
|
||||
private ApplicationConfig applicationConfig;
|
||||
|
||||
@Autowired
|
||||
private List<RegistryConfig> registries;
|
||||
|
||||
@EventListener(ServiceBeanExportedEvent.class)
|
||||
public void onServiceBeanExportedEvent(ServiceBeanExportedEvent event) {
|
||||
ServiceBean serviceBean = event.getServiceBean();
|
||||
ReferenceBean referenceBean = new ReferenceBean();
|
||||
referenceBean.setApplication(applicationConfig);
|
||||
referenceBean.setRegistries(registries);
|
||||
referenceBean.setInterface(serviceBean.getInterfaceClass());
|
||||
referenceBean.setInterface(serviceBean.getInterface());
|
||||
referenceBean.setVersion(serviceBean.getVersion());
|
||||
referenceBean.setGroup(serviceBean.getGroup());
|
||||
Object object = referenceBean.get();
|
||||
System.out.println(object);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DubboSpringCloudBootstrap.class)
|
||||
.run(args);
|
||||
|
@@ -17,23 +17,48 @@
|
||||
package org.springframework.cloud.alibaba.dubbo.service;
|
||||
|
||||
import com.alibaba.dubbo.config.annotation.Service;
|
||||
import com.alibaba.dubbo.rpc.RpcContext;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
|
||||
/**
|
||||
* Default {@link EchoService}
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@Service(version = "1.0.0")
|
||||
@Service(version = "1.0.0", protocol = {"dubbo", "rest"})
|
||||
@RestController
|
||||
@Path("/")
|
||||
public class DefaultEchoService implements EchoService {
|
||||
|
||||
@Override
|
||||
@GetMapping("/echo")
|
||||
public String echo(@RequestParam String message) {
|
||||
return "[echo] : " + message;
|
||||
@GetMapping(value = "/echo",
|
||||
consumes = MediaType.APPLICATION_JSON_VALUE,
|
||||
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@Path("/echo")
|
||||
@GET
|
||||
@Consumes("application/json")
|
||||
@Produces("application/json;charset=UTF-8")
|
||||
public String echo(@RequestParam @QueryParam("message") String message) {
|
||||
return RpcContext.getContext().getUrl() + " [echo] : " + message;
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostMapping("/plus")
|
||||
@Path("/plus")
|
||||
@POST
|
||||
public String plus(@RequestParam @QueryParam("a") int a, @RequestParam @QueryParam("b") int b) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -24,4 +24,7 @@ package org.springframework.cloud.alibaba.dubbo.service;
|
||||
public interface EchoService {
|
||||
|
||||
String echo(String message);
|
||||
|
||||
String plus(int a, int b);
|
||||
|
||||
}
|
||||
|
@@ -1,9 +1,13 @@
|
||||
|
||||
|
||||
dubbo:
|
||||
scan:
|
||||
base-packages: org.springframework.cloud.alibaba.dubbo.service
|
||||
protocol:
|
||||
port: 12345
|
||||
protocols:
|
||||
dubbo:
|
||||
name: dubbo
|
||||
port: 12345
|
||||
rest:
|
||||
name: rest
|
||||
port: 9090
|
||||
server: netty
|
||||
registry:
|
||||
address: spring-cloud://dummy
|
Reference in New Issue
Block a user