• dubbo-springboot入门级demo


    1. dubbo-springboot入门级demo

    1.1. 前言

    1. 最后一个做运维的朋友和我提起,他们公司想做个dubbo灰度发布的功能,而这个功能落到了他头上。在我的印象里,dubbo应该可以通过扩展代码来实现特定用户路由到特定服务器,来实现这方面的灰度功能,但完全由运维去做,可能就需要脚本了吧,dubbo是否支持脚本我还不清楚,趁着这个进一步熟悉dubbo的过程,我来系统的学一下dubbo的基本功能,由于现在基本都用springboot来开发了,所以直接从springboot入门级dubbo应用开始

    1.2. 生产者

    1.2.1. 服务方法和接口

    public interface GreetingService {
        String sayHello(String name);
    }
    
    import com.tzxylao.dubbo.service.GreetingService;
    import org.apache.dubbo.config.annotation.Service;
    import org.springframework.beans.factory.annotation.Value;
    
    @Service(version = "1.0.0")
    public class GreetingServiceImpl implements GreetingService {
    
        @Value("${dubbo.application.name}")
        private String serviceName;
        @Value("${dubbo.protocol.port:0}")
        private int port;
    
        @Override
        public String sayHello(String name) {
            return String.format("[%s][%s] : Hello, %s", serviceName,port, name);
        }
    }
    

    1.2.2. 配置文件application.properties

    server.port=8082
    
    dubbo.application.name=first-dubbo-provider
    dubbo.scan.base-packages=com.tzxylao.dubbo.service
    
    dubbo.protocol.name=dubbo
    dubbo.protocol.port=20884
    dubbo.registry.address=zookeeper://127.0.0.1:2181
    

    1.2.3. pom

    <properties>
    		<java.version>1.8</java.version>
            <spring-boot.version>2.1.1.RELEASE</spring-boot.version>
            <dubbo.version>2.7.1</dubbo.version>
            <curator.version>2.8.0</curator.version>
            <zookeeper.version>3.4.6</zookeeper.version>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.projectlombok</groupId>
    			<artifactId>lombok</artifactId>
    			<optional>true</optional>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    
    
            <!-- Dubbo Spring Boot Starter -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
            </dependency>
    
    
            <!-- zookeeper -->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>io.netty</groupId>
                        <artifactId>netty</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>${curator.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>${curator.version}</version>
            </dependency>
    	</dependencies>
        <repositories>
            <repository>
                <id>apache.snapshots.https</id>
                <name>Apache Development Snapshot Repository</name>
                <url>https://repository.apache.org/content/repositories/snapshots</url>
                <releases>
                    <enabled>false</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
    

    1.2.4. 启动方法

    @EnableAutoConfiguration
    public class DubboApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(DubboApplication.class, args);
    	}
    
    }
    

    1.3. 消费者

    1.3.1. 接口同上

    1.3.2. pom同上

    1.3.3. 配置文件

    server.port=8081
    
    spring.application.name=first-dubbo-consumer
    dubbo.registry.address=zookeeper://127.0.0.1:2181
    

    1.3.4. 启动方法

    @EnableAutoConfiguration
    public class DubboConsumerApplication {
    
    	@Reference(version = "1.0.0")
    	private GreetingService greetingService;
    
    	public static void main(String[] args) throws InterruptedException {
    		SpringApplication.run(DubboConsumerApplication.class, args);
    	}
    
    	@Bean
    	public ApplicationRunner runner(){
    		int count = 0;
    		while (true) {
    			System.out.println(greetingService.sayHello("World "));
    			try {
    				TimeUnit.SECONDS.sleep(3);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			count++;
    			if (count == 100) {
    				break;
    			}
    		}
    		return s -> {
    			System.out.println(greetingService.sayHello("World"));
    		};
    	}
    
    }
    

    1.4. 运行结果

    ...
    [first-dubbo-provider][20884] : Hello, World
    [first-dubbo-provider][20884] : Hello, World
    ...
    

    1.5. 总结

    1. 这里需要注意的几个核心点就是扫描包路劲别搞错
    2. @Refernece注解用的是dubbo包里的,@Service也是dubbo包里的

    Github地址:https://github.com/tzxylao/learn-demo

  • 相关阅读:
    Socket
    利用Python自动生成暴力破解的字典
    【转】六年测试工作的思考1
    【转】手机测试入行三年的感想
    【转】移动测试人员的未来:测试开发技术的融合
    【转】一个互联网项目即将结束的软件测试经验总结
    【转】电子商务网站测试经验总结
    【转】六年软件测试感悟-从博彦到VMware
    使用PL/SQL删除百万条记录的大表
    【转】百万级数据查询优化
  • 原文地址:https://www.cnblogs.com/sky-chen/p/10734549.html
Copyright © 2020-2023  润新知