Skip to content

Commit

Permalink
[Feature] Dubbo Admin provides service mock ability. (#838)
Browse files Browse the repository at this point in the history
* commit the API

* develop the front page.

* add edit logic

* develop the front page and test.

* ui change

* change the config key and group

* change rule enable to config center.

* update GlobalMockRule update logic.

* remove the GlobalMockRule

* [feature admin mock] move the diver dependency out of the project.

* [feature admin mock] remove the contributor name and date in javadoc.

* [feature admin mock] optimize the delete mock rule step.

* [feature admin mock] fix the dialog cannot be closed when delete successfully.

* [feature admin mock] add the support for h2 database.

* [feature admin mock] rollback to zookeeper registry.

* [feature admin mock] fix properties.

* [feature admin mock] change mock-admin-api maven version.

* [feature admin mock] fix the feedback and add the parameters in docker-compose.

* [feature admin mock] fix the ci problem.

* [feature admin mock] fix the ci problem.

* [feature admin mock] removed unused import.

* [feature admin mock] add license.
  • Loading branch information
brotherlu-xcq authored Nov 3, 2021
1 parent 2578af0 commit e14a255
Show file tree
Hide file tree
Showing 25 changed files with 966 additions and 70 deletions.
2 changes: 1 addition & 1 deletion docker/latest/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RUN apt-get update && apt-get install -y tini
COPY --from=0 /source/dubbo-admin-snapshot/dubbo-admin-distribution/target/dubbo-admin-0.3.0-SNAPSHOT.jar /app.jar
COPY --from=0 /source/dubbo-admin-snapshot/docker/entrypoint.sh /usr/local/bin/entrypoint.sh

ENV JAVA_OPTS ""
ENV JAVA_OPTS "-Dloader.path=/opt-libs"

ENTRYPOINT ["tini", "--", "/usr/local/bin/entrypoint.sh"]
EXPOSE 8080
15 changes: 14 additions & 1 deletion docker/stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@ services:
- zookeeper
ports:
- 8080:8080
# the db driver path
volumes:
- "/mnt/opt-libs:/opt-libs"
environment:
- admin.registry.address=zookeeper://zookeeper:2181
- admin.config-center=zookeeper://zookeeper:2181
- admin.metadata-report.address=zookeeper://zookeeper:2181
- admin.metadata-report.address=zookeeper://zookeeper:2181
- dubbo.application.name=dubbo-admin
- dubbo.registry.address=zookeeper://zookeeper:2181
- mybatis-plus.global-config.db-config.id-type=none
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- spring.datasource.url=jdbc:mysql://xxx:3306/dubbo-admin?characterEncoding=utf8&connectTimeout=1000&socketTimeout=10000&autoReconnect=true
- spring.datasource.username=root
- spring.datasource.password=mysql
# use internal h2 as database
# - spring.datasource.url=jdbc:h2:mem:~/dubbo-admin;
# - spring.datasource.username=sa
18 changes: 18 additions & 0 deletions dubbo-admin-distribution/src/bin/config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,21 @@ admin.check.sessionTimeoutMilli=3600000
server.compression.enabled=true
server.compression.mime-types=text/css,text/javascript,application/javascript
server.compression.min-response-size=10240

#dubbo config
dubbo.application.name=dubbo-admin
dubbo.registry.address=${admin.registry.address}

# mysql
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/dubbo-admin?characterEncoding=utf8&connectTimeout=1000&socketTimeout=10000&autoReconnect=true
#spring.datasource.username=root
#spring.datasource.password=mysql

# h2
spring.datasource.url=jdbc:h2:mem:~/dubbo-admin;
spring.datasource.username=sa
spring.datasource.password=

# id generate type
mybatis-plus.global-config.db-config.id-type=none
2 changes: 1 addition & 1 deletion dubbo-admin-distribution/src/bin/startup.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ set SERVER=dubbo-admin

set "JAVA_OPT=%JAVA_OPT% -Xms512m -Xmx512m -Xmn256m"

set "JAVA_OPT=%JAVA_OPT% -jar %BASE_DIR%\lib\%SERVER%.jar"
set "JAVA_OPT=%JAVA_OPT% -Dloader.path=%BASE_DIR%\opt-libs -jar %BASE_DIR%\lib\%SERVER%.jar"

call "%JAVA%" %JAVA_OPT% dubbo.admin %*
4 changes: 2 additions & 2 deletions dubbo-admin-distribution/src/bin/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ if [ ! -f "${BASE_DIR}/logs/start.out" ]; then
fi

JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/lib/${SERVER}.jar"
nohup $JAVA ${JAVA_OPT} dubbo.admin >> ${BASE_DIR}/logs/catlog.out 2>&1 &
JAVA_OPT="${JAVA_OPT} -Dloader.path=${BASE_DIR}/opt-libs -jar ${BASE_DIR}/lib/${SERVER}.jar"
nohup "$JAVA" ${JAVA_OPT} dubbo.admin >> ${BASE_DIR}/logs/catlog.out 2>&1 &
echo "${SERVER} is starting,you can check the ${BASE_DIR}/logs/catlog.out"
27 changes: 27 additions & 0 deletions dubbo-admin-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@
<version>${jjwt-version}</version>
</dependency>


<dependency>
<groupId>org.apache.dubbo.extensions</groupId>
<artifactId>dubbo-mock-api</artifactId>
</dependency>

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>

<!-- the mysql db driver need user put it in /opt-libs path -->
<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- <version>5.1.49</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -212,6 +236,9 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.2.RELEASE</version>
<configuration>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<phase>package</phase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

package org.apache.dubbo.admin;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@SpringBootApplication(exclude={
DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class
HibernateJpaAutoConfiguration.class
})
@EnableDubbo(scanBasePackages = {"org.apache.dubbo.admin.provider"})
@MapperScan(basePackages = {"org.apache.dubbo.admin.mapper"})
public class DubboAdminApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.dubbo.admin.config;

import org.apache.commons.lang3.StringUtils;

import org.apache.dubbo.admin.common.exception.ConfigurationException;
import org.apache.dubbo.admin.common.util.Constants;
import org.apache.dubbo.admin.registry.config.GovernanceConfiguration;
Expand All @@ -28,8 +26,9 @@
import org.apache.dubbo.admin.registry.metadata.MetaDataCollector;
import org.apache.dubbo.admin.registry.metadata.impl.NoOpMetadataCollector;
import org.apache.dubbo.admin.service.impl.InstanceRegistryCache;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.Environment;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
Expand All @@ -39,18 +38,12 @@
import org.apache.dubbo.registry.RegistryService;
import org.apache.dubbo.registry.client.ServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceDiscoveryFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY;
import static org.apache.dubbo.registry.client.ServiceDiscoveryFactory.getExtension;
Expand Down Expand Up @@ -138,38 +131,9 @@ GovernanceConfiguration getDynamicConfiguration() {
//throw exception
}
}
initDubboEnvironment();
return dynamicConfiguration;
}

