dubbo configuration use

The project built by dubbo this time, I divided it into three project frameworks

interfaced all projects that expose interfaces on the server side

provider service provider

consumer service user

 

First, build three maven projects through eclipse, and change the compilation environment of their projects and some projects to web projects and adjust them

The structure is as follows



 

In the pom file of the interfaced project, we only need to simply configure the unit test dependencies to be dependent.

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

</dependencies>

 

However, the push configuration of the private server must be added, because when the other two projects depend on it, the jar package needs to be downloaded in the private server environment.

<distributionManagement>

<repository>

<id>interfaced-release</id>

<name>interfaced Project Release</name>

<url>http://192.168.1.222:8081/nexus/content/repositories/releases/</url>

</repository>

 

<snapshotRepository>

<id>interfaced-snapshots</id>

<name>interfaced Project SNAPSHOTS</name>

<url>http://192.168.1.222:8081/nexus/content/repositories/snapshots/</url>

</snapshotRepository>

</distributionManagement>

By the way, the configuration of the setting file

<servers>

<server>

<id>interfaced-release</id>

<username>admin</username>

<password>admin123</password>

</server>

 

<server>

<id>interfaced-snapshots</id>

<username>admin</username>

<password>admin123</password>

</server>

  </servers>

Note that the id attribute value in the setting file must be consistent with the id value in the project pom file, otherwise it will not be published.

 

Then we add a simple userservice interface class in the interfaced project and provide a simple login method

public interface IUserService {

 

/**

* testing method

* @year 2017-10-16

* @author guiwenqing

* @param username

* @param password

* @return   

* @return boolean

*

*/

public boolean login(String username,String password);

}

In this way, the interfaced project is configured

 

Configure the pom file of the provider project

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<junit.version>3.8.1</junit.version>

<springframework.version>4.1.6.RELEASE</springframework.version>

<commonsLogging.version>1.2</commonsLogging.version>

</properties>

 

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

 

<dependency>

<groupId>com.101tec</groupId>

<artifactId>zkclient</artifactId>

<version>0.10</version>

</dependency>

<!-- **************************** Spring depends on **************** ************ -->

<!-- Add Spring-core package-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${springframework.version}</version>

</dependency>

<!-- Add spring-tx package-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${springframework.version}</version>

</dependency>

<!-- Add spring-jdbc package-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${springframework.version}</version>

</dependency>

<!--Add spring-web package-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${springframework.version}</version>

</dependency>

<!-- Add spring-context package-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${springframework.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${springframework.version}</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>${commonsLogging.version}</version>

</dependency>

<!--Add aspectjweaver package-->

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.8.5</version>

</dependency>

<!-- **************************** /Spring Depends *************** ************* -->

 

<!-- **************************** Dubbo depends on **************** ************ -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>dubbo</artifactId>

<version>2.5.3</version>

<exclusions>

<exclusion>

<groupId>org.springframework</groupId>

<artifactId>spring</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

<version>3.3.3</version>

<exclusions>

<exclusion>

<groupId>com.sun.jmx</groupId>

<artifactId>jmxri</artifactId>

</exclusion>

<exclusion>

<groupId>com.sun.jdmk</groupId>

<artifactId>jmxtools</artifactId>

</exclusion>

<exclusion>

<groupId>javax.jms</groupId>

<artifactId>jms</artifactId>

</exclusion>

</exclusions>

</dependency>

 

                 <!-- Directly introduced interfaced dependencies -->

<dependency>

<groupId>com.dubbo.interfaced</groupId>

<artifactId>interfaced</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

 

</dependencies>

 

Create the spring master control configuration file spring-all.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- **************************** Annotation Scan **************** ************ -->

<context:component-scan base-package="com.user.service.impl"/>

<!-- ****************************/Annotation Scan **************** ************* -->

<!-- **************************** Import other XML files************** **************** -->

<import resource="spring-provider.xml"/>

<!-- ****************************/Import other XML files ************* **************** -->

</beans>

Create dubbo service provider configuration file spring-provider.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo

        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

 

    <!-- application name-->

    <dubbo:application name="dubbodemo-provider"/>

    <!-- Which registry to connect to -->

    <dubbo:registry id="dubbodemo" address="zookeeper://192.168.1.222:2181"/>

    <!-- Expose services on port 20880 using the dubbo protocol -->

    <dubbo:protocol name="dubbo" port="28080"/>

    <!-- Declare the service interface that needs to be exposed -->

    <dubbo:service registry="dubbodemo" timeout="3000" interface="com.user.service.IUserService" ref="userServiceImpl"/>

</beans>

Notice

application is the application name

Registry is registered to the registry with id as the name

protocol exposes what port to the user by what protocol

service exposes which interface in which service, and the reference of ref must be noted. The service class below me uses the default class name as the bean name, don't make a mistake

 

Implement the userservice interface class just now

@Service

public class UserServiceImpl implements IUserService {

 

private Logger logger = Logger.getLogger(UserServiceImpl.class);

 

public boolean login(String username, String password) {

logger.info("Submit information" + username + password);

if (username != null && password != null && username.equals(password)) {

return true;

}

return false;

}

 

}

 

Because of the dubbo service project, the official support is opened. Here, the spring container is used directly to start, and the ClassPathXmlApplicationContext class is used to load spring start.

Build the base startup class

public class BeanFactoryUtil {

 

private static ApplicationContext ctx_producer = null;

 

public final static String ApplicationContextRoot = "";

public final static String ApplicationContextPath = ApplicationContextRoot + "spring-all.xml";

 

public static void init() {

if (ctx_producer == null) {

synchronized (BeanFactoryUtil.class) {

if (ctx_producer == null) {

String[] configLocations = new String[] { ApplicationContextPath };

ctx_producer = new ClassPathXmlApplicationContext(configLocations);

}

}

}

}

}

Create a startup test class

public class Launcher {

 

private static Log logger = LogFactory.getLog(Launcher.class);

 

public static void main(String[] args) {

// initialize spring

logger.info("Start initializing core service");

BeanFactoryUtil.init();

}

 

}

Run this class directly with the main method to start the service provider project of dubbo, and the service provider is configured.

 

Configure the consumer project pom file

<!-- **************************** Properties Configuration **************** ************ -->

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<junit.version>3.8.1</junit.version>

<springframework.version>4.1.6.RELEASE</springframework.version>

<commonsLogging.version>1.2</commonsLogging.version>

</properties>

 

<!-- **************************** /Properties configuration *************** ************* -->

 

<dependencies>

<!-- **************************** Spring depends on **************** ************ -->

<!-- Add Spring-core package-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${springframework.version}</version>

</dependency>

<!-- Add spring-tx package-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${springframework.version}</version>

</dependency>

<!--Add spring-web package-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${springframework.version}</version>

</dependency>

<!-- Add spring-context package-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${springframework.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${springframework.version}</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>${commonsLogging.version}</version>

</dependency>

<!--Add aspectjweaver package-->

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.8.5</version>

</dependency>

<!-- **************************** /Spring Depends *************** ************* -->

 

<!-- **************************** Dubbo depends on **************** ************ -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>dubbo</artifactId>

<version>2.5.3</version>

<exclusions>

<exclusion>

<groupId>org.springframework</groupId>

<artifactId>spring</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

<version>3.3.3</version>

<exclusions>

<exclusion>

<groupId>com.sun.jmx</groupId>

<artifactId>jmxri</artifactId>

</exclusion>

<exclusion>

<groupId>com.sun.jdmk</groupId>

<artifactId>jmxtools</artifactId>

</exclusion>

<exclusion>

<groupId>javax.jms</groupId>

<artifactId>jms</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>com.github.sgroschupf</groupId>

<artifactId>zkclient</artifactId>

<version>0.1</version>

</dependency>

<!-- **************************** /Dubbo depends on *************** ************* -->

 

<!-- **************************** interface depends on **************** ************ -->

<dependency>

<groupId>com.dubbo.interfaced</groupId>

<artifactId>interfaced</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

<!-- **************************** /interface depends on *************** ************* -->

</dependencies>

 

Continue to configure the spring master control file applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">

    <!-- **************************** Import other XML files************** **************** -->

    <import resource="spring-consumer.xml"/>

    <!-- ****************************/Import other XML files ************* **************** -->

</beans>

Configure the spring configuration file spring-consumer.xml for dubbo consumers

<?xml version="1.0" encoding="UTF-8"?>

<!-- Add DUBBO SCHEMA -->

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo

        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

 

    <!-- application name-->

    <dubbo:application name="dubbodemo-consumer"/>

    <!-- Which registry to connect to (connect to the 2181 port zookeeper of this machine) -->

    <dubbo:registry address="zookeeper://192.168.1.222:2181"/>

    <!-- What protocol does the consumer use to obtain the service (use the dubbo protocol to expose the service on port 20880) -->

    <dubbo:protocol port="28080"/>

    <!-- Which interfaces are provided for consumers to call -->

    <dubbo:reference id="userService" interface="com.user.service.IUserService"/>

</beans>

application service name

which registry the registry links to

protocol which protocol and port to use for service acquisition

Which interfaces are used by reference

 

Use spring container features to create test classes

public class UserServiceConsumer {

 

private static Logger logger = Logger.getLogger(UserServiceConsumer.class);

 

public static void main(String args[]) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

IUserService userService = (IUserService) ctx.getBean("userService");

logger.info("Execution result: " + userService.login("test", "test"));

}

}

 

Simple dubbo service consumption configuration completed

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326196445&siteId=291194637