一。引入相关依赖
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();
--------------
spring mvc+tomcat源码分析视频 (复制链在浏览器打开)