private void initDubboEnvironment() {
Environment env = ApplicationModel.getEnvironment();
SortedMap<String, String> sortedMap = new TreeMap<>();
if (registryUrl == null) {
if (StringUtils.isNotBlank(registryAddress)) {
registryUrl = formUrl(registryAddress, registryGroup, registryNameSpace, username, password);
}
}

if (metadataUrl == null) {
if (StringUtils.isNotEmpty(metadataAddress)) {
metadataUrl = formUrl(metadataAddress, metadataGroup, metadataGroupNameSpace, username, password);
metadataUrl = metadataUrl.addParameter(CLUSTER_KEY, cluster);
}
}
if (registryUrl != null) {
sortedMap.put("dubbo.registry.address", registryUrl.toFullString());
}
if (configCenterUrl != null) {
sortedMap.put("dubbo.config-center.address", configCenterUrl.toFullString());
}
if (metadataUrl != null) {
sortedMap.put("dubbo.metadata-report.address", metadataUrl.toFullString());
}
Map<String, String> map = Collections.unmodifiableSortedMap(sortedMap);
env.updateAppConfigMap(map);
}

/*
* generate registry client
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dubbo.admin.controller;

import org.apache.dubbo.admin.annotation.Authority;
import org.apache.dubbo.admin.model.dto.MockRuleDTO;
import org.apache.dubbo.admin.service.MockRuleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* Mock Rule Controller.
*/
@Authority(needLogin = true)
@RestController
@RequestMapping("/api/{env}/mock/rule")
public class MockRuleController {

@Autowired
private MockRuleService mockRuleService;

@PostMapping
public boolean createOrUpdateMockRule(@RequestBody MockRuleDTO mockRule) {
mockRuleService.createOrUpdateMockRule(mockRule);
return true;
}

@DeleteMapping
public boolean deleteMockRule(@RequestBody MockRuleDTO mockRule) {
mockRuleService.deleteMockRuleById(mockRule.getId());
return true;
}

@GetMapping("/list")
public Page<MockRuleDTO> listMockRules(@RequestParam(required = false) String filter, Pageable pageable) {
return mockRuleService.listMockRulesByPage(filter, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dubbo.admin.mapper;

import org.apache.dubbo.admin.model.domain.MockRule;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
* The database operator of mock rule.
*/
public interface MockRuleMapper extends BaseMapper<MockRule> {
}
Loading

0 comments on commit e14a255

Please sign in to comment.