javascript
dubbo转Spring Cloud
一個(gè)系統(tǒng)原來是使用dubbo的,現(xiàn)在想把微服務(wù)架構(gòu)轉(zhuǎn)為Spring Cloud。但是這個(gè)系統(tǒng)已經(jīng)在使用中了,希望一步步的把dubbo服務(wù)一個(gè)個(gè)的轉(zhuǎn)為Spring Cloud,轉(zhuǎn)換一個(gè)使用一個(gè),轉(zhuǎn)換的過程中盡量少影響原系統(tǒng)。
比如現(xiàn)在要轉(zhuǎn)換dubbo服務(wù)D1成Spring Cloud服務(wù)C1,D1又需要調(diào)用dubbo服務(wù)D2。怎么辦呢?如果同時(shí)轉(zhuǎn)換D1,D2的話,D2可能又會調(diào)用D3,這樣可能引用一大堆服務(wù)。這種做法肯定不可取。一種更好的解決方案是:提供一個(gè)Spring Cloud的代理服務(wù),這個(gè)代理服務(wù)同時(shí)是一個(gè)Spring Cloud服務(wù)和dubbo客戶端,其作用就是將對Spring Cloud服務(wù)的調(diào)用轉(zhuǎn)為對dubbo服務(wù)的調(diào)用。這樣Spring Cloud服務(wù)C1需要調(diào)用的Spring Cloud服務(wù)C2就先以代理的方式實(shí)現(xiàn)。等服務(wù)C1測試完成后,再停掉代理中的C2,將D2轉(zhuǎn)換為真正的Spring Cloud服務(wù)C2。這樣逐步把所有的dubbo服務(wù)都轉(zhuǎn)換成Spring Cloud服務(wù)。這種方案的優(yōu)點(diǎn)是原系統(tǒng)不用做任何修改,避免了修改帶來的風(fēng)險(xiǎn)。
這個(gè)方案的關(guān)鍵就是代理服務(wù)的實(shí)現(xiàn)。
假設(shè)有兩個(gè)dubbo服務(wù),分別為Hello1Service和Hello2Service如下:
public interface Hello1Service {String hello(String name); } public interface Hello2Service {String hello(String name); }創(chuàng)建一個(gè)MAVEN項(xiàng)目。POM文件為:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>CloudDubboProxy</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>CloudDubboProxy</name><description>Cloud Dubbo Proxy</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.RC1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</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-jetty</artifactId></dependency--><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.8.4</version></dependency><!-- dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.15.0-GA</version></dependency--><dependency><groupId>org.apache.mina</groupId><artifactId>mina-core</artifactId><version>1.1.7</version></dependency><dependency><groupId>org.glassfish.grizzly</groupId><artifactId>grizzly-core</artifactId><version>2.1.4</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.5</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.1.39</version></dependency><dependency><groupId>org.apache.bsf</groupId><artifactId>bsf-api</artifactId><version>3.1</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.6</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><!-- dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>2.5.0</version></dependency--><dependency><groupId>com.googlecode.xmemcached</groupId><artifactId>xmemcached</artifactId><version>1.3.6</version></dependency><dependency><groupId>de.ruedigermoeller</groupId><artifactId>fst</artifactId><version>1.55</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!-- dependency><groupId>com.alibaba</groupId><artifactId>dubbo-parent</artifactId><version>2.8.4</version><type>pom</type><scope>import</scope></dependency--> </dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><snapshots><enabled>false</enabled></snapshots></repository><repository> <id> central</id> <name> Maven Repository Switchboard</name> <layout> default</layout> <url> http://repo1.maven.org/maven2</url> <snapshots> <enabled> false</enabled> </snapshots> </repository> </repositories></project>dubbo的Spring配置文件:
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:application name="demo-consumer" owner="programmer" organization="dubbox"/><dubbo:registry address="zookeeper://127.0.0.1:2181"/><!--uncomment this if you want to test dubbo's monitor--><!--<dubbo:monitor protocol="registry"/>--><dubbo:reference id="hello1Service" interface="com.alibaba.dubbo.demo.hello.Hello1Service"/><dubbo:reference id="hello2Service" interface="com.alibaba.dubbo.demo.hello.Hello2Service"/> </beans>Spring Cloud的配置文件:
#debug: true eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/ server:port: 9999 spring:application:name: service-aapplication:name: service-b注意:Spring Cloud 1.x中只支持一個(gè)服務(wù),Spring Cloud 2.x中可以定義多個(gè)服務(wù)。
代理的啟動類:
@SpringBootApplication @EnableEurekaClient @RestController public class CloudDubboProxyApplication {private static Hello1Service hello1Service;private static Hello2Service hello2Service;public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-context.xml");context.start();hello1Service = context.getBean(Hello1Service.class);hello2Service = context.getBean(Hello2Service.class);SpringApplication.run(CloudDubboProxyApplication.class, args);}@RequestMapping("/a")public String ma(@RequestParam String name) {String s = hello1Service.hello("world");return "From Cloud service A: "+name+"<br>"+s;}@RequestMapping("/b")public String mb(@RequestParam String name) {String s = hello2Service.hello("world");return "From Cloud service B: "+name+"<br>"+s;} }寫個(gè)測試類:
@SpringBootApplication @EnableDiscoveryClient @RestController public class DemoRibbonApplication {public static void main(String[] args) {SpringApplication.run(DemoRibbonApplication.class, args);}@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}@AutowiredRestTemplate restTemplate;@RequestMapping(value = "/a")public String ha(@RequestParam String name){return restTemplate.getForObject("http://SERVICE-A/a?name="+name,String.class);} @RequestMapping(value = "/b")public String hb(@RequestParam String name){return restTemplate.getForObject("http://SERVICE-B/b?name="+name,String.class);} }可以驗(yàn)證確實(shí)實(shí)現(xiàn)了Spring Cloud調(diào)用dubbo功能。
總結(jié)
以上是生活随笔為你收集整理的dubbo转Spring Cloud的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于tomcat开启gzip的配置
- 下一篇: 猿创征文|Spring系列框架之面向切面