您现在的位置是:主页 > news > 建设网站com/口碑营销案例2021

建设网站com/口碑营销案例2021

admin2025/5/11 21:58:01news

简介建设网站com,口碑营销案例2021,我想自己建个网站 应该怎么做,网站建设怎样去销售1.概述 官网:http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支…

建设网站com,口碑营销案例2021,我想自己建个网站 应该怎么做,网站建设怎样去销售1.概述 官网:http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支…

1.概述

官网:http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign

Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

Feign是一个声明式的Web服务客户端,使得编写Web服务客户端变得非常容易,
只需要创建一个接口,然后在上面添加注解即可。
参考官网:https://github.com/OpenFeign/feign
在这里插入图片描述

1.1 Feign能干什么

Feign旨在使编写Java Http客户端变得更容易。
前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量。

1.2 Feign集成了Ribbon

利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用

1.3操作

1.1新建工程consumer-pay-feign

<?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>PayUtilsMode</artifactId><groupId>com.xql</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>consumer-pay-feign</artifactId><description>支付微服务消费者</description><dependencies><dependency><!-- 引入自己定义的api通用包,可以使用Entity --><groupId>com.xql</groupId><artifactId>pay-api</artifactId><version>${api.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 修改后立即生效,热部署 --><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId><version>${springloaded.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version></dependency><!-- Ribbon相关 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>${ribbo.version}</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId><version>${feign.version}</version></dependency></dependencies>
</project>
package com.xql.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.xql.api"})
@ComponentScan(value = {"com.xql.api","com.xql.consumer.controller"})
public class ConsumerPayFeignApplication {public static void main(String[] args) {SpringApplication.run(ConsumerPayFeignApplication.class, args);}}
#改端口号
server.port= 8002#配置json日期出入格式
spring.mvc.date-format= yyyy-MM-dd HH:mm:ss
spring.jackson.date-format= yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone= GMT+8#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
eureka.client.register-with-eureka=falseeureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
package com.xql.consumer.controller;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xql.api.model.DTO.TestDto;
import com.xql.api.model.Orders;
import com.xql.api.service.DeptClientService;
import com.xql.model.Vo.PageVO;
import com.xql.model.Vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;/*** @author 许清磊* @version 1.0* @ClassName ConsumerPayController* @description: TODO* @date 2021/12/7 10:03*/@RestController
public class ConsumerPayController
{
//    private static final String REST_URL_PREFIX = "http://localhost:8001";@Autowiredprivate DeptClientService service = null;@RequestMapping(value="/consumer/pay/add")public boolean add(Orders orders){return service.add(orders);}@RequestMapping(value="/consumer/pay/get/{id}")public Orders get(@PathVariable("id") String id){return service.get(id);}@SuppressWarnings("unchecked")@RequestMapping(value="/consumer/pay/list")public JSONObject list(){return service.list2();}
}

在pay-api模块新增

package com.xql.api.service;import com.alibaba.fastjson.JSONObject;
import com.xql.api.model.DTO.TestDto;
import com.xql.api.model.Orders;
import com.xql.model.Vo.PageVO;
import com.xql.model.Vo.ResultVO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@FeignClient(value = "PROVIDER-PAY")
public interface DeptClientService {@RequestMapping(value="/pay/add",method= RequestMethod.POST)public boolean add(@RequestBody Orders orders);@RequestMapping(value="/pay/get/{id}",method= RequestMethod.GET)public Orders get(@PathVariable("id") String id);@RequestMapping(value="/pay/list",method= RequestMethod.GET)public ResultVO<PageVO<Orders>> list();@RequestMapping(value="/pay/list2",method= RequestMethod.GET)public JSONObject list2();}

pom新增坐标

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId><version>${feign.version}</version></dependency>

重新把api模块 clean install

1.4 重启项目测试

Feign通过接口的方法调用Rest服务(之前是Ribbon+RestTemplate),
该请求发送给Eureka服务器(http://PROVIDER-PAY/consumer/pay/list2),
通过Feign直接找到服务接口,由于在进行服务调用的时候融合了Ribbon技术,所以也支持负载均衡作用。