实例: springcloud+nacos+gateway+webservice

父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>
    <groupId>com.nacos.project2</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>mod-test</module>
        <module>mod-gateway</module>
    </modules>
    <packaging>pom</packaging>
    <!--
    <properties>
        <spring.cloud.version>Greenwich.RELEASE</spring.cloud.version>
        <spring.cloud.alibaba.version>2.1.0.RELEASE</spring.cloud.alibaba.version>
        <spring.boot.version>2.1.3.RELEASE</spring.boot.version>
    </properties>
    -->
    <!--<properties>-->
        <!--<spring.cloud.version>Hoxton.RELEASE</spring.cloud.version>-->
        <!--<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>-->
        <!--<spring.boot.version>2.2.4.RELEASE</spring.boot.version><!– 2.2.4 –>-->
    <!--</properties>-->
    <properties>
        <spring.cloud.version>Hoxton.RELEASE</spring.cloud.version>
        <spring.cloud.alibaba.version>2.2.5.RELEASE</spring.cloud.alibaba.version>
        <spring.boot.version>2.2.7.RELEASE</spring.boot.version><!-- 2.2.4 -->
    </properties>

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

    <repositories>
        <repository>
            <id>aliyun-repos</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

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

webservice-biz模块 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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.nacos.project2</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mod-test</artifactId>

    <name>mod-test</name>
    <!-- FIXME change it to the project2's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <oracle.version>19.8.0.0</oracle.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!-- no console -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>${oracle.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.23</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.3.6</version>
        </dependency>

    </dependencies>
</project>

webservice-biz 的 入口Application,SpringApplication:

@SpringBootApplication
@EnableAspectJAutoProxy(exposeProxy = true)
@EnableScheduling
@MapperScan({"com.nacos.project2.mapper"})
public class SpringApplication
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(NacosApplication.class, args);
    }
}

定义webservice的接口api

import javax.jws.WebService;

@WebService(name = "ITestWebservice", // 暴露服务名称
        targetNamespace = "http://service.webservice_server.test.com"// 命名空间,一般是接口的包名倒序
)
public interface ITestWebservice {

    TestWSDataVO getData();
}

webservice服务的实现类:

@Service
@WebService(serviceName = "ITestWebservice", // 与接口中指定的name一致
        targetNamespace = "http://service.webservice_server.test.com", // 与接口中的命名空间一致
        endpointInterface = "com.nacos.project2.webservice.ITestWebservice" // 接口地址
)
public class TestWebservice implements ITestWebservice{
    @Override
    public TestWSDataVO getData() {

        TestWSDataVO dataVO = new TestWSDataVO();
        dataVO.setCreateDate(new Date());
        dataVO.setLongs(5L);
        dataVO.setNums(3);
        dataVO.setTestStr("str");

        WsChildVO childVO = new WsChildVO();
        childVO.setData("sdfsfdsfsf");
        dataVO.setWsChildVO(childVO);

        List<String> list = new ArrayList<>();
        list.add("adfas");
        list.add("dfsfs");
        dataVO.setList(list);
        return dataVO;
    }
}

创建cxfConfig,初始化webservice:

package com.nacos.project2.webservice;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.cxf.jaxws.EndpointImpl;
import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @Autowired
    private ITestWebservice iTestWebservice;

    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), iTestWebservice);
        endpoint.publish("/api");
        return endpoint;
    }
}

创建服务调用controller: wsClientController,通过 gateway;

package com.nacos.project2.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/ws")
public class WsClientController {

    @GetMapping(value = "/getWsInfo")
    public String getWsInfo() {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8300/webservice/webservice/api?wsdl");
        Object[] objects = new Object[0];
        ObjectMapper mapper = new ObjectMapper();
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("getData");
            System.out.println(mapper.writeValueAsString(objects[0]));
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }

        return "getWebService";
    }
}

webservice-biz的bootstrap.yml配置:

server:
  port: 8200

spring:
  application:
    name: webservice-biz
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.137.100:8848
        group: DEFAULT_GROUP
      config:
        enabled: true
        server-addr: 192.168.137.100:8848
        file-extension: yml
        group: DEFAULT_GROUP
        shared-configs[0]:
          data-id: application-frame-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
          group: DEFAULT_GROUP
        shared-configs[1]:
          data-id: application-druid-frame-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
          group: DEFAULT_GROUP

  main:
    allow-bean-definition-overriding: true

gateway服务的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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.nacos.project2</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mod-gateway</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <oracle.version>19.8.0.0</oracle.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <!-- no console -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.2.7.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

</project>

gateway服务的bootstrap.yml:

server:
  port: 8300

spring:
  application:
    name: mod-gateway
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.137.100:8848
        group: DEFAULT_GROUP
      config:
        enabled: true
        server-addr: 192.168.137.100:8848
        file-extension: yml
        group: DEFAULT_GROUP
        shared-configs[0]:
          data-id: application-frame-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
          group: DEFAULT_GROUP
        shared-configs[1]:
          data-id: application-druid-frame-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
          group: DEFAULT_GROUP

  main:
    allow-bean-definition-overriding: true

gateway服务的路由配置:

spring:
  cloud:
    inetutils:
      ignored-interfaces: enp0s8*
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
      routes:
        # webservice
        - id: mod-test-service
          uri: lb://mod-test/webservice
          predicates:
            - Path=/webservice/**
          filters:
            - StripPrefix=1

        - id: mod-test-web
          uri: lb://mod-test/ws
          predicates:
            - Path=/nacos/**
          filters:
            - StripPrefix=1

欢迎您的到来,感谢您的支持!

为您推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注