Merge pull request #554 from mercyblitz/master

Polish spring-cloud-incubator/spring-cloud-alibaba#553 : Dubbo Spring…
pull/555/head
Mercy Ma 6 years ago committed by GitHub
commit fb8ebf99da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.cloud.alibaba.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;
@ -77,6 +78,10 @@ public class DubboServiceRegistrationNonWebApplicationAutoConfiguration {
@EventListener(ServiceBeanExportedEvent.class)
public void onServiceBeanExported(ServiceBeanExportedEvent event) {
setWebPort(event.getServiceBean());
}
@EventListener(ApplicationStartedEvent.class)
public void onApplicationStarted() {
register();
}

@ -26,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;
@ -53,6 +54,9 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
@EnableFeignClients
public class DubboSpringCloudConsumerBootstrap {
@Reference
private UserService userService;
@Reference(version = "1.0.0", protocol = "dubbo")
private RestService restService;
@ -119,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

@ -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();
}
}

@ -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();
}
}

@ -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…
Cancel
Save