Dubbo---Using direct connection method dubbo

1. Direct connection

  • Point-to-point direct connection project: Consumers directly access the service provider without a registration center. Consumers must specify the access address (url) of the service provider
  • Consumers directly access the fixed service provider through the url address, this url address is unchanged
    Insert picture description here

Method to realize:

(1) Create a service provider
A. Add dependencies and plug-ins in pom.xml
<!--Spring依赖-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.16.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.16.RELEASE</version>
</dependency>

<!--dubbo依赖-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
</dependency>
<build>
    <plugins>
        <!--JDK1.8编译插件-->
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
B. Create an entity class: User.java
public class User implements Serializable {
    
    

    private Integer id;
    private String username;
    private Integer age;
	//set,get方法
}
C. New service interface: UserService.java
public interface UserService {
    
    

    /**
     * 根据用户标识获取用户信息
     * @param id
     * @return
     */
    User queryUserById(Integer id);
}
D. New interface implementation class: UserServiceImpl.java
public class UserServiceImpl implements UserService {
    
    

    @Override
    public User queryUserById(Integer id) {
    
    
        User user = new User();
        user.setId(id);
        user.setUsername("lisi");
        user.setAge(23);
        return user;
    }
}
E. Create dubbo configuration file: dubbo-userservice-provider.xml
<!--服务提供者声明名称:必须保证服务名称的唯一性,它的名称是dubbo内部使用的唯一标识-->
<dubbo:application name="003-link-userservice-provider"/>

<!--访问服务协议的名称及端口号,dubbo官方推荐使用的是dubbo协议,端口号默认为20880-->
<!--
    name:指定协议的名称
    port:指定协议的端口号(默认为20880)
-->
<dubbo:protocol name="dubbo" port="20880"/>

<!--
    暴露服务接口->dubbo:service
    interface:暴露服务接口的全限定类名
    ref:接口引用的实现类在spring容器中的标识
    registry:如果不使用注册中心,则值为:N/A
-->
<dubbo:service interface="com.hcz.dubbo.service.UserService" ref="userServiceImpl" registry="N/A"/>

<!--将接口的实现类加载到spring容器中-->
<bean id="userServiceImpl" class="com.hcz.dubbo.service.impl.UserServiceImpl"/>
F. Configure the listener in web.xml
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:dubbo-userservice-provider.xml</param-value>
</context-param>
<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
G. Type the project into a jar package to the maven warehouse
  • The methods in the service interface need to be used by consumers, and the consumer project needs to know the interface name and the method names and parameters in the interface. These information service providers know that the class file of the interface needs to be packaged as a jar
  • The class files of the service interface project are packaged as jars and installed in the maven warehouse. The provider jars in the warehouse can be used by consumers
  • Use idea's maven window to execute install
(2) Create service consumers
A. Add the jar package of the service provider in the pom.xml file, and the rest is the same as the service provider above
<!--依赖服务提供者-->
<dependency>
    <groupId>com.hcz.dubbo</groupId>
    <artifactId>003-link-userservice-provider</artifactId>
    <version>1.0.0</version>
    <scope>compile</scope>
</dependency>
B. Create a service consumer controller class: UserController.java
@Controller
public class UserController {
    
    

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/user")
    public String userDetail(Model model,Integer id) {
    
    

        User user = this.userService.queryUserById(id);
        model.addAttribute("user",user);
        return "userDetail";
    }
}
C. Create dubbo configuration file: dubbo-consumer.xml
<!--声明服务消费者的名称:保证唯一性-->
<dubbo:application name="004-link-consumer"/>

<!--
    引用远程服务接口:
    id:远程服务接口对象名称
    interface:调用远程接口的全限定类名
    url:访问服务接口的地址
    registry:不使用注册中心,值为:N/A
-->
<dubbo:reference id="userService"
                 interface="com.hcz.dubbo.service.UserService"
                 url="dubbo://localhost:20880"
                 registry="N/A"/>
D. Create a spring configuration file: application.xml
<!--扫描组件-->
<context:component-scan base-package="com.wkcto.dubbo.web"/>

<!--配置注解驱动-->
<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
</bean>
E. Configure the central scheduler and initialization context in the web.xml file
<servlet>
   <servlet-name>dispatcherServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:application.xml,classpath:dubbo-consumer.xml</param-value>
   </init-param>
</servlet>
<servlet-mapping>
   <servlet-name>dispatcherServlet</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>
F. Create a display page: userDetail.jsp
<body>
    <h1>用户详情</h1>
    <div>用户标识:${user.id}</div>
    <div>用户名称:${user.username}</div>
</body>

Please see the next blog post for the best practice of dubbo servitization! ! !

Guess you like

Origin blog.csdn.net/hcz666/article/details/115058048