标签:cmd 集群模式 media bsp sse pes 版本 获取 代理
2、在父项目中分别创建common(存放接口定义或bean)、provider(服务提供者)、consumer(服务消费者)的module子项目
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.mysteel</groupId> 8 <artifactId>test</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <modules> 12 <module>common</module> 13 <module>consumer</module> 14 <module>provider</module> 15 </modules> 16 17 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId>spring-context</artifactId> 21 <version>5.2.3.RELEASE</version> 22 </dependency> 23 24 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> 25 <dependency> 26 <groupId>org.springframework</groupId> 27 <artifactId>spring-core</artifactId> 28 <version>5.2.3.RELEASE</version> 29 </dependency> 30 31 <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-beans</artifactId> 35 <version>5.2.3.RELEASE</version> 36 </dependency> 37 38 <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-expression</artifactId> 42 <version>5.2.3.RELEASE</version> 43 </dependency> 44 45 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-aop</artifactId> 49 <version>5.2.3.RELEASE</version> 50 </dependency> 51 52 <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo --> 53 <dependency> 54 <groupId>org.apache.dubbo</groupId> 55 <artifactId>dubbo</artifactId> 56 <version>2.7.3</version> 57 </dependency> 58 59 <!--增加zookeeper作为注册中心--> 60 <dependency> 61 <groupId>org.apache.zookeeper</groupId> 62 <artifactId>zookeeper</artifactId> 63 <version>3.3.3</version> 64 <type>pom</type> 65 </dependency> 66 67 <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes --> 68 <dependency> 69 <groupId>org.apache.curator</groupId> 70 <artifactId>curator-recipes</artifactId> 71 <version>2.12.0</version> 72 </dependency> 73 74 <!--使用curator客户端--> 75 <dependency> 76 <groupId>com.netflix.curator</groupId> 77 <artifactId>curator-framework</artifactId> 78 <version>1.1.10</version> 79 </dependency> 80 81 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> 82 <dependency> 83 <groupId>org.slf4j</groupId> 84 <artifactId>slf4j-api</artifactId> 85 <version>1.7.24</version> 86 </dependency> 87 88 </dependencies> 89 90 </project>
<dependency> <groupId>com.mysteel</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
1 public class Address implements Serializable { 2 private int id; 3 private int userId; 4 private String address; 5 6 public Address(int id, int userId, String address) { 7 this.id = id; 8 this.userId = userId; 9 this.address = address; 10 } 11 12 public int getId() { 13 return id; 14 } 15 16 public void setId(int id) { 17 this.id = id; 18 } 19 20 public int getUserId() { 21 return userId; 22 } 23 24 public void setUserId(int userId) { 25 this.userId = userId; 26 } 27 28 public String getAddress() { 29 return address; 30 } 31 32 public void setAddress(String address) { 33 this.address = address; 34 } 35 36 @Override 37 public String toString() { 38 return "Address{" + 39 "id=" + id + 40 ", userId=" + userId + 41 ", address=‘" + address + ‘\‘‘ + 42 ‘}‘; 43 } 44 }
1 public class Person implements Serializable { 2 private int userId; 3 private String name; 4 private int age; 5 private List<Address> addresses; 6 7 public Person(int userId, String name, int age, List<Address> addresses) { 8 this.userId = userId; 9 this.name = name; 10 this.age = age; 11 this.addresses = addresses; 12 } 13 14 public int getUserId() { 15 return userId; 16 } 17 18 public void setUserId(int userId) { 19 this.userId = userId; 20 } 21 22 public String getName() { 23 return name; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public int getAge() { 31 return age; 32 } 33 34 public void setAge(int age) { 35 this.age = age; 36 } 37 38 public List<Address> getAddresses() { 39 return addresses; 40 } 41 42 public void setAddresses(List<Address> addresses) { 43 this.addresses = addresses; 44 } 45 46 @Override 47 public String toString() { 48 return "Person{" + 49 "userId=" + userId + 50 ", name=‘" + name + ‘\‘‘ + 51 ", age=" + age + 52 ", addresses=" + addresses + 53 ‘}‘; 54 } 55 }
public interface UserService { Person getUserAddressesByUserId(int userId); }
1 public class UserServiceImpl implements UserService { 2 public Person getUserAddressesByUserId(int userId) { 3 Person[] people = new Person[2]; 4 Address address1 = new Address(1,1,"江西省吉安市吉州区XXXX"); 5 Address address2 = new Address(2,1,"福建省厦门市思明区XXXX"); 6 Address address3 = new Address(3,2,"江西省南昌市青山湖区XXXX"); 7 List<Address> addressList1 = new ArrayList<Address>(); 8 addressList1.add(address1); 9 addressList1.add(address2); 10 List<Address> addressList2 = new ArrayList<Address>(); 11 addressList2.add(address3); 12 Person person1 = new Person(1,"张三",22,addressList1); 13 Person person2 = new Person(2,"李四",22,addressList2); 14 people[0] = person1; 15 people[1] = person2; 16 for (int i = 0;i < people.length;i++){ 17 if (people[i].getUserId() == userId){ 18 return people[i]; 19 } 20 } 21 return null; 22 } 23 }
<?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://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="provider"/> <!-- 使用zookeeper广播注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" client="curator"/> <!-- 用dubbo协议在2088端口暴露服务 --> <dubbo:protocol name="dubbo" port="2088"/> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.mysteel.cominterface.UserService" ref="userService"/> <!-- 和本地bean一样实现服务(暴露接口的实现) --> <bean id="userService" class="com.mysteel.impl.UserServiceImpl"/> </beans>
1 public class Provider { 2 public static void main(String[] args) throws IOException { 3 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml"); 4 context.start(); 5 System.in.read(); 6 } 7 }
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> 5 6 <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> 7 <dubbo:application name="consumer"/> 8 9 <!-- 使用zookeeper广播注册中心暴露服务地址 --> 10 <dubbo:registry address="zookeeper://127.0.0.1:2181" client="curator"/> 11 12 <!-- 生成远程服务代理,可以和本地bean一样使用userService,声明需要调用的接口 interface填写common中接口的全路径 --> 13 <dubbo:reference interface="com.mysteel.cominterface.UserService" id="userService"/> 14 </beans>
1 public class Consumer { 2 public static void main(String[] args) { 3 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:consumer.xml"); 4 context.start(); 5 //获取远程服务代理 6 UserService userService = (UserService) context.getBean("userService"); 7 System.out.println(userService.getUserAddressesByUserId(1)); 8 } 9 }
标签:cmd 集群模式 media bsp sse pes 版本 获取 代理
原文地址:https://www.cnblogs.com/remote/p/12915907.html