mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
sync dubbo example in finchley
This commit is contained in:
parent
7a7cc68c38
commit
eef0e92dd3
@ -95,87 +95,69 @@
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<!-- Spring Cloud Nacos Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<profiles>
|
||||
<!-- Spring Cloud Eureka Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Nacos -->
|
||||
<profile>
|
||||
<id>nacos</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<dependencies>
|
||||
<!-- Nacos Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>eureka</id>
|
||||
<dependencies>
|
||||
<!-- Eureka Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
|
||||
<!-- Zookeeper -->
|
||||
<profile>
|
||||
<id>zookeeper</id>
|
||||
<dependencies>
|
||||
<!-- Zookeeper Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
|
||||
<version>${spring-cloud-zookeeper.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<!-- Spring Cloud Zookeeper Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
|
||||
<version>${spring-cloud-zookeeper.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
<version>3.4.12</version>
|
||||
<optional>true</optional>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.curator</groupId>
|
||||
<artifactId>curator-framework</artifactId>
|
||||
<version>${curator.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
<dependency>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
<version>3.4.12</version>
|
||||
<optional>true</optional>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<profile>
|
||||
<id>consul</id>
|
||||
<dependencies>
|
||||
<!-- Spring Cloud Consul -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
|
||||
<version>${spring-cloud-consul.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
<dependency>
|
||||
<groupId>org.apache.curator</groupId>
|
||||
<artifactId>curator-framework</artifactId>
|
||||
<version>${curator.version}</version>
|
||||
</dependency>
|
||||
|
||||
</profiles>
|
||||
<!-- Spring Cloud Consul Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
|
||||
<version>${spring-cloud-consul.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
@ -21,6 +21,18 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Open Feign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Retry -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.retry</groupId>
|
||||
<artifactId>spring-retry</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sample API -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
@ -28,11 +40,14 @@
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Open Feign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -17,6 +17,7 @@
|
||||
package org.springframework.cloud.alibaba.dubbo.bootstrap;
|
||||
|
||||
import org.apache.dubbo.config.annotation.Reference;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
@ -25,6 +26,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.alibaba.dubbo.annotation.DubboTransported;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.RestService;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.User;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.UserService;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
@ -52,7 +54,10 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
|
||||
@EnableFeignClients
|
||||
public class DubboSpringCloudConsumerBootstrap {
|
||||
|
||||
@Reference(version = "1.0.0")
|
||||
@Reference
|
||||
private UserService userService;
|
||||
|
||||
@Reference(version = "1.0.0", protocol = "dubbo")
|
||||
private RestService restService;
|
||||
|
||||
@Autowired
|
||||
@ -94,7 +99,7 @@ public class DubboSpringCloudConsumerBootstrap {
|
||||
}
|
||||
|
||||
@FeignClient("${provider.application.name}")
|
||||
@DubboTransported()
|
||||
@DubboTransported(protocol = "dubbo")
|
||||
public interface DubboFeignRestService {
|
||||
|
||||
@GetMapping(value = "/param")
|
||||
@ -118,7 +123,28 @@ public class DubboSpringCloudConsumerBootstrap {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner paramRunner() {
|
||||
public ApplicationRunner userServiceRunner() {
|
||||
return arguments -> {
|
||||
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setName("小马哥");
|
||||
user.setAge(33);
|
||||
|
||||
// save User
|
||||
System.out.printf("UserService.save(%s) : %s\n", user, userService.save(user));
|
||||
|
||||
// find all Users
|
||||
System.out.printf("UserService.findAll() : %s\n", user, userService.findAll());
|
||||
|
||||
// remove User
|
||||
System.out.printf("UserService.remove(%d) : %s\n", user.getId(), userService.remove(user.getId()));
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner callRunner() {
|
||||
return arguments -> {
|
||||
|
||||
// To call /path-variables
|
||||
@ -191,7 +217,7 @@ public class DubboSpringCloudConsumerBootstrap {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.requestBodyMap(data, "Hello,World"));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
// System.out.println(dubboFeignRestService.requestBody("Hello,World", data));
|
||||
System.out.println(dubboFeignRestService.requestBody("Hello,World", data));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.requestBody("Hello,World", data));
|
||||
|
||||
|
@ -1,12 +1,18 @@
|
||||
dubbo:
|
||||
registry:
|
||||
# The Spring Cloud Dubbo's registry extension
|
||||
## the default value of dubbo-provider-services is "*", that means to subscribe all providers,
|
||||
## thus it's optimized if subscriber specifies the required providers.
|
||||
address: spring-cloud://localhost
|
||||
# The traditional Dubbo's registry
|
||||
# The traditional Dubbo's registry also is supported
|
||||
# address: zookeeper://127.0.0.1:2181
|
||||
cloud:
|
||||
# The subscribed services in consumer side
|
||||
subscribed-services: ${provider.application.name}
|
||||
|
||||
server:
|
||||
port: 0
|
||||
|
||||
provider:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-web-provider
|
||||
name: spring-cloud-alibaba-dubbo-provider
|
@ -17,6 +17,13 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Resolve the Dubbo REST RPC issue -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Resolve the Spring Cloud registration issue -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
@ -73,4 +80,13 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -16,6 +16,7 @@
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.bootstrap;
|
||||
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
@ -30,6 +31,7 @@ public class DubboSpringCloudProviderBootstrap {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DubboSpringCloudProviderBootstrap.class)
|
||||
.properties("spring.profiles.active=nacos")
|
||||
.web(WebApplicationType.NONE)
|
||||
.run(args);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.service;
|
||||
|
||||
import org.apache.dubbo.config.annotation.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* In-Memory {@link UserService} implementation
|
||||
*/
|
||||
@Service(protocol = "dubbo")
|
||||
public class InMemoryUserService implements UserService {
|
||||
|
||||
private Map<Long, User> usersRepository = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean save(User user) {
|
||||
return usersRepository.put(user.getId(), user) == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Long userId) {
|
||||
return usersRepository.remove(userId) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<User> findAll() {
|
||||
return usersRepository.values();
|
||||
}
|
||||
}
|
@ -16,6 +16,12 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Production Ready features -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
@ -30,4 +36,13 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.service;
|
||||
|
||||
import org.apache.dubbo.config.annotation.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* In-Memory {@link UserService} implementation
|
||||
*/
|
||||
@Service(protocol = "dubbo")
|
||||
public class InMemoryUserService implements UserService {
|
||||
|
||||
private Map<Long, User> usersRepository = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean save(User user) {
|
||||
return usersRepository.put(user.getId(), user) == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Long userId) {
|
||||
return usersRepository.remove(userId) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<User> findAll() {
|
||||
return usersRepository.values();
|
||||
}
|
||||
}
|
@ -16,4 +16,10 @@ feign:
|
||||
enabled: true
|
||||
|
||||
server:
|
||||
port: 8080
|
||||
port: 8888
|
||||
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: dubborestmetadata
|
@ -1,6 +1,6 @@
|
||||
spring:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-web-provider
|
||||
name: spring-cloud-alibaba-dubbo-provider
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.service;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* {@link User} Service
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
boolean save(User user);
|
||||
|
||||
boolean remove(Long userId);
|
||||
|
||||
Collection<User> findAll();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user