后台服务系统之编写服务消费方实现
生活随笔
收集整理的這篇文章主要介紹了
后台服务系统之编写服务消费方实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?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>learn-dubbo</artifactId><groupId>cn.learn.dubbo</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>learn-dubbo-consumer</artifactId><dependencies><!--添加springboot依賴,非web項目--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.13</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><!--引入service的依賴--><dependency><groupId>cn.learn.dubbo</groupId><artifactId>learn-dubbo-service</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>
package cn.learn.dubbo;import cn.learn.dubbo.pojo.User;
import cn.learn.dubbo.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.util.List;@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {@Reference(version = "1.0.0", loadbalance = "roundrobin")private UserService userService;@Testpublic void testQueryAll() {for (int i = 0; i < 100; i++) {System.out.println("開始調用遠程服務 >>>>>" + i);List<User> users = this.userService.queryAll();for (User user : users) {System.out.println(user);}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}
package cn.learn.dubbo.service;import cn.learn.dubbo.pojo.User;import java.util.List;public interface UserService {/*** 查詢所有的用戶數據** @return*/List<User> queryAll();}
package cn.learn.dubbo.service.impl;import java.util.ArrayList;
import java.util.List;import cn.learn.dubbo.pojo.User;
import cn.learn.dubbo.service.UserService;
import com.alibaba.dubbo.config.annotation.Service;@Service(version = "${dubbo.service.version}") //聲明這是一個dubbo服務
public class UserServiceImpl implements UserService {/*** 實現查詢,這里做模擬實現,不做具體的數據庫查詢*/public List<User> queryAll() {List<User> list = new ArrayList<User>();for (int i = 0; i < 10; i++) {User user = new User();user.setAge(10 + i);user.setId(Long.valueOf(i + 1));user.setPassword("123456");user.setUsername("username_" + i);list.add(user);}System.out.println("---------Service 3------------");return list;}}
# Spring boot application
spring.application.name = learn-dubbo-consumer
server.port = 9091# 應用名稱
dubbo.application.name = dubbo-consumer-demo# zk注冊中心
dubbo.registry.address = zookeeper://localhost:2181
dubbo.registry.client = zkclient
?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的后台服务系统之编写服务消费方实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 后台服务系统之编写服务提供方实现
- 下一篇: 后台服务系统之Dubbo Admin的讲