admin 管理员组文章数量: 1184232
Dubbo 配置http协议
一 服务提供者
在服务提供者中,需要添加 注释为http协议支持 的依赖jar包,修改启动类的启动方式,添加provider.xml配置
<?xml version="1.0" encoding="UTF-8"?><project xmlns=".0.0" xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><parent><artifactId>tech-dubbo</artifactId><groupId>com.tech</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>tech-service</artifactId><name>tech-service</name><properties><spring-boot.version>2.3.0.RELEASE</spring-boot.version><dubbo.version>2.7.8</dubbo.version></properties><dependencies><dependency><groupId>com.tech</groupId><artifactId>tech-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!-- Dubbo Spring Boot Starter --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId></dependency><!--zookeeper--><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.6.3</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>4.0.1</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>4.0.1</version></dependency><!--http协议支持--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-core</artifactId></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-server</artifactId></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-servlet</artifactId></dependency><dependency><groupId>com.github.briandilley.jsonrpc4j</groupId><artifactId>jsonrpc4j</artifactId><version>1.2.0</version></dependency></dependencies><dependencyManagement><dependencies><!-- Spring Boot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- Apache Dubbo --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-bom</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion><exclusion><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId></exclusion><exclusion><groupId>log4j</groupId><artifactId>log4j</artifactId></exclusion></exclusions></dependency></dependencies></dependencyManagement>
</project>
启动类修改服务启动方式
package com.tech;import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;/*** @author lw* @since 2021/10/11*/
@ImportResource("provider.xml")
@EnableAutoConfiguration
public class ServiceApp {public static void main(String[] args) {
// SpringApplication.run(ServiceApp.class);new SpringApplicationBuilder(ServiceApp.class).web(WebApplicationType.NONE).run();}
}
provider.xml
userService fileService配置为http协议
<beans xmlns:xsi=""xmlns:dubbo=""xmlns=""xsi:schemaLocation=" .xsd .xsd"><dubbo:registry address="zookeeper://192.168.50.133:2183?backup=192.168.50.132:2182,192.168.50.131:2181" timeout="30000"/><dubbo:protocol name="http" port="8666" server="jetty"/><bean id="userService" class="com.tech.impl.UserServiceImpl"/><bean id="fileService" class="com.tech.impl.FileServiceImpl"/><dubbo:service interface="com.tech.UserService" ref="userService" cluster="failsafe" protocol="http"/><dubbo:service interface="com.tech.FileService" ref="fileService" protocol="http"/>
</beans>
服务接口
package com.tech.impl;import com.tech.UserService;
import lombok.extern.slf4j.Slf4j;import java.util.concurrent.TimeUnit;@Slf4j
public class UserServiceImpl implements UserService {public String hello() {log.info("服务调用");try {TimeUnit.SECONDS.sleep(5L);log.info("调用结束");} catch (InterruptedException e) {e.printStackTrace();}return "hello word";}
}
package com.tech.impl;import com.tech.FileService;/*** @author lw* @since 2021/10/13*/
public class FileServiceImpl implements FileService {@Overridepublic void upload(String msg) {System.out.println("文件上传成功 "+msg);}
}
二 服务消费者
服务消费者,需要添加依赖包jsonrpc4j,counsermer.xml与采用dubbo协议配置方式一样,不用修改
<?xml version="1.0" encoding="UTF-8"?><project xmlns=".0.0" xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><parent><artifactId>tech-dubbo</artifactId><groupId>com.tech</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>tech-web</artifactId><name>tech-web</name><properties><spring-boot.version>2.3.0.RELEASE</spring-boot.version><dubbo.version>2.7.8</dubbo.version><zookeeper.version>2.12.0</zookeeper.version></properties><dependencies><dependency><groupId>com.tech</groupId><artifactId>tech-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Dubbo Spring Boot Starter --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.6.3</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>4.0.1</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>4.0.1</version></dependency><!--http协议支持--><dependency><groupId>com.github.briandilley.jsonrpc4j</groupId><artifactId>jsonrpc4j</artifactId><version>1.2.0</version></dependency></dependencies><dependencyManagement><dependencies><!-- Spring Boot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- Apache Dubbo --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-bom</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion><exclusion><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId></exclusion><exclusion><groupId>log4j</groupId><artifactId>log4j</artifactId></exclusion></exclusions></dependency></dependencies></dependencyManagement>
</project>
consumer.xml
<beans xmlns:xsi=""xmlns:dubbo=""xmlns=""xsi:schemaLocation=" .xsd .xsd"><dubbo:registry address="zookeeper://192.168.50.133:2183?backup=192.168.50.132:2182,192.168.50.131:2181" timeout="30000"/><dubbo:reference id="userService" check="false" interface="com.tech.UserService" retries="4" timeout="6000"/><dubbo:reference id="fileService" check="false" interface="com.tech.FileService" retries="4"/>
</beans>
启动类
package com.tech;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;@SpringBootApplication
@ImportResource("consumer.xml")
public class WebApp {public static void main(String[] args) {SpringApplication.run(WebApp.class);}
}
服务调用类
package com.tech.controller;import com.tech.FileService;
import com.tech.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author lw* @since 2021/10/11*/
@RestController
public class UserController {//dubbo.registry.address=N/A 需要通过url配置服务地址
// @DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:12345")//check 默认为true,启动依赖检查,如果依赖的UserService不存在(注册中心中没有该服务实例信息)则启动失败
// @DubboReference(version = "1.0.0",check = false)//timeout 服务调用时的超时时间,达到该时间服务消费者报超时异常//cluster配置集群容错策略
// failover (默认容错策略) 失败重试,默认重试2次
// failfast 快速失败,不进行重试
// forking 并行调用多个服务器,只要有一个成功就返回
// @DubboReference(version = "1.0.0",timeout = 3000,retries = 1,cluster = "failover")
// private UserService userService;
//
// @DubboReference(version = "1.0.0")
// private FileService fileService;@Autowiredprivate UserService userService;@Autowiredprivate FileService fileService;@GetMapping("hello")public String hello() {return userService.hello();}@GetMapping("upload")public String upload(){fileService.upload("hello word");return "hello word";}
}
本文标签: Dubbo 配置http协议
版权声明:本文标题:Dubbo 配置http协议 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.roclinux.cn/b/1686651735a20551.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论