# 微服务集成

# 工程示例

系统应用集成示例工程打开 (opens new window)

# 本内容你将获得

  • 基于多种方式的微服务模式,包括 OpenFeign/Dubbo/Forest
  • 微服务场景模式的切换
  • 新旧系统场景下的微服务兼容切换

# 微服务调用说明

这里主要演示 openFeign/Forest/dubbo 场景下的,其它的 k8s/Mesh 下的类同

# 配置 nacos 注册中心

此步骤可省略

添加注册中心配置,在 yml 中添加如下配置,openFeign 请自行百度进行配置

dubbo:
  registry:
    address: nacos://nacos.lbxinhu.linesno.com:23456
    timeout: 60000
    file: ${user.home}/alinesno-dubbo-output/${dubbo.area.city}-${spring.application.name}/dubbo.cache

# 集成 OpenFeign

集成 Forest 与 OpenFeign 类似,这里不再做阐述,添加 openFeign 组件包

<dependency>
	<groupId>com.alinesno.cloud.common</groupId>
	<artifactId>alinesno-cloud-common-feign</artifactId>
  <version>${alinesno.cloud.version}</version>
</dependency>

gateway模块中提供接口服务即可

# 集成 Dubbo

此处不建议和不推荐使用 dubbo 注解的模式,后期难以维护和切换技术

针对于老系统的改造配置,这里更加切合

添加 RPC 注解,如下:

@EnableRpc // 添加RPC注解
@EnableApi
@EnableAutoConfiguration
@MapperScan("com.alinesno.cloud.busines.shop.manage.mapper")
@ComponentScan(basePackages = {"com.alinesno.cloud.busines.shop.manage"})
public class Application {

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

}

配置 RPC 服务,在resource/spring目录中,配置提供或者消费的服务如下:

<!-- 生成远程服务代理,可以和本地bean一样使用demoService
<dubbo:reference id="demoService" interface="org.apache.dubbo.demo.DemoService" />
-->

文件说明:

  • rpc-consumer.xml: 消费 dubbo 服务
  • rpc-provider.xml: 提供 dubbo 服务

具体配置查看alinesno-cloud-common-config目录下 spring-rpc 开头的配置,如下:

dubbo:
  area:
    city: ${DUBBO_AREA:guangxi}
  protocol:
    name: dubbo
    port: -1
  monitor:
    protocol: registry
  consumer:
    timeout: ${DUBBO_CONSUMER_TIMEOUT:9000000}
    check: false
    retries: 3
    cache: threadlocal
    group: ${DUBBO_GROUP:alinesno-cloud}
    loadbalance: leastactive
    version: ${DUBBO_CONSUMER_VERSION:2.1.2-Alpha}
  provider:
    token: false
    version: ${DUBBO_PRIVIDER_VERSION:2.1.2-Alpha}
    group: ${DUBBO_GROUP:alinesno-cloud}
    loadbalance: leastactive
    delay: 5
    timeout: ${DUBBO_PRIVIDER_TIMEOUT:1800000}
    threadpool: ${DUBBO_PROVIDER_THREADPOOL:fixed}
    threads: ${DUBBO_PROVIDER_THREADS:300}
    accepts: ${DUBBO_PROVIDER_ACCEPTS:280}
    register: ${DUBBO_PRIVIDER_REGISTER:true}
  application:
    logger: slf4j
    name: ${spring.application.name}
  registry:
    address: nacos://nacos.lbxinhu.linesno.com:23456
    timeout: 60000
    file: ${user.home}/alinesno-dubbo-output/${dubbo.area.city}-${spring.application.name}/dubbo.cache

最后提供服务即可

# 其它