sofa框架server-client搭建

sofa框架server-client搭建

采用工具IDEA,组件添加完成spring-boot构建插件

jdk版本8.0以上,构建工具maven

第一步创建工程 SofaDemoServer 和 SofaDemoClient

 

 

SofaDemoClient 和 SofaDemoServer 的创建方式一样就不再重复了。

第二步配置 SofaDemoServer 和 SofaDemoClient 的pom.xml文件

注释掉原来的spring-boot的配置,修改为 sofa

	<!--<parent>-->
	<!--<groupId>org.springframework.boot</groupId>-->
	<!--<artifactId>spring-boot-starter-parent</artifactId>-->
	<!--<version>2.0.5.RELEASE</version>-->
	<!--<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
	<!--</parent>-->

	<parent>
		<groupId>com.alipay.sofa</groupId>
		<artifactId>sofaboot-dependencies</artifactId>
		<version>2.3.2</version>
	</parent>

导入的maven包也是一样

	<dependencies>
		<dependency>
			<groupId>com.alipay.sofa</groupId>
			<artifactId>rpc-sofa-boot-starter</artifactId>
		</dependency>
		<!--<dependency>-->
			<!--<groupId>org.springframework.boot</groupId>-->
			<!--<artifactId>spring-boot-starter-web</artifactId>-->
		<!--</dependency>-->

		<!--<dependency>-->
			<!--<groupId>org.springframework.boot</groupId>-->
			<!--<artifactId>spring-boot-starter-test</artifactId>-->
			<!--<scope>test</scope>-->
		<!--</dependency>-->
	</dependencies>

第三步 配置 SofaDemoServer

在src->main->resource下创建 spring文件下 ,并且在spring文件夹下创建 rpc-sofa.xml

rpc-sofa.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:sofa="http://sofastack.io/schema/sofaboot"
       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://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
       default-autowire="byName">

    <bean id="helloSynServiceImpl" class="com.example.demo.HelloSynServiceImpl" />
    <sofa:service ref="helloSynServiceImpl" interface="com.example.demo.HelloSynService">
        <sofa:binding.bolt/>
    </sofa:service>


    <!-- <sofa:reference id="helloSynServiceReference" interface="com.example.demogrowth.HelloSynService">
        <sofa:binding.bolt/>
    </sofa:reference> -->


</beans>

创建 HelloSynService.java

package com.example.demo;

public interface HelloSynService {
    String saySync(String str);
}

创建 HelloSynServiceImpl.java

package com.example.demo;

public class HelloSynServiceImpl implements HelloSynService {
    @Override
    public String saySync(String str){
        return str;
    }
}

修改SofademoserverApplication.java

@ImportResource({"classpath*:spring/rpc-sofa.xml"})
@SpringBootApplication
public class SofademoserverApplication {

	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication(SofademoserverApplication.class);
		ApplicationContext applicationContext = springApplication.run(args);
//		SpringApplication.run(SofademoserverApplication.class, args);
	}
}

最后给服务端配置上名称和端口号

application.properties

server.port=8082
spring.application.name=SofaDemoServer

第四步:配置 SofaDemoClient

在src->main->resource下创建 spring文件下 ,并且在spring文件夹下创建 rpc-sofa.xml

rpc-sofa.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:sofa="http://sofastack.io/schema/sofaboot"
       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://sofastack.io/schema/sofaboot   http://sofastack.io/schema/sofaboot.xsd"
       default-autowire="byName">

    <sofa:reference id="helloSynServiceReference" interface="com.example.demo.HelloSynService">
        <sofa:binding.bolt/>
    </sofa:reference>

</beans>

创建 HelloSynService.java

package com.example.demo;


public interface HelloSynService {
    String saySync(String str);
}

修改 SofademoclientApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;

@ImportResource({"classpath*:spring/rpc-sofa.xml"})
@SpringBootApplication
public class SofademoclientApplication {

	public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(SofademoclientApplication.class);
        ApplicationContext applicationContext = springApplication.run(args);
        HelloSynService helloSynServiceReference = (HelloSynService)applicationContext.getBean("helloSynServiceReference");
        System.out.println(helloSynServiceReference.saySync("sync"));
//		SpringApplication.run(SofademoclientApplication.class, args);
	}
}

最后配置application.properties

server.port=8081
spring.application.name=SofaDemoClient

 

ok到此完成了基本的构建。先启动 SofaDemoServer 然后启动 SofaDemoClient 调用成功会出现如下日志:

client日志

2018-09-17 14:42:44.410  INFO 9584 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-09-17 14:42:44.421  INFO 9584 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2018-09-17 14:42:44.548  INFO 9584 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2018-09-17 14:42:44.553  INFO 9584 --- [           main] c.e.demo.SofademoclientApplication       : Started SofademoclientApplication in 5.241 seconds (JVM running for 5.798)
sync

最后给大家demo下载地址

https://download.youkuaiyun.com/download/kevin2562/10671626

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值