dubbo监控服务
原版的项目结构
因原版的项目比较久远 还采用的是web.xml来初始化配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Dubbo monitor</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.handu.open.dubbo.monitor.config.MonitorConfig</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
代码中可以看出除了一些基本spring配置额外的就是加载了MonitorConfig这个配置文件
/**
* Copyright 2006-2015 handu.com
* <p/>
* Licensed 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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 com.handu.open.dubbo.monitor.config;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
/**
* MonitorConfig
*
* @author Jinkai.Ma
*/
@Configuration
@ComponentScan(basePackages = {
"com.handu.open.dubbo.monitor"}, includeFilters = {
@ComponentScan.Filter(value = Service.class)})
@Import({
WebConfig.class, DubboConfig.class, MyBatisConfig.class, Security.class})
@PropertySource("classpath:/application.properties")
public class MonitorConfig {
}
点进去发现其实就是
1.初始化了包扫描 2.加载了一些基本的配置类 3.加载了application.properties文件
与dubbo有关系的其实主要就是DubboConfig.class 和application.properties这两个文件
/**
* Copyright 2006-2015 handu.com
* <p/>
* Licensed 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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 com.handu.open.dubbo.monitor.config;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**
* DubboConfig
*
* @author Jinkai.Ma
*/
@Configuration
public class DubboConfig {
private static final String REGISTRY_ADDRESS = "dubbo.registry.address";
private static final String APPLICATION_NAME = "dubbo.application.name";
private static final String APPLICATION_OWNER = "dubbo.application.owner";
private static final String PROTOCOL_PORT = "dubbo.protocol.port";
@Autowired
private Environment env;
//向spring容器中注册一个包扫描的bean
@Bean
public static AnnotationBean annotationBean() {
AnnotationBean annotationBean = new AnnotationBean();
annotationBean.setPackage("com.handu.open.dubbo.monitor");
return annotationBean;
}
//向spring容器注册一个服务配置bean声明服务的名字和服务的所属 这两个值无所谓只是用来区别在dubbo中的服务名
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName(env.getProperty(APPLICATION_NAME, "dubbo-monitor"));
applicationConfig