Spring cloud Alibaba 3. Create a microservice project with openFeign client (ribbon by default) + Nacos registry

Previous: project structure and description

In this paper, we build a real project environment of spring cloud Alibaba. Based on the micro service project of nacos, we use openFeign client and the default Ribbon of openFeign to achieve load balancing

1. Environment use of this article

Nacos: Registry
openFeign: rpc calling tool (Ribbon is integrated by default)

Nacos environment construction reference: https://blog.csdn.net/qq_41463655/article/details/104002968

2. Spring cloud Alibaba technology stack at a glance

Spring Cloud Alibaba Nacos service registration
Spring Cloud Alibaba Nacos distributed configuration center
Spring Cloud Alibaba Sentinel service protection
Spring cloud Alibaba Seata distributed transaction solution framework
Alibaba Cloud OSS Alibaba cloud storage
Alibaba Cloud SchedulerX distributed task scheduling platform
Alibaba Cloud SMS distributed SMS system

3. Current project structure

spring-cloud-alibaba-demo
------alibaba-api
------------alibaba-server-api
------------alibaba-client-api
------alibaba-api-impl
------------alibaba-client-api-impl
------------alibaba-server-api-impl


Code implementation

1, Spring cloud Alibaba demo (parent POM)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-cloud-alibaba-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-alibaba-demo</name>
    <description>Demo project for Spring Boot</description>
    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <modules>
        <module>alibaba-api</module>
        <module>alibaba-api/alibaba-client-api</module>
        <module>alibaba-api/alibaba-server-api</module>
        <module>alibaba-api-impl</module>
        <module>alibaba-api-impl/alibaba-client-api-impl</module>
        <module>alibaba-api-impl/alibaba-server-api-impl</module>
    </modules>

    <dependencies>
        <!--  springboot integration web assembly-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- nacos Registry Center -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
        <!-- openfeign Client side ( rpc) -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <!-- -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2, Alibaba API (interface parent project)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-alibaba-demo</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>alibaba-api</artifactId>
    <packaging>pom</packaging>
</project>

3, Alibaba API impl (interface implementation parent project)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-alibaba-demo</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>alibaba-api-impl</artifactId>
    <packaging>pom</packaging>

</project>

4, Alibaba server API (interface sub project 1-producer)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>alibaba-api</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>alibaba-server-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>alibaba-server-api</name>
    <description>Demo project for Spring Boot</description>
</project>

Interface UserService

public interface UserService {

    /**
     * TODO  Query user information
     */
    @GetMapping("/getUserId")
    public String getUserId(@RequestParam("userId") String userId);
}

5, Alibaba client API (interface sub project 2-consumer)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>alibaba-api</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>alibaba-client-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>alibaba-client-api</name>
    <description>Demo project for Spring Boot</description>
</project>

Interface LoginService

package com.example.alibabaclient.server;

import org.springframework.web.bind.annotation.GetMapping;

public interface LoginService {

    /**
     * TODO  Sign in
     */
    @GetMapping("/login")
    public String login();

}

6, Alibaba server API impl (implement sub project producer)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>alibaba-api-impl</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>alibaba-server-api-impl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>alibaba-server-api-impl</name>
    <description>Demo project for Spring Boot</description>

   <dependencies>
       <dependency>
           <groupId>com.example</groupId>
           <artifactId>alibaba-server-api</artifactId>
           <version>0.0.1-SNAPSHOT</version>
       </dependency>
   </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

server:
  port: 8082
spring:
  application:
    ## Service name
    name: alibaba-server
  cloud:
    nacos:
      discovery:
        ## Service registration address
        server-addr: 192.168.177.128:8848

Implementation interface

package com.example.alibabaserver.service;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
// Mediatype.application? json? Utf8? Value means to return the json string
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserServiceImpl  implements   UserService{

    @Value("${server.port}")
    private String port;

    public String getUserId(@RequestParam("userId") String userId){
         return  "userId:"+userId+ "|| port:"+port;
    }
}

Startup class

@SpringBootApplication
class AlibabaServerApplication {

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

}

7, Alibaba client API impl

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>alibaba-api-impl</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>alibaba-client-api-impl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>alibaba-client-api-impl</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>alibaba-server-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>alibaba-client-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

server:
  port: 8081
spring:
  application:
    ## Service name
    name: alibaba-client
  cloud:
    nacos:
      discovery:
        ## Service registration address
        server-addr: 192.168.177.128:8848

openFeign customer service interface

package com.example.alibabaclient.feign;

import com.example.alibabaserver.service.UserService;
import org.springframework.cloud.openfeign.FeignClient;
/**
 * TODO  alibaba-server = service name
 *  Directly depend on the interface project, reduce the code amount, and make the implementation class directly available
 * @return
 */
@FeignClient("alibaba-server")
public interface UserServiceFeign extends UserService {


}

Implementation interface

package com.example.alibabaclient.server;

import com.example.alibabaclient.feign.UserServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginServiceImpl implements LoginService {

    @Autowired
   private UserServiceFeign userServiceFeign;

    @Override
    public String login() {
        String result = userServiceFeign.getUserId("123456789");
        System.out.println("call alibaba-server service,result:" + result);
        return result;
    }
}

Startup class

Add @ EnableFeignClients annotation

package com.example.alibabaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
class AlibabaClientApplication {

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

Eight, test

Start Alibaba server API impl twice, cluster (modify port number for the second start)
Start Alibaba client API impl

Access the login interface of Alibaba client API impl, and use openFeign to access successfully

183 original articles published, praised 23, visited 60000+
Private letter follow

Tags: Maven Spring Apache xml

Posted on Sat, 18 Jan 2020 06:37:36 -0800 by Tyche