Categories
Uncategorized

Spring Cloud Alibaba | Dubbo Spring Cloud of HTTP combat

An article on “Spring Cloud Alibaba | Dubbo and Spring Cloud the perfect combination,” we introduce the basic use of Dubbo Spring Cloud, Nacos use of the service center for the Spring Cloud Alibaba offered, providing interior Dubbo Dubbo-based RPC calls at the same time, Dubbo Spring Cloud after the integration of Spring Cloud, can provide direct HTTP interface seamlessly with Spring Cloud, directly supports remote call Feign, RestTemplate, etc., and at the same time provides HTTP services can provide Dubbo service. Dubbo Spring Cloud support HTTP remote call-level large facilitated our external docking system, without the need for secondary packaging Dubbo to do.

1. real case

Next, we pass a simple case to introduce Dubbo Spring Cloud annotated by the way is how to Dubbo while providing services and HTTP services.

1.1 Creating a parent project dubbo-spring-cloud-http

Project relies pom.xml as follows:

代码清单:Alibaba/dubbo-spring-cloud-http/pom.xml
***


    
        org.springframework.boot
        spring-boot-starter-actuator
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-discovery
    

    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

1.2 create a sub-project dubbo_provider_web, service providers

Project relies pom.xml as follows:

代码清单:Alibaba/dubbo-spring-cloud-http/dubbo_provider_web/pom.xml
***


    
    
        com.springcloud
        dubbo_api
        ${project.version}
    
    
    
        com.alibaba.cloud
        spring-cloud-starter-dubbo
    

Incorporated herein Dubbo Spring Cloud Dubbo API Toolkit and dependencies.

Profiles on a reference configuration, not repeat them here.

UserServiceI.java interface class is as follows:

代码清单:Alibaba/dubbo-spring-cloud-http/dubbo_provider_web/src/main/java/com/springcloud/dubbo_provider_web/service/UserServiceI.java
***

@Service(version = "1.0.0")
@RestController
@Slf4j
public class UserServiceI implements UserService {

    private Map usersRepository = Maps.newHashMap();

    @Override
    @PostMapping("/save")
    public UserModel save(@RequestBody UserModel user) {
        return usersRepository.put(user.getId(), user);
    }

    @Override
    @DeleteMapping("/remove")
    public void remove(@RequestParam("id") Long userId) {
        usersRepository.remove(userId);
    }

    @Override
    @GetMapping("/findAll")
    public Collection findAll() {
        return usersRepository.values();
    }
}

    @Service notes that many configuration attributes related services, defined here using the current version Interface version, the version here only take effect when the call Dubbo, HTTP call is invalid, more configuration can refer to the source org.apache.dubbo.config.annotation. Service.

1.3 create a sub-project spring_cloud_consumer, web service consumer

Project relies pom.xml as follows:

代码清单:Alibaba/dubbo-spring-cloud-http/spring_cloud_consumer/pom.xml
***


    
    
        com.springcloud
        dubbo_api
        ${project.version}
    
    
        org.springframework.cloud
        spring-cloud-starter-openfeign
    

Profile application.yml as follows:

代码清单:Alibaba/dubbo-spring-cloud-http/spring_cloud_consumer/src/main/resources/application.yml
***

server:
port: 8080
spring:
application:
    name: spring-cloud-consumer-server
cloud:
    nacos:
    discovery:
        server-addr: 192.168.44.129:8848

Interface test class UserController.java follows:

代码清单:Alibaba/dubbo-spring-cloud-http/spring_cloud_consumer/src/main/java/com/springcloud/spring_cloud_consumer/controller/UserController.java
***

@RestController
public class UserController {

    @Autowired
    UserRemote userRemote;

    @Autowired
    RestTemplate restTemplate;

    @PostMapping("/saveByFeign")
    public UserModel saveByFeign(@RequestBody UserModel user) {
        return userRemote.save(user);
    }

    @DeleteMapping("/removeByFeign")
    public void removeByFeign(@RequestParam("id") Long userId) {
        userRemote.remove(userId);
    }

    @GetMapping("/findAllByFeign")
    public Collection findAllByFeign() {
        return userRemote.findAll();
    }

    @PostMapping("/saveByRestTemplate")
    public UserModel saveByRestTemplate(@RequestBody UserModel user) {
        return restTemplate.postForObject("http://dubbo-spring-cloud-provider-web/save/", user, UserModel.class);
    }

    @DeleteMapping("/removeByRestTemplate")
    public void removeByRestTemplate(@RequestParam("id") Long userId) {
        restTemplate.delete("http://dubbo-spring-cloud-provider-web/remove?id=" + userId);
    }

    @GetMapping("/findAllByRestTemplate")
    public Collection findAllByRestTemplate() {
        return restTemplate.getForObject("http://dubbo-spring-cloud-provider-web/findAll/", Collection.class);
    }
}

A total of three test interface, here are two test ways, one is through Feign call, the other is through RestTemplate calls.

SpringCloudConsumerApplication.java as follows:

代码清单:Alibaba/dubbo-spring-cloud-http/spring_cloud_consumer/src/main/java/com/springcloud/spring_cloud_consumer/SpringCloudConsumerApplication.java
***

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class SpringCloudConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConsumerApplication.class, args);
    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

Use @EnableFeignClients open Feign function, RestTemplate inject Spring in the form of Bean.

1.4 dubbo_consumer create a sub-project as a service consumer Dubbo

Interface test class UserController.java follows:

代码清单:Alibaba/dubbo-spring-cloud-http/dubbo_consumer/src/main/java/com/springcloud/dubbo_consumer/controller/UserController.java
***

@RestController
public class UserController {

    @Reference(version = "1.0.0")
    UserService userService;

    @PostMapping("/save")
    public UserModel save(@RequestBody UserModel user) {
        return userService.save(user);
    }

    @DeleteMapping("/remove")
    public void remove(@RequestParam("id") Long userId) {
        userService.remove(userId);
    }

    @GetMapping("/findAll")
    public Collection findAll() {
        return userService.findAll();
    }
}

Here @Reference comment to be specified call the service provider version number of the interface, if you do not specify a version number, will not call interface to our previous service provider.

2. Test

We use the test tool PostMan testing, sequential start three sub-projects provider_web, spring_cloud_consumer and dubbo_consumer, first tested components Feign access to use PostMan: http: // localhost: 8080 / saveByFeign sending a POST request, as shown:

Test link: http: // localhost: 8080 / findAllByFeign, as shown:

Test RestTemplate access, as shown:

So far, spring_cloud_consumer test is successful, the following continue to test dubbo_consumer, as shown:

3. Sample Code

Sample Code -Github

Sample Code -Gitee

4. Reference

Dubbo Spring Cloud official documents

Leave a Reply