一创建项目 1.1创建->项目->Maven->maven-archetype-webapp->Next-> GroupId->com.fmjava ArtifactId->dubbo_service 1.2 补齐目录 main->Directory->java main->Directory->resources 1.3做标记 java->Mark Directory as->Sources Root resources->Mark Directory as-> Resources Root 1.4做依赖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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>war</packaging> <name>dubbo_service</name> <groupId>com.fmjava</groupId> <artifactId>dubbo_service</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.2.3.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- dubbo相关 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.11.0.GA</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.32.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> </dependency> </dependencies> <build> <finalName>dubbo_service</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project> 1.5 配置web.xml 因为我们是web应用,需要我们配置web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> 1.6 applicationContext-service.xml 因为web.xml文件中引入配置文件,所以在 src->main->resources中创建配置文件applicationContext-service.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 给当前项目服务起个名字 --> <dubbo:application name="dubboxdemo-service"/> <!-- 配置连接zookeeper的IP和端口 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 配置包扫描, 在这个包下面的实现类中使用@Service注解才会生效 --> <dubbo:annotation package="com.fmjava.service"/> </beans> 1.7接口和实现 因为配置包扫描, 在这个包下面的实现类中使用了@Service注解才会生效--> 所以需要创建src->main->java->创建包com.fmjava.service->之后再创建接口文件Kind:Interface Name:TestDemo TestDemo.java定义端口如下 接口实现文件TestDemoImpl文件如下 package com.fmjava.service; public class TestDemoImpl implements TestDemo{ @Override public String getName(){ return "fmjava dubbo"; } } 1.8加入注解 目前工程代码已经完成,如果给被人用需要加入注解,别人才可以调用 即:service实现类上写@service注解(写此注解的时候,注意先包,选的是阿里dubbo下的包) 即1.7中TestDemoImpl.java中代码修改为如下内容 package com.fmjava.service; import org.apache.dubbo.config.annotation.Service; @Service public class TestDemoImpl implements TestDemo{ @Override public String getName(){ return "fmjava dubbo"; } } 1.9部署 IDEA->Add Configuration->Tomcat Server->local->Name(dubbo_service) Deployment->+号->dubbo_service:war exploded ->Application context: / 路径为/ ->完成 上面端口8080如果冲突可以改为8081 二创建客户端 File->new project->Maven->Create from archetype->org.apache.maven.archetypes:maven-archetype-webapp ->GroupId : com.fmjava ArtifactId : dubbo_consumer 完成 java/resources目录创建,并且标记 1.2 配置pom.xml依赖 1.3 web.xml文件 1.4 applicationContext-web.xml 配置和中文乱码问题 1.5 com.fmjava.controller创建package 1.6应用 TestController.java内容如下 package com.fmjava.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.fmjava.service.TestInterface; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Reference TestInterface testInterface; @RequestMapping("/getname") public String getName(){ String name = testInterface.getName(); return name; } } com.fmjava->service->TestInterface package com.fmjava.service; public interface TestInterface { public String getName(); }
Dubbo第一个程序
最新推荐文章于 2020-05-15 11:35:59 发布