先写提纲,后续有时间完善内容和Demo
- spring boot+swagger
1)用swagger2注解生成带界面的接口文档
2)用swagger-maven-plugin生成接口文档swagger.json或者swagger.yaml
3)用swagger-codegen-maven-plugin根据swagger.json生成java客户端
4)利用xml-maven-plugin修改客户端的pom.xml文件添加依赖注入
(不使用swagger-codegen-maven-plugin自动生成的实体对象,当时自带的自定义模板功能一直没生效,不知道现在解决了没有)
5)maven打包java客户端,安装到本地或者上传到maven仓库
生成的jar客户端需要用restTemplate配置ApiClient
升级Spring Cloud后服务提供者provider的host希望从eureka server上获取,消费者可以通过ribbon调用,只需要在restTemplate上加一个@LoadBalanced注解即可
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
public @Bean
ApiClient myApiClient() throws Exception {
ApiClient c = new ApiClient(restTemplate);
c.setBasePath("http://eurekaProvider1");
return c;
}
- spring boot+cxf
将cxf工程除service实现以外打成jar包
service实现端xml配置applicationContext-ws-service.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:simple="http://cxf.apache.org/simple" xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:cxf="http://cxf.apache.org/core" xmlns:http="http://cxf.apache.org/transports/http/configuration"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
classpath:schema/spring-util-3.1.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
default-autowire="byName">
<http:conduit name="*.http-conduit">
<http-conf:client ConnectionTimeout="300000"
ReceiveTimeout="300000" AcceptEncoding="gzip" />
</http:conduit>
<cxf:bus>
<cxf:features>
<!-- <cxf:logging /> -->
</cxf:features>
</cxf:bus>
<jaxws:endpoint id="wsQueryService" implementor="#wsQueryServiceImpl"
address="/wsQueryService">
<jaxws:features>
<bean class="org.apache.cxf.transport.common.gzip.GZIPFeature" />
</jaxws:features>
<jaxws:inInterceptors>
<bean class="org.apache.cxf.transport.common.gzip.GZIPInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.transport.common.gzip.GZIPOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>
<bean id="wsQueryServiceImpl"
class="org.liufang.service.ws.impl.WsQueryServiceImpl">
</bean>
</beans>
package org.liufang.service.ws;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface IWsQueryService {
@WebMethod(operationName = "searchById")
WsInfoRS searchById(@WebParam(name = "wsIdQueryRQ") WsIdQueryRQ wsIdQueryRQ);
}
package org.liufang.service.ws.impl;
import org.liufang.service.ws.IWsQueryService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
public WsQueryServiceImpl implements IWsQueryService {
@Override
WsInfoRS searchById(WsIdQueryRQ wsIdQueryRQ){
.....
return result;
}
}
客户端配置applicationContext-ws-client.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:simple="http://cxf.apache.org/simple" xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:cxf="http://cxf.apache.org/core" xmlns:http="http://cxf.apache.org/transports/http/configuration"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
default-autowire="byName" default-lazy-init="false">
<http:conduit name="*.http-conduit">
<http-conf:client ConnectionTimeout="300000"
ReceiveTimeout="600000" AcceptEncoding="gzip" />
</http:conduit>
<jaxws:client id="wsQueryService"
serviceClass="org.liufang.service.ws.IWsQueryService"
address="${wsService.ws.url}/wsQueryService?wsdl">
<jaxws:properties>
<entry key="set-jaxb-validation-event-handler" value="false" />
<entry key="schema-validation-enabled" value="false" />
</jaxws:properties>
<jaxws:features>
<bean class="org.apache.cxf.transport.common.gzip.GZIPFeature" />
</jaxws:features>
<jaxws:inInterceptors>
<bean class="org.apache.cxf.transport.common.gzip.GZIPInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.transport.common.gzip.GZIPOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:client>
</beans>
- dubbo+zookeeper 或者 dubbo+edas
推荐文章:高效开发 Dubbo
先写一个service-api工程,然后用service-impl实现用dubbo注册到zookeeper中,客户端通过service-api就可以调用方法
service-api.jar定义接口
package org.liufang.censor.api;
public interface ICensorService {
boolean hasForbidden(String content) throws Exception;
}
service-impl实现接口方法
package org.liufang.censor.service;
import org.liufang.censor.api.ICensorService;
public class CensorService implements ICensorService {
@Override
public boolean hasForbidden(String content) throws Exception {
return true;
}
}
service-impl中配置dubbo注册censor-services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="false" default-autowire="byName">
<context:annotation-config/>
<dubbo:application name="Censor-Service"/>
<dubbo:registry protocol="zookeeper" address="${public.dubbo.registry.url}"/>
<dubbo:protocol name="dubbo" port="${dubbo.export.port}"/>
<!-- 提供违禁词dubbo服务 -->
<dubbo:service interface="org.liufang.censor.api.ICensorService"
ref="censorService" version="${dubbo.export.version}"/>
<bean id="censorService" class="org.liufang.censor.service.CensorService">
</bean>
</beans>
客户端client注入一个bean,就可以调用方法了
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="Censor-Client" />
<!-- 使用zookeeper注册中心 -->
<dubbo:registry address="zookeeper://192.168.1.1:2181" />
<dubbo:protocol name="dubbo" port="20889" />
<dubbo:reference id="censorService" interface="org.liufang.censor.api.ICensorService" version="LATEST"/>
</beans>
@Autowired
ICensorService censorService;
void test(){
Assert Boolean.TRUE.equals(censorService.hasForbidden("我违规了"));
}
- FeignClient
微服务中实现, 必须是Spring cloud 项目
feign-plus可以在Spring boot中使用 - 自定义注解接口实现
参照 springboot实战之常用http客户端整合
对应的代码实现链接
6.Retrofit构建Spring bean
@Configuration
public class MyServiceConfig {
@Autowired
private Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder;
@Bean
public MyService myService() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://127.0.0.1/")
.client(new OkHttpClient.Builder()
.connectTimeout(3000, TimeUnit.MILLISECONDS)
.writeTimeout(3000, TimeUnit.MILLISECONDS)
.readTimeout(3000, TimeUnit.MILLISECONDS).build())
.addConverterFactory(JacksonConverterFactory.create(
jackson2ObjectMapperBuilder.build()))
.build();
return retrofit.create(MyService.class);
}
}
MyService.java
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;
import java.util.Map;
public interface MyService {
@GET("/test")
Call<Object> test(@QueryMap Map<String, String> map);
@GET("/test2")
Call<MyResponse> test2(@Query("action") String action, @Query("id") String id);
}