dubbo转Spring Cloud

一个系统原来是使用dubbo的,现在想把微服务架构转为Spring Cloud。但是这个系统已经在使用中了,希望一步步的把dubbo服务一个个的转为Spring Cloud,转换一个使用一个,转换的过程中尽量少影响原系统。

比如现在要转换dubbo服务D1成Spring Cloud服务C1,D1又需要调用dubbo服务D2。怎么办呢?如果同时转换D1,D2的话,D2可能又会调用D3,这样可能引用一大堆服务。这种做法肯定不可取。一种更好的解决方案是:提供一个Spring Cloud的代理服务,这个代理服务同时是一个Spring Cloud服务和dubbo客户端,其作用就是将对Spring Cloud服务的调用转为对dubbo服务的调用。这样Spring Cloud服务C1需要调用的Spring Cloud服务C2就先以代理的方式实现。等服务C1测试完成后,再停掉代理中的C2,将D2转换为真正的Spring Cloud服务C2。这样逐步把所有的dubbo服务都转换成Spring Cloud服务。这种方案的优点是原系统不用做任何修改,避免了修改带来的风险。

这个方案的关键就是代理服务的实现。

假设有两个dubbo服务,分别为Hello1Service和Hello2Service如下:

public interface Hello1Service {
    String hello(String name);
}
public interface Hello2Service {
    String hello(String name);
}

创建一个MAVEN项目。POM文件为:



	4.0.0

	com.example
	CloudDubboProxy
	0.0.1-SNAPSHOT
	jar

	CloudDubboProxy
	Cloud Dubbo Proxy

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.1.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
		Finchley.RC1
	

	
		
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            com.alibaba
            dubbo
            2.8.4
        

        

        
            org.apache.mina
            mina-core
            1.1.7
        

        
            org.glassfish.grizzly
            grizzly-core
            2.1.4
        

        
            org.apache.httpcomponents
            httpclient
            4.5.5
        

        
            com.alibaba
            fastjson
            1.1.39
        

        
            org.apache.bsf
            bsf-api
            3.1
        

        
            org.apache.zookeeper
            zookeeper
            3.4.6
        

        
            com.github.sgroschupf
            zkclient
            0.1
        

        

        
            com.googlecode.xmemcached
            xmemcached
            1.3.6
        

        
            de.ruedigermoeller
            fst
            1.55
        
        
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
						
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
		
	      alimaven
	      aliyun maven
	      http://maven.aliyun.com/nexus/content/groups/public/
			
				false
			
		
		        
    		 central        
    		 Maven Repository Switchboard        
    		 default        
    		 http://repo1.maven.org/maven2        
    		        
      			 false        
    		        
  		 
  	



dubbo的Spring配置文件:




    

    

    
    

    
    

Spring Cloud的配置文件:

#debug: true
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 9999
spring:
  application:
    name: service-a
  application:
    name: service-b    

注意:Spring Cloud 1.x中只支持一个服务,Spring Cloud 2.x中可以定义多个服务。

代理的启动类:

@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+"
"+s; } @RequestMapping("/b") public String mb(@RequestParam String name) { String s = hello2Service.hello("world"); return "From Cloud service B: "+name+"
"+s; } }

写个测试类:

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class DemoRibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoRibbonApplication.class, args);
	}
	
	@Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
	
	@Autowired
	RestTemplate 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);
    }    
}
可以验证确实实现了Spring Cloud调用dubbo功能。


你可能感兴趣的:(微服务,java,大数据)