Mr.Chrosing`s Home

Phoenix Nirvana

:D 获取中...


这以前是一个很懒的家伙,现在要变勤快啦.


Welecome to Chrosing`s Home

SOA project整合Zookeeper

工程配置详解 Provider和Customer

1.详解 Provider服务提供者工程

a.pom.xml文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.nasity</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/>
    </parent>
    <groupId>cn.nasity</groupId>
    <artifactId>provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>provider</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

b.application.yaml和spring-provider-dubbo.xml配置

application.yaml

server:
  port: 8080
  servlet:
    context-path: /

spring-provider-dubbo.xml

<?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.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="provider"/>
    <!-- 注册中心的ip地址 -->
    <dubbo:registry address="zookeeper://139.224.22.52:2181"/>
    <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
    <dubbo:annotation package="service.impl"/>
    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>


    <!-- 和本地bean一样实现服务 -->
    <bean id="service" class="cn.nasity.provider.service.impl.ServiceImpl"/>
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="cn.nasity.provider.service.IService" ref="userService"/>
</beans>

c.创建一个ServiceImpl实例

IService.class
package cn.nasity.provider.service;

import cn.nasity.provider.service.impl.IService;
import org.springframework.stereotype.Service;
package cn.nasity.provider.service.impl;

/**
 * @author : Kaisec_Cao
 * @version : 1.0
 * @date : 2020/1/11
 */
public interface IService {

    public String systemOutPrintOneString(String name);
}


ServiceImpl.class
package cn.nasity.provider.service;

import cn.nasity.provider.service.impl.IService;
import org.springframework.stereotype.Service;

/**
 * @author : Kaisec_Cao
 * @version : 1.0
 * @date : 2020/1/11
 */
@Service
public class ServiceImpl implements IService {
    @Override
    public String systemOutPrintOneString(String name) {
        return name;
    }
}

d.提供者启动Class

package cn.nasity.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
// 特别注意这里必须应用配置文件
@ImportResource({"classpath:spring-provider-dubbo.xml"})
public class ProviderApplication {

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

}

2.详解 Customer 服务消费者工程

a.pom.xml文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.nasity</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/>
    </parent>
    <groupId>cn.nasity</groupId>
    <artifactId>customer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>customer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
	
	<!-- 这里需要指向提供者 因为需要用到提供者中的service中的IService -->
        <dependency>
            <groupId>cn.nasity</groupId>
            <artifactId>provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

b.application.yaml和spring-customer-dubbo.xml配置

application.yaml

server:
  port: 8090
  servlet:
    context-path: /

spring-customer-dubbo.xml

<?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.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="consumer"/>
    <dubbo:registry address="zookeeper://139.224.22.52:2181"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:annotation package="controller"/>

    <!-- 接口地址 -->
    <dubbo:reference id="service" check="false" interface="cn.nasity.provider.service.IService"/>

</beans>

c.创建一个Controller实例

package cn.nasity.customer.controller;

import cn.nasity.provider.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : Kaisec_Cao
 * @version : 1.0
 * @date : 2020/1/11
 */
@RestController
@RequestMapping("/")
public class DemoController {

    private final IService service;

    @Autowired
    public DemoController(IService service){
        this.service = service;
    }

    @GetMapping
    public String demoController(){
        return service.systemOutPrintOneString("Hello World!!!");
    }
}

d.消费者启动Class

package cn.nasity.customer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
// 应用配置文件xml
@ImportResource({"classpath:spring-customer-dubbo.xml"})
public class CustomerApplication {

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

启动提供者Provider和消费者Customer

这是提供者Provider的启动日志
2
可以看见 日志信息中 有Client的一些参数和我们连接服务器的地址(就是zookeeper的安装 ,然后提供者的端口号是8080,

这是提供者Customer的启动日志
3
其中也有Client的基本信息和Zk服务地址
但是 这里我们启动的是8090地址
所以 在浏览器中输入 http://localhost:8090/demo
这个demo就是我们在Controller.class配置的连接
4
如图所示

最后 可以发现 页面上输出了Hello World!!!
好了 至此全部搭建完成

下一节我们将围绕这个 看看Dubbo-Admin里面的服务注册

最近的文章

Java23种设计模式(白话文)

转载自:https://blog.csdn.net/sdjadycsdn/article/details/80659466作者Blog链接:https://blog.csdn.net/sdjadycsdn一、创建型模式  1、FACTORY:追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱…

继续阅读
更早的文章

linux安装mysql服务以及本地连接数据库

Linux(版本CentOS7)安装Mysql(傻瓜式安装)下载repo源wgethttp://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm安装rpm包rpm-ivhmysql-community-release-el7-5.noar…

继续阅读