eureka,feign,RestTemplate
********* 服务消费者
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@RibbonClient(name = "hello-service", configuration = TestConfiguration.class)
//@ComponentScan(excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value= ExcludeFromComponentScan.class)})
public class EurekaTest {
public static void main(String[] args) {
SpringApplication.run(EurekaTest.class, args);
}
@Bean
@LoadBalanced//eureka远程调用,必须使用 默认为轮询负载策略
RestTemplate restTemplate() {
return new RestTemplate();
}
@FeignClient("hello-service")
public interface ServiceClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
public String getHelloServer(@RequestParam("message") String message);
spring.application.name:hello-client
eureka.client.service-url.defaultZone:http://localhost:9099/eureka
server.port=9091
第一种调用方式,feign
@Autowired
private ServiceClient sclient;
@RequestMapping(value = "/helloFeign",method = RequestMethod.GET)
@ResponseBody
public String helloFeign(){
logger.info("*********"+sclient);
String s =sclient.getHelloServer("myFegin");
logger.info(s+"*********");
return s;
}
第二种调用方式,不使用feign //@EnableFeignClients
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
@ResponseBody
public String home(){ String s = restTemplate.getForObject("http://hello-service/hello?mess=xxx", String.class,"werwer");
logger.info(s+"*********");
return s;
}
********* 服务提供者
@EnableEurekaClient
@SpringBootApplication(scanBasePackages="cn.com.xmh",exclude={DataSourceAutoConfiguration.class})
public class EurekaTest {
public static void main(String[] args) {
SpringApplication.run(EurekaTest.class, args);
}
}
//@RestController
@Controller
public class TestController {
@RequestMapping("/hello")
@ResponseBody
public Object hello(HttpServletRequest request, Model model) {
System.out.println("hello********"+st);
return "helloxxxxxxxxx";
}
spring.application.name:hello-service
eureka.client.service-url.defaultZone:http://localhost:9099/eureka
server.port=9093
server.session.timeout=1
********* eureka服务
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerTest {
public static void main(String[] args) {
SpringApplication.run(EurekaServerTest.class, args);
}
}
server.port=9099
eureka.instance.hostname=localhost
#不要向注册中心注册自己
eureka.client.register-with-eureka=false
#禁止检索服务
eureka.client.fetch-registry=false
@FeignClient(url = "http://localhost:9091",name="mspurl")//调用非注册服务的接口,http接口
public interface ServiceClientCommon {
@RequestMapping(method = RequestMethod.GET, value = "/helloServer")
public String getHelloServer(@RequestParam("message") String message);
}
注册中心宕机,消费者与服务者仍可以调用
//消费者 声明式REST客户端:Feign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.com.mybootcloud</groupId>
<artifactId>mybootcloud</artifactId>
<packaging>jar</packaging>
<version>0.0.1</version>
<name>mybootcloud Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-thymeleaf</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!--compiler插件 windows平台默认使用GBK编码-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>cn.com.xmh.BusinessWebApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
zuul
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
#服务器不用注册到其他服务器
eureka.client.registerWithEureka=false
#服务器不用去服务器抓取注册信息
eureka.client.fetchRegistry=false
zuul.routes.serviceName.path=/exampleService/**
zuul.routes.serviceName.url=http://127.0.0.1:9090/
@EnableZuulProxy
测试
zuul.routes.serviceName.path=/exampleService/**
zuul.routes.serviceName.url=http://127.0.0.1:9090/
http://localhost:9092/exampleService/hello/one/two
会访问到 http://127.0.0.1:9090/hello/one/two
- 1、zuul作为整个应用的流量入口,接收所有的请求,如app、网页等,并且将不同的请求转发至不同的处理微服务模块,其作用可视为nginx。
- 2、feign则是将当前微服务的部分服务接口暴露出来,并且主要用于各个微服务之间的服务调用
Eureka服务端 @EnableEurekaServer
服务端配置eureka.server.eviction-interval-timer-in-ms:
eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒。服务下线存在两种情况,一种是客户端主要发送“服务下线”的请求,另外一种是通过定时任务扫描,每隔固定的时间清理无效的节点信息。清理无效节点的策略由客户端决定,客户端节点会设置相应的上报时间和最大超时时间(最大超时时间必须超过上报时间,推介设置为3倍关系),eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance
eureka.server.enable-self-preservation:
是否开启自我保护模式,默认为true。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定
Eureka客户端 @EnableEurekaClient “@EnableEurekaClient”注解信息,所以当服务启动之后该服务会自动注册到Eureka服务器之中
– 1、Eureka配置eureka.instance.lease-renewal-interval-in-seconds:
lease-renewal-interval-in-seconds,表示eureka client发送心跳给server端的频率。如果在leaseExpirationDurationInSeconds后,server端没有收到client的心跳,则将摘除该instance。除此之外,如果该instance实现了HealthCheckCallback,并决定让自己unavailable的话,则该instance也不会接收到流量,默认30秒。
– 2、Eureka配置eureka.instance.lease-expiration-duration-in-seconds:
lease-expiration-duration-in-seconds,表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance,该值默认为90秒。
– 3、Client端配置eureka.client.registry-fetch-interval-seconds:
表示eureka client间隔多久去拉取服务注册信息,默认为30秒,对于api-gateway,如果要迅速获取服务注册状态,可以缩小该值,比如5秒。
https://blog.csdn.net/qq_28524127/article/details/79589919
json 转换
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
@RequestMapping("/hello")
public @ResponseBody Object hello(HttpServletRequest request, Model model) {
System.out.println("hello********");
logger.info("hello********");
return "hello";
}

浙公网安备 33010602011771号