springboot 集成dubbo
来源:互联网 发布:天津网络推广和seo 编辑:程序博客网 时间:2024/06/06 23:57
一。引入相关依赖
pom.xml
<!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>
二。引入配置
package com.jztey.framework.boot; import com.alibaba.dubbo.config.*; import com.alibaba.dubbo.config.spring.AnnotationBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; /** * Created by Charles on 2016/6/11. */ public class ApplicationDubbo { @Bean @ConfigurationProperties(prefix = "dubbo.application") public ApplicationConfig applicationConfig() { return new ApplicationConfig(); } @Bean @ConfigurationProperties(prefix = "dubbo.protocol") public ProtocolConfig protocolConfig() { return new ProtocolConfig(); } @Bean @ConfigurationProperties(prefix = "dubbo.registry") public RegistryConfig registryConfig() { return new RegistryConfig(); } @Bean @ConfigurationProperties(prefix = "dubbo.annotation") public AnnotationBean annotationBean() { return new AnnotationBean(); } @Bean @ConfigurationProperties(prefix = "dubbo.provider") public ProviderConfig providerConfig() { return new ProviderConfig(); } @Bean @ConfigurationProperties(prefix = "dubbo.reference") public ReferenceConfig referenceConfig() { return new ReferenceConfig(); } @Bean @ConfigurationProperties(prefix = "dubbo.consumer") public ConsumerConfig consumerConfig() { return new ConsumerConfig(); } }
三。启动导入配置
package com.jztey.gxdemo; import java.net.UnknownHostException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.CachingConfigurer; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.jztey.framework.boot.ApplicationDruid; import com.jztey.framework.boot.ApplicationDubbo; import com.jztey.framework.boot.ApplicationInterfaceMvc; import com.jztey.framework.boot.ApplicationMonitoring; import com.jztey.gxdemo.entity.OauthParam; import com.jztey.gxdemo.service.DemoApiService; /** * Created by yushi on 2016/8/8. */ @SpringBootApplication @Import({ ApplicationInterfaceMvc.class // , ApplicationDubbo.class// 导入dubbo , ApplicationDruid.class// 导入Druid , ApplicationMonitoring.class }) public class Application { @Bean public WebMvcConfigurer corsConfigurer() { // 允许跨域 return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS"); } }; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
四。application.properties配置
#dubbo配置 dubbo.application.name=gxdemo dubbo.registry.protocol=zookeeper dubbo.registry.address=10.3.5.36:2181,10.3.5.38:2181,10.3.5.42:2181 dubbo.annotation.package=com.jztey.${spring.application.name}
五。编写服务端
package com.jztey.gxdemo.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestOperations; import com.alibaba.fastjson.JSONArray; import com.jztey.gxdemo.entity.OauthParam; @com.alibaba.dubbo.config.annotation.Service @Service public class DemoApiServiceImpl implements DemoApiService{ @Autowired private RestOperations restOperations; @Autowired private OauthService oauthService; public JSONArray queryCompany(){ String url = "https://internal.api.ehaoyao.com/logistics/v1.1/company"; OauthParam param = new OauthParam(); param.setGrantType("client_credentials"); param.setClientId("02bceec105cd4462b2dadd892fdba943"); param.setClientSecret("fa50ffdbbbf9442f8958e1493e7a1b41"); param.setScope("mall"); String token = oauthService.getToken(param); url=url+"?access_token="+token; JSONArray result = restOperations.getForObject(url, JSONArray.class); System.out.println(result.toString()); return result; } }
六。消费端
mvn clean deploy
用注释 com.alibaba.dubbo.config.annotation.Reference; 注入
@Reference private DemoApiService demoApiService;
调用
demoApiService.queryCompany();
阅读全文
0 0
- springboot集成dubbo
- springboot集成dubbo
- Springboot集成dubbo
- springboot 集成dubbo
- springboot+dubbo+mybatis集成
- springboot集成dubbo、zookeeper
- springboot 集成dubbo
- SpringBoot集成dubbo
- springboot集成dubbo,dubbox分布式服务框架
- 一步一步学springboot (十三)集成dubbo
- springboot+dubbo
- springboot 集成dubbo,RPC 远程调用服务接口:提示服务空指针异常
- springboot+mybatis+dubbo整合
- springboot整合dubbo
- 使用springboot构建dubbo
- springboot如何接入dubbo
- springboot整合dubbo
- springboot整合dubbo
- UNIX再学习 -- 函数 select、poll、epoll
- js数组去重方法
- hashMap的基础知识
- vtk编译vtkSDI找不到MFC模块的解决方案
- skynet添加动态库爬坑记录
- springboot 集成dubbo
- Android 开发之toolbar使用
- HashMap详解
- 队列
- Java--填充替换数组元素、对数组进行排序、复制数组、数组查询、
- java读取文本
- Beyond compare4.2.0Beta破解注册码 Beyond Compare:代码比较神器
- protege本体开发
- 【转载】字符编码详解——彻底理解掌握编码知识,“乱码”不复存在