微服务,对内RPC,对外Rest

  |   0 评论   |   564 浏览

应用拆分成多个微服务后,在局域网内各个微服务内部调用采用RPC通讯,对外提供Rest服务。

为啥要对内RPC?打开链接有一段描述,
https://github.com/Meituan-Dianping/Leaf/blob/master/README_CN.md#leaf-core

Leaf Core

当然,为了追求更高的性能,需要通过RPC Server来部署Leaf 服务,那仅需要引入leaf-core的包,把生成ID的API封装到指定的RPC框架中即可。

这是美团公司开源的分布式主键项目(distributed-id-generator)。这里用 Spring Cloud Alibaba Dubbo RPC 演示使用。

image.png

leaf-rpc,新增2个接口 ISegmentService,ISnowflakeService

package com.sankuai.inf.leaf.rpc.service;

public interface ISegmentService {
    public String getDId(String key);
}
package com.sankuai.inf.leaf.rpc.service;

public interface ISnowflakeService {
    public String getDId(String key);
}

调整 leaf-server pom.xml

  • properties.spring-boot-dependencies.version 改为 2.2.5.RELEASE
  • dependencyManagement.dependencies 增加
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-alibaba-dependencies</artifactId>
	<version>2.2.0.RELEASE</version>
	<type>pom</type>
	<scope>import</scope>
</dependency>
  • dependencies 增加依赖,Dubbo 服务注册到 Nacos
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
	<groupId>com.sankuai.inf.leaf</groupId>
	<artifactId>leaf-rpc</artifactId>
	<version>${project.version}</version>
</dependency>
  • src\main\resources\application.properties 增加
spring.application.name=leaf-server
server.port=0

spring.cloud.nacos.discovery.server-addr=192.168.1.6:8848

dubbo.scan.base-packages=com.sankuai.inf.leaf.server.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost
  • src\main\resources\leaf.properties 填写 zk 地址和端口,并启动zk
leaf.name=com.sankuai.leaf.opensource.test
leaf.segment.enable=false
#leaf.jdbc.url=
#leaf.jdbc.username=
#leaf.jdbc.password=

leaf.snowflake.enable=true
leaf.snowflake.zk.address=192.168.1.6
leaf.snowflake.port=2181
root@jazz-pc:/opt#  docker ps --format "table {{.Names}} ------> {{.Ports}}" | grep zookeeper
zookeeper ------> 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp
  • 修改 src\main\java\com\sankuai\inf\leaf\server\service\SegmentService.java
    实现 leaf-rpc ISegmentService 接口,实现接口方法。把 @Service 注解换成 Dubbo @Service(timeout = 4000999) 具体路径为
import org.apache.dubbo.config.annotation.Service;
  • 复制 src\main\java\com\sankuai\inf\leaf\server\controller\LeafController.java 里面 get 方法到 SegmentService 里面。
@Override
public String getDId(String key) {
	return get(key, getId(key));
}

private String get(String key, Result id) {
	Result result;
	if (key == null || key.isEmpty()) {
		throw new NoKeyException();
	}
	result = id;
	if (result.getStatus().equals(Status.EXCEPTION)) {
		throw new LeafServerException(result.toString());
	}
	return String.valueOf(result.getId());
}
  • SnowflakeService 实现 leaf-rpc ISnowflakeService 接口并做类似操作

调整 leaf-parent pom.xml

新增 properties.java.version 1.8
调整 properties.spring.version 5.2.4.RELEASE
调整 properties.spring.slf4j.version 1.7.30
调整 properties.log4j.version 2.12.1

org.apache.maven.plugins source和target 调整为1.8

leaf-test 新增

  • TestController.java
package com.sankuai.inf.leaf.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.sankuai.inf.leaf.rpc.service.ISnowflakeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;

import java.util.ArrayList;
import java.util.List;

@RestController
public class TestController {
    @Reference(timeout = 4000999)
    private ISnowflakeService snowflakeService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test")
    public List<Object> test() {
        String restDid, rpcDid, format = "{\"restDid\":\"%s\",\"rpcDid\":\"%s\"}";
        List<Object> list = new ArrayList<Object>();

        for (int i = 0; i < 6; i++) {
            restDid = restTemplate.getForObject("http://leaf-server/api/snowflake/get/test", String.class);
            rpcDid = snowflakeService.getDId("test");
            list.add(JSONObject.parseObject(String.format(format, restDid, rpcDid)));
        }
        return list;
    }

}

@Configuration
class RestTemplateConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

  • 启动类 LeafTestApplication
package com.sankuai.inf.leaf.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

  • src\main\resources\application.properties Dubbo 服务订阅
spring.application.name=leaf-test
server.port=8080

spring.cloud.nacos.discovery.server-addr=192.168.1.6:8848

dubbo.registry.address=spring-cloud://localhost
dubbo.cloud.subscribed-services=leaf-server

  • pom.xml
<parent>
	<artifactId>leaf-parent</artifactId>
	<groupId>com.sankuai.inf.leaf</groupId>
	<version>1.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>leaf-test</artifactId>

<properties>
	<spring-boot-dependencies.version>2.2.5.RELEASE</spring-boot-dependencies.version>
</properties>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>${spring-boot-dependencies.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-alibaba-dependencies</artifactId>
			<version>2.2.0.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<dependencies>
	<dependency>
		<groupId>com.sankuai.inf.leaf</groupId>
		<artifactId>leaf-rpc</artifactId>
		<version>1.0.1</version>
	</dependency>
	<dependency>
		<groupId>com.alibaba.cloud</groupId>
		<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>com.alibaba.cloud</groupId>
		<artifactId>spring-cloud-starter-dubbo</artifactId>
	</dependency>
</dependencies>

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

都修改调整好了,然后 maven clean install leaf-parent,leaf-core,leaf-rpc -Dmaven.test.skip=true

启动注册中心 Nacos

root@jazz-pc:/opt/nacos/bin# ls
derby.log  logs  nohup.out  shutdown.cmd  shutdown.sh  startup.cmd  startup.sh  work
root@jazz-pc:/opt/nacos/bin# nohup bash -f ./startup.sh -m standalone &
[1] 13116
root@jazz-pc:/opt/nacos/bin# nohup: 忽略输入并把输出追加到'nohup.out'

[1]+  已完成               nohup bash -f ./startup.sh -m standalone

运行 leaf-server Dubbo 服务暴露 和 leaf-test Dubbo 服务订阅

image.png

image.png


标题:微服务,对内RPC,对外Rest
作者:shuikan95
地址:http://javadaily.cn/articles/2020/04/13/1586754235969.html

评论

发表评论