雖然Spring Boot能夠無縫的和Spring cloud對接。但仍是想試下Spring Boot + Dubbo怎麼玩java
根據Dubbo的官網介紹git
https://github.com/apache/incubator-dubbo-spring-boot-projectgithub
須要添加依賴以下:spring
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency>
###配置文件apache
按照文檔,設置的配置問價以下,zk做爲註冊中心。json
server: port: 8083 spring: profiles: dev logging: level: debug path: D:/log/ #dubbo註冊部分 dubbo: scan: #將這些包下面的Dubbo相關注解掃描到容器中 base-packages: cn.com.boostarp.service application: id: dubbo-provider-demo name: dubbo-provider-demo protocol: name: dubbo #dubbo註冊地址 registry: id: zk_address #註冊地址 address: localhost:2181;localhost:2182;localhost:2183 #使用的協議 protocol: zookeeper timeout: 6000
對外提供的服務包路徑應當包含與YML配置的路徑下面tomcat
package cn.com.boostarp.service; import java.util.Map; public interface ProviderService { Map<String, Object> getMsg() throws Exception; }
對應的實現app
package cn.com.boostarp.service.impl; import cn.com.boostarp.service.ProviderService; import com.alibaba.dubbo.config.annotation.Service; import java.util.HashMap; import java.util.Map; //這個Service註解是Dubbo提供的 @Service public class ProviderServiceImpl implements ProviderService { @Override public Map<String, Object> getMsg() throws Exception { Map<String, Object> listMap = new HashMap<String, Object>(5); listMap.put("f", "1"); listMap.put("g", "1"); return listMap; } }
最後一步在啓動生產者的時候,重啓後,tomcat會自動中止。最後在看Dubbo的版本是2.0.1。估計是Dubbo版本過低的緣故,因此須要本身手動引入高版本的Dubbo模塊,修改配置文件以下:須要排除 dubbo-spring-boot-starter 中依賴的2.0.1版本,而後再從新引入2.6.1版本。ide
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> <exclusions> <exclusion> <artifactId>com.alibaba</artifactId> <groupId>dubbo</groupId> </exclusion> </exclusions> </dependency> <!-- 從新引入2.6.1 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.1</version> </dependency>
從新啓動後,就能將服務註冊上去。spring-boot
測試:invoke cn.com.boostarp.service.ProviderService.getMsg()。發現消息轉換錯誤.
添加fastJSON依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
添加響應的消息轉換器
@Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { /** * {@inheritDoc} * <p>This implementation is empty. * * @param converters */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); //定義一個json消息轉換對象 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //添加配置信息 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //2-1 處理中文亂碼問題 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); //三、在convert中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); //四、將convert添加到converters中 //HttpMessageConverter<?> converter = fastConverter; converters.add(fastConverter); } }
至此,對應的服務就註冊到zk中心了。