Spring Cloud云服务架构 - commonservice-config配置服务搭建

1. 介绍

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring EnvironmentPropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。

2. 引入pom相关jar包,其中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 http://maven.apache.org/xsd/maven-4.0.0.xsd">    
    <modelVersion>4.0.0</modelVersion>    
    
    <parent>    
        <groupId>com.ml.honghu</groupId>    
        <artifactId>commonservice</artifactId>    
        <version>0.0.1-SNAPSHOT</version>    
    </parent>    
        
    <artifactId>commonservice-config</artifactId>    
    <packaging>jar</packaging>    
    
    <name>commonservice-config</name>    
    <description>Config Server</description>    
    
    <dependencies>    
        <dependency>    
            <groupId>org.springframework.cloud</groupId>    
            <artifactId>spring-cloud-config-server</artifactId>    
        </dependency>    
        <dependency>    
            <groupId>org.springframework.cloud</groupId>    
            <artifactId>spring-cloud-starter-eureka</artifactId>    
        </dependency>    
        <dependency>    
                    <groupId>org.springframework.boot</groupId>    
                    <artifactId>spring-boot-starter-security</artifactId>    
            </dependency>    
        <dependency>    
            <groupId>org.springframework.boot</groupId>    
            <artifactId>spring-boot-starter-test</artifactId>    
            <scope>test</scope>    
        </dependency>    
    </dependencies>    
    
    <build>    
        <plugins>    
            <plugin>    
                <groupId>org.springframework.boot</groupId>    
                <artifactId>spring-boot-maven-plugin</artifactId>    
                <executions>    
                    <execution>    
                        <id>1</id>    
                        <goals>    
                            <goal>repackage</goal>    
                        </goals>    
                    </execution>    
                                    <execution>    
                                        <id>2</id>    
                                            <goals>    
                                                   <goal>build-info</goal>    
                                            </goals>    
                                    </execution>    
                </executions>    
            </plugin>    
        </plugins>    
    </build>    
</project>    
 

 3. 在src/main/java进行ConfigApplication.java启动文件配置:

package com.ml.honghu;    
    
import org.springframework.boot.SpringApplication;    
import org.springframework.boot.autoconfigure.SpringBootApplication;    
import org.springframework.cloud.config.server.EnableConfigServer;    
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;    
    
@EnableConfigServer    
@EnableEurekaClient    
@SpringBootApplication    
public class ConfigApplication {    
    
    public static void main(String[] args) {    
        SpringApplication.run(ConfigApplication.class, args);    
    }    
}    

 

4. 在src/main/resource下进行bootstrap.yml配置

server:     
port: 8888    
spring:    
  application:    
    name: commonservice-config-server    
  profiles:    
    active: discovery,native    
  cloud:    
    config:    
      server:    
        git:    
          uri: http://192.168.0.254/honghu.../honghu-config.git    
          username: honghu    
          password: 123456    
          searchPaths: config-dev    
security:    
  basic:    
    enabled: true    
  user:    
    name: honghu    
    password: 123456    
eureka:    
  client:    
    serviceUrl:    
      defaultZone: http://honghu:123456@localhost:8761/eureka/    
      honghuZone: http://honghu:123456@localhost:8761/eureka/    
    registry-fetch-interval-seconds: 300    
    availability-zones:    
      honghu: honghuZone    
  instance:    
    prefer-ip-address: true    
    metadataMap:    
      version: 1.0    
      variant: A    
      user: ${security.user.name}    
      password: ${security.user.password}    
management:    
  security:    
    enabled: false    

 

注意: 如果不从远程git或者svn库加载配置文件信息,可以配置加载本地地址,比如window下配置使用:欢迎大家一起学习研究相关技术愿意了解源码的朋友直接求求交流分享技术:2147775633

server:     
port: 8888    
spring:    
  application:    
    name: commonservice-config-server    
  profiles:    
    active: discovery,native    
  cloud:    
    config:    
      server:    
        <span style="color: #ff0000;">native.searchLocations: d:/honghu-config</span>    
security:    
  basic:    
    enabled: true    
  user:    
    name: honghu    
    password: 123456    
eureka:    
  client:    
    serviceUrl:    
      defaultZone: http://honghu:123456@localhost:8761/eureka/    
      honghuZone: http://honghu:123456@localhost:8761/eureka/    
    registry-fetch-interval-seconds: 300    
    availability-zones:    
      honghu: honghuZone    
  instance:    
    prefer-ip-address: true    
    metadataMap:    
      version: 1.0    
      variant: A    
      user: ${security.user.name}    
      password: ${security.user.password}    
management:    
  security:    
    enabled: false    

到此,整个config服务项目配置完毕!!
​​​​​

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
很高兴为您服务!以下是将jar包里的共通方法服务化的代码示例: 1. 在spring工程中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 2. 创建一个接口,定义服务方法: ```java public interface CommonService { public String commonMethod(String param); } ``` 3. 创建一个实现类,实现接口中的服务方法: ```java @Service @WebService(serviceName = "CommonService") public class CommonServiceImpl implements CommonService { @Override public String commonMethod(String param) { // 调用jar包中的共通方法 String result = CommonUtil.commonMethod(param); return result; } } ``` 4. 在启动类中添加以下注解,发布服务: ```java @EnableWs @SpringBootApplication public class Application extends SpringBootServletInitializer { @Bean public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet() { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(new AnnotationConfigApplicationContext()); servlet.setTransformWsdlLocations(true); return new ServletRegistrationBean<>(servlet, "/ws/*"); } @Bean(name = "CommonService") public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema commonSchema) { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); wsdl11Definition.setPortTypeName("CommonServicePort"); wsdl11Definition.setLocationUri("/ws"); wsdl11Definition.setTargetNamespace("http://example.com/common"); wsdl11Definition.setSchema(commonSchema); return wsdl11Definition; } @Bean public XsdSchema commonSchema() { return new SimpleXsdSchema(new ClassPathResource("common.xsd")); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 5. 创建common.xsd文件,定义服务的XML Schema: ```xml <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.com/common" targetNamespace="http://example.com/common" elementFormDefault="qualified"> <xs:element name="commonMethodRequest" type="tns:commonMethodRequest"/> <xs:element name="commonMethodResponse" type="tns:commonMethodResponse"/> <xs:complexType name="commonMethodRequest"> <xs:sequence> <xs:element name="param" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="commonMethodResponse"> <xs:sequence> <xs:element name="result" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> ``` 6. 使用SOAPUI或浏览器访问http://localhost:8080/ws/CommonService.wsdl查看服务的WSDL定义。 希望这个代码示例能够帮助您!如果您有其他问题,请随时提出。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